有时您想使用JDK21(甚至更新的JDK)的那些新的幻想功能,例如预览功能,也许还有一些孵化器的类。
那么,如何配置Maven构建以支持这种游戏课程?这比您想象的要容易。让我们开始配置。我的假设是,您想使用JDK21的预览功能(例如字符串模板(JEP430))来玩耍。我刚刚选择了此JEP进行演示。您可以选择预览中的任何JEP。
首先要知道您必须通过以下方式激活预览功能
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<enablePreview>true</enablePreview>
</configuration>
</plugin>
自Maven Compiler Plugin版本3.10.1以来,enablePreview
的配置存在。因此,总的来说,我建议使用plugins的最新版本,否则可能会发现某些配置选项或与新的JDK版本完全不存在或特殊内容。
这应该在您的pom.xml
文件的pluginManagement
部分中进行。
因此,现在您可以在测试代码中使用此新功能(如果愿意,也可以在生产代码中使用它):
package com.soebes.jdk21;
import org.junit.jupiter.api.Test;
import static java.lang.StringTemplate.STR;
import static org.assertj.core.api.Assertions.assertThat;
class TemplateTest {
@Test
void name() {
String name = "Jon";
String info = STR."My name is \{name}";
assertThat(info).isEqualTo("My name is Jon");
}
}
好吧,一切顺利,但是请等一下,在您可以进行此测试之前,您必须做更多的事情。我们需要更改配置以在预览功能的支持下运行测试。这是为了使JVM提供在执行测试期间使用的选项--enable-preview
所必需的。这必须如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--enable-preview
</argLine>
</configuration>
</plugin>
正如我之前提到的,我强烈建议使用plugins的最新版本。
一件重要的事情要提及。如果您运行这些测试或整个构建,您将获得一些WARNING
,如以下内容:
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ jdk21 ---
[INFO] Changes detected - recompiling the module! :dependency
[INFO] Compiling 5 source files with javac [release 21] to target/test-classes
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[12,23] string templates are a preview feature and may be removed in a future release.
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[12,23] string templates are a preview feature and may be removed in a future release.
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[5,24] java.lang.StringTemplate is a preview API and may be removed in a future release.
[INFO]
如果您已经在制作中使用了此类功能,则这些警告将在制作代码汇编期间发出。这清楚地表明您正在使用这种preview features。
和一个极为重要的提示:
切勿在任何地方使用预览功能。不在您公司的存储库马槽中,也不是进入中央存储库
StringTemplate
类被标记为独立的预览功能,它是软件包java.lang
:
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
public interface StringTemplate {
...
}
所以另一件好事是使用像Vector
API这样的孵化器中的类?好的。我们需要做什么?
第一件事是通过--add-modules jdk.incubator.vector
信息增强我们以前的配置
喜欢以下内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<enablePreview>true</enablePreview>
<compilerArgs>
<arg>--add-modules</arg>
<arg>jdk.incubator.vector</arg>
</compilerArgs>
</configuration>
</plugin>
当然,您必须添加相同的内容才能添加到maven-surefire-plugin
配置中,以激活测试中的jdk.incubator.vector
模块。这意味着Maven-Surefire-Plugin的配置应看起来如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--enable-preview
--add-modules jdk.incubator.vector
</argLine>
</configuration>
</plugin>
这像以前一样导致警告:
..
[INFO] --- compiler:3.11.0:compile (default-compile) @ jdk21 ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 2 source files with javac [release 21] to target/classes
[WARNING] using incubating module(s): jdk.incubator.vector
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ jdk21 ---
[INFO] skip non existing resourceDirectory ../jdk21/src/test/resources
[INFO] skip non existing resourceDirectory ../jdk21/src/test/resources-filtered
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ jdk21 ---
[INFO] Changes detected - recompiling the module! :dependency
[INFO] Compiling 5 source files with javac [release 21] to target/test-classes
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[12,23] string templates are a preview feature and may be removed in a future release.
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[12,23] string templates are a preview feature and may be removed in a future release.
[WARNING] using incubating module(s): jdk.incubator.vector
[WARNING] ../jdk21/src/test/java/com/soebes/jdk21/TemplateTest.java:[5,24] java.lang.StringTemplate is a preview API and may be removed in a future release.
[INFO]
[INFO] --- surefire:3.1.2:test (default-test) @ jdk21 ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
WARNING: Using incubator modules: jdk.incubator.vector
[INFO] Running com.soebes.jdk21.TemplateTest
..
基于JDK21+的使用的小提示。从JDK21开始,JEP-451已集成到JDK21构建中,这意味着使用动态加载的代理,例如,著名的Mockito库结果也陷入了警告,这看起来像这样:
..
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.217 s -- in com.soebes.jdk21.SequencedCollectionTest
[INFO] Running com.soebes.jdk21.AFinalClassTest
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
WARNING: A Java agent has been loaded dynamically (/Users/khm/.m2/repository/net/bytebuddy/byte-buddy-agent/1.14.5/byte-buddy-agent-1.14.5.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.472 s -- in com.soebes.jdk21.AFinalClassTest
[INFO] Running com.soebes.jdk21.incubator.VectorTest
..
这使得有必要将补充配置-XX:+EnableDynamicAgentLoading
添加到您的maven-surefire-plugin配置中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--enable-preview
--add-modules jdk.incubator.vector
-XX:+EnableDynamicAgentLoading
</argLine>
</configuration>
</plugin>
一个完整的工作示例,显然需要JDK21最新的EA构建可以找到here。
快乐的编码。