Discover how to enable Java 14 preview features in Gradle, Maven, Idea and Command line.
Writing my next post
about the improved instanceof
operator in Java 14, I have noticed that enabling the preview features might be a good candidate for a short blog post to reference in other places.
Let’s start with the basics.
Command line
Standalone files with preview features of Java 14 can be easily run from the command line (especially as calling javac
is no longer needed):
java --source 14 --enable-preview J14Instanceof.java
Idea
IntelliJ IDEA is known to support the new features and technologies in advance. With Java 14, it is available in Idea 2020.1, which the EAP (Early Access Program) version has been released in January 2020 .
It should be enough to just change the Java language settings in a given project to Java 14.
Maven
For Maven, just a few lines of XML should be required (I haven’t tested, I’m a Gradle guy ;-) :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
Gradle
For Gradle, version 6.3 (currently available as SNAPSHOT only ) is required for run with Java 14. To cover multiple compile and test tasks in the project the following construction could be used:
sourceCompatibility = 14
tasks.withType(JavaCompile) {
options.compilerArgs += '--enable-preview'
}
tasks.withType(Test) {
jvmArgs += "--enable-preview"
}
Summary
Nothing fancy in this post, but I feel in my bones that I will use it as a reference more than once during my Migration to modern Java training :-).
Lead photo by StockSnap, published in Pixabay, Pixabay License.