java - 使用 Maven 编译并执行 JDK 预览功能

标签 java maven switch-statement java-12 preview-feature

JDK/12 EarlyAccess Build 10 ,JEP-325 Switch Expressions 已作为预览功能集成在 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!");
}

其中 Day 是一个枚举为

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

Preview Language and VM Features JEP-12已经详细说明了如何在编译和运行时使用 javacjava 启用功能。

如何使用 Maven 试用此功能?

最佳答案

步骤 1

可以使用以下 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>

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

第二步

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

mvn clean verify 

第三步

使用命令行执行上一步创建的jar的主类为:

java --enable-preview -jar target/forty-bits-of-java-1.0.0-SNAPSHOT.jar

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

这会产生预期的输出:

enter image description here

(屏幕截图来自之前的执行。)

Source on GitHub


编辑:从不需要的调试 session 中学习,使用格式如下的参数:

<compilerArgs>
    <arg>--enable-preview</arg>
</compilerArgs>

原因是,如果您指定两个不同的参数,它在配置验证期间不会失败,并且稍后找到的参数会否决有效配置:

<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>-Xlint:all</compilerArgs>

关于java - 使用 Maven 编译并执行 JDK 预览功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232681/

相关文章:

java - 找不到 PropertySource : I/O error on GET request

c# - 使用 Jenkins 的 Maven 聚合器对 c# 多模块项目进行 Sonarqube StyleCop 分析

python - 基于字典的 switch case 中的 NameError

java - 组合字符串未附加到值

java - 优先级队列没有根据Java中的优先级返回正确的值

java - 如何解析java源代码而不存储空行?

c# - 使用JNA将c#的字符串转换为java

java - 编译 PrimeFaces 1.1

c++ - cin 和 switch 奇怪的行为

javascript - 是否可以在 Javascript 中将代码块分配给 switch case 两次?