我想尝尝最新的Java 19功能,也不要更改默认的Java Home(Java 17)。
所以我在~/.m2中创建了一个toolchains.xml,并像这样定义了一个
jdk
类型的工具链。
<?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>
然后我在我的项目 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>
按照建议在这个问题中,我还将
--enable-preview
添加到.mvn/jvm.config。
但是在命令行运行项目时。
mvn clean package exec:java
我仍然遇到这样的异常。
[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
显然,
--enable-preview
选项不适用于exec:java
和工具链配置的JDK,但它与maven-compiler-plugin配合使用效果很好。
示例项目已在我的 Github 上共享。
对此进行了调查,对我来说这似乎无法完成:
exec:java
似乎没有工具链支持。如果您查看用于 exec:java
的 ExecJavaMojo 源代码,您会发现它没有对工具链的引用,因此它无法工作。将其与包含它们的 ExecMojo 源代码 进行比较。
如果您将配置更改为使用
exec:exec
,则安装应该可以正常工作:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>--enable-preview</argument>
<argument>-classpath</argument>
<classpath/>
<argument>com.example.demo.RecordPatternExample</argument>
</arguments>
</configuration>
</plugin>
使用
mvn exec:exec
并配置工具链进行测试运行:
[INFO] --- exec-maven-plugin:3.1.0:exec (default-cli) @ record-pattern ---
[INFO] Toolchain in exec-maven-plugin: JDK[C:/Program Files/Java/jdk-19+36]
Hantsy Bai
Circle with r:1.2, area:4.5216
Square with x:1.2, area:1.44
Rectangle with x: 1.0 and y:2.0, area:2.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.350 s
[INFO] Finished at: 2022-10-12T13:25:56+03:00
[INFO] Final Memory: 10M/25M
[INFO] ------------------------------------------------------------------------
exec-maven-plugin 文档没有说得很清楚,但是要让插件使用工具链有两个要求:
你必须使用exec:exec。正如其他人指出的那样, exec:java 用于在 maven 运行的同一虚拟机中运行 java 类。
您必须将其指定为简单的“java”。
然后插件就可以正确使用jdk工具链配置了。