使用Maven编译并执行JDK预览功能 [英] Compile and execute a JDK preview feature with Maven

查看:189
本文介绍了使用Maven编译并执行JDK预览功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 JDK/12 EarlyAccess Build 10 ,JEP-325交换机表达式已集成为一个JDK中的预览功能.表达式的示例代码(同样在JEP中):

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):

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!");
}

其中Day是作为

public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

预览语言和VM功能JEP-12 已经详细说明了如何在此过程中启用功能使用javacjava进行编译和运行.

The Preview Language and VM Features JEP-12 already elaborate how a feature can be enabled during compile and runtime using javac and java.

如何使用Maven尝试此功能?

How can one try out this feature using Maven?

推荐答案

步骤1

一个人可以利用以下maven配置使用--enable-preview--release 12 +(例如131415)自变量来编译代码.

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>

注意 :-我还必须确保在MacOS上将~/.mavenrc文件配置为将Java 13标记为为maven配置的默认Java.

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.

执行maven命令从模块类构建jar

Execute the maven command to build the jar from the module classes

mvn clean verify 

步骤3

使用命令行执行上一步中创建的jar的主类,如下所示:

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

最后一个参数是maven构建的jar的路径.

这将产生预期的输出:

(屏幕快照来自上一次执行.)

(screenshot is from a previous execution.)

来自GitHub的源

这篇关于使用Maven编译并执行JDK预览功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆