I want to taste the newest Java 19 feature, also do not change the default Java Home( Java 17).
So I create a toolchains.xml in the ~/.m2, and define a jdk
type toolchain like this.
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>D:/jdks/jdk8</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>D:/jdks/jdk11</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>17</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>D:/jdks/jdk17</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>19</version>
<vendor>oracle</vendor>
</provides>
<configuration>
<jdkHome>D:/jdks/jdk-19</jdkHome>
</configuration>
</toolchain>
<!-- other toolchains -->
<toolchain>
<type>netbeans</type>
<provides>
<version>15</version>
</provides>
<configuration>
<installDir>D:/devtools/netbeans</installDir>
</configuration>
</toolchain>
</toolchains>
Then I added the following plugins in my project POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>19</version>
<vendor>oracle</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>com.example.demo.RecordPatternExample</mainClass>
<commandlineArgs>--enable-preview</commandlineArgs>
<arguments>
<argument>--enable-preview</argument>
</arguments>
</configuration>
</plugin>
As suggested in this question, I also added --enable-preview
to the .mvn/jvm.config.
But when running the project in command line.
mvn clean package exec:java
I still got the exception like this.
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ record-pattern ---
[INFO] Toolchain in maven-compiler-plugin: JDK[D:/jdks/jdk-19]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\hantsylabs\java-sandbox\record-pattern\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ record-pattern ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\hantsylabs\java-sandbox\record-pattern\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ record-pattern ---
[INFO] Toolchain in maven-compiler-plugin: JDK[D:/jdks/jdk-19]
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ record-pattern ---
[INFO] Toolchain in surefire-plugin: JDK[D:/jdks/jdk-19]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ record-pattern ---
[INFO] Building jar: D:\hantsylabs\java-sandbox\record-pattern\target\record-pattern-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ record-pattern ---
[WARNING]
java.lang.UnsupportedClassVersionError: com/example/demo/RecordPatternExample has been compiled by a more recent version of the Java Runtime (class file version 63.65535), this version of the Java Runtime only recognizes class file versions up to 61.0
Obviously the --enable-preview
option was not applied with exec:java
and toolchain configured JDK, but it worked well with maven-compiler-plugin.
The sample project is shared on my Github.
toolchain.xml
ortoolchains.xml
? should be the latter, but you wrote the former. did the compiler plugin pick up your toolchain configuration? – Crimpytoolchains.xml
. The compiler plugin is working well and picked up the--enable-preview
arguments. But there is no way to make exec plugin working as expected. – Catercorner--enable-preview
, but not using toolchain configuration altogether. However, if compiler plugin picked up the new toolchain, so should exec:java. – Crimpymvn toolchains:toolchain exec:java
and see if that works – Crimpymvn clean package exec:java
again, and updated the whole log ofcompile
,testCompile
andtest
, andexec:java
, it seemsexec
does not run against the toolchain configured JDK. – Catercorner