How to enable Java 12 preview features with Gradle?

You need to configure the JavaCompile tasks, so that Gradle passes this option to the Java compiler when compiling.

Something like this should work:

tasks.withType(JavaCompile).each {
    it.options.compilerArgs.add('--enable-preview')
}

To run the app/tests we need to add jvmArgs.

Example:

test {
    jvmArgs(['--enable-preview'])
}

Leave a Comment