Gradle Goodness: Enabling Preview Features For Java
Check out how to enable and play with preview Java features in Gradle.
Join the DZone community and get the full member experience.Join For Free
Java introduced preview features in the language starting in Java 12. These features can be tried out by developers, but are still subject to change and can even be removed in the next release. By default, the preview features are not enabled when we want to compile and run our Java code. We must explicitly specify that we want to use the preview feature to the Java compiler and Java runtime using the command-line argument
--enable-preview. In Gradle, we can customize our build file to enable preview features. We must customize tasks of type
JavaCompile and pass
--enable-preview to the compiler arguments. Also tasks of type
JavaExec must be customized where we need to add the JVM argument
In the following Gradle build script written in Kotlin, we have a Java project written with Java 15 where we reconfigure the tasks to enable preview features:
Written with Gradle 6.8.3
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.