Java 使用 Maven 编译和执行 JDK 预览功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52232681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile and execute a JDK preview feature with Maven
提问by Naman
With JDK/12 EarlyAccess Build 10, the JEP-325 Switch Expressions has been integrated as a preview feature in the JDK. A sample code for the expressions (as in the JEP as well):
在JDK/12 EarlyAccess Build 10 中,JEP-325 开关表达式已作为预览功能集成到 JDK 中。表达式的示例代码(在 JEP 中也是如此):
Scanner scanner = new Scanner(System.in);
Day day = Day.valueOf(scanner.next());
switch (day) {
case MONDAY, TUESDAY -> System.out.println("Back to work.") ;
case WEDNESDAY -> System.out.println("Wait for the end of week...") ;
case THURSDAY,FRIDAY -> System.out.println("Plan for the weekend?");
case SATURDAY, SUNDAY -> System.out.println("Enjoy the holiday!");
}
where Day
being an enum as
其中Day
作为一个枚举作为
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
The Preview Language and VM Features JEP-12already elaborate how a feature can be enabled during compile and runtime using javac
and java
.
该预览语言和VM特点JEP-12已经阐述了如何功能可以编译和使用运行时期间启用javac
和java
。
How can one try out this feature using Maven?
如何使用 Maven 试用此功能?
采纳答案by Naman
Step 1
第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.
可以使用以下 maven 配置来编译代码,--enable-preview
并使用--release 12
+(例如13
, 14
, 15
)参数。
<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.
注意:- 我还必须确保在我的 MacOS 上我的~/.mavenrc
文件被配置为将 java 13 标记为为 maven 配置的默认 java。
Step 2
第2步
Execute the maven command to build the jar from the module classes
执行 maven 命令从模块类构建 jar
mvn clean verify
Step 3
第 3 步
Use the command line to execute the main class of the jar created in the previous step as :
使用命令行执行上一步创建的jar的主类为:
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.
最后一个参数是 maven 构建的 jar 的路径。
This produces the output as expected as:
这会产生预期的输出:
(screenshot is from a previous execution.)
(截图来自之前的执行。)
回答by ricky
To enable preview feature you must define --enable-preview in pom.xml under compilerArgs
要启用预览功能,您必须在编译器参数下的 pom.xml 中定义 --enable-preview
in below I mention how to enable preview feature with java 13.
在下面我提到了如何使用 java 13 启用预览功能。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>