Compile and Execute a Jdk Preview Feature with Maven

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:

Sample Image

(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.

  1. Right-click on the project and select Properties.
  2. Select Actions in the left menu.
  3. A list of actions is presented. Select Run file via main().
  4. 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



Leave a reply



Submit