Compile and execute a JDK preview feature with Maven
Step 1
One can make use of the following maven configurations to compile the code using the --enable-preview
along with --release 12
+ (e.g. 13
, 14
, 15
) argument.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release> <!-- <release>13/14/15</release> -->
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<!-- This is just to make sure the class is set as main class to execute from the jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>edu.forty.bits.expression.SwitchExpressions</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note:- I had to also ensure on my MacOS that my ~/.mavenrc
file was configured to mark java 13 as the default java configured for maven.
Step 2
Execute the maven command to build the jar from the module classes
mvn clean verify
Step 3
Use the command line to execute the main class of the jar created in the previous step as :
java --enable-preview -jar target/forty-bits-of-java-1.0.0-SNAPSHOT.jar
the last argument is the path to the jar built by maven.
This produces the output as expected as:
(screenshot is from a previous execution.)
Source on GitHub
Edit: A learning from an unwanted debugging session, use the arguments in the format as follows:
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
Reason being, if you specify two different arguments it doesn't fail during the configuration validation and the one found later overrules the effective config:
<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>-Xlint:all</compilerArgs>
Maven Exec Plugin with Preview Features
The problem is that exec:java runs in the same maven java process, which by default isn't started with --enable-preview
.
You could instead switch to exec:exec
, but one way to still use exec:java is to create a .mvn/jvm.config
file containing --enable-preview
. You can put this in your project's root directory and check into git. Or create a MVN_OPS environment variable.
Reference: https://maven.apache.org/configure.html
How to run a single Java file with preview features enabled in Netbeans 12?
This can be configured within the project properties.
- Right-click on the project and select Properties.
- Select Actions in the left menu.
- A list of actions is presented. Select Run file via main().
- Edit the
exec.args
property by adding--enable-preview
immediately behind the equals sign.
Cant run maven project. Try running with '--enable-preview'
I suspect that the reason Maven's jvm.config
does not work is because those arguments apply to the JVM which runs Maven, but Spring Boot's run
goal is forking a new JVM process without those arguments.
You can either disable forking using the fork
config property, so your app runs in the same JVM as Maven did, or specify jvmArguments
for the forked process.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
...
</excludes>
<fork>false</fork><!-- jvmArguments OR this, but not both -->
<jvmArguments>--enable-preview</jvmArguments>
</configuration>
</plugin>
Documentation for those 2 properties, as well others which might be useful, is here
Maven JavaDoc --enable-preview
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<release>14</release>
<additionalOptions>--enable-preview</additionalOptions>
...
Related Topics
Correct Way of Throwing Exceptions with Reactor
Java Read Large Text File with 70Million Line of Text
Get First Date of Current Month in Java
What Is the Best/Simplest Way to Read in an Xml File in Java Application
How to Get a Spring Bean in a Servlet Filter
How to Change My Windows Desktop Wallpaper Programmatically in Java/Groovy
Is Gnu's Java Compiler (Gcj) Dead
What Java 8 Stream.Collect Equivalents Are Available in the Standard Kotlin Library
When Does Hashset 'Add' Method Calls Equals
How to Deserialize Js Date Using Jackson
Java 8: Where Is Trifunction (And Kin) in Java.Util.Function? or What Is the Alternative
Keylistener, Keypressed Versus Keytyped
Output an Image File from a Servlet
Accessing the Last Entry in a Map
Printing in Java to Label Printer