【问题标题】:Compile and execute a JDK preview feature with Maven使用 Maven 编译并执行 JDK 预览功能
【发布时间】:2019-02-13 09:44:41
【问题描述】:

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

其中Day 是一个枚举

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

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

如何使用 Maven 试用此功能?

【问题讨论】:

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


    【解决方案1】:

    步骤 1

    可以使用以下 maven 配置来编译代码,使用 --enable-preview--release 12+(例如 131415)参数。

    <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的路径。

    这会产生预期的输出:

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

    Source on GitHub


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

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

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

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

    【讨论】:

    • maven-compiler-plugin doco maven.apache.org/plugins/maven-compiler-plugin/… 声明除非 fork=true 和 fork=false,否则不会使用 compilerArgs。所以我不确定上面是如何工作的。
    • 我的 conf 中的更改是:&lt;!-- This is just to make sure the class is set as main class to execute from the jar--&gt; 下面提到的插件在我的 conf 中不存在,但其余步骤相同,这些步骤对我不起作用,我仍然必须指定 @987654337 @同时做javac。否则我会得到错误。
    【解决方案2】:

    要启用预览功能,您必须在 pom.xml 中的 compilerArgs 下定义 --enable-preview

    在下面我提到了如何使用 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>
    

    【讨论】:

    • surefire 插件配置块需要能够运行单元测试并避免出现以下错误:Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for YourTest
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多