how to set the use --enable-preview compile and run flags from gradle?

adding this to the build.gradle file:

compileJava {
    options.warnings = false
    options.deprecation = false
    options.compilerArgs += ["-Xdoclint:none", "-Xlint:none", "-nowarn"]
    options.compilerArgs += ["-Xlint"]
    options.compilerArgs += ["--enable-preview"]
    options.compilerArgs += ["-source 14"]
    // options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" 
}

also, the run option is required:

tasks.withType(JavaExec) { jvmArgs += '--enable-preview' }

might fix, or is at least on the right track I think.


To make this work you can modify the compileJava task and add this flag. Add this to your build.gradle :

compileJava {
    options.compilerArgs += ['--enable-preview']
}

This will make sure that your code will compile.


If you have other tasks which require compilation (for example compileTestJava) you can enable this flag for all tasks which have type JavaCompile :

tasks.withType(JavaCompile).all {
    options.compilerArgs += ['--enable-preview']
}

To enable this flag for test tasks you can do the following :

tasks.withType(Test).all {
    jvmArgs += '--enable-preview'
}

You also have to make sure to add this flag for the JVM that will run your code :

tasks.withType(JavaExec) {
    jvmArgs += '--enable-preview'
}

This is described in the corresponding JEP :

Developers who wish to use preview language features in their programs must explicitly enable them in the compiler and the runtime system