使用Maven编译和执行JDK预览功能

45

使用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
}
预览语言和VM功能JEP-12 已经详细说明了如何使用 javacjava 在编译和运行时启用功能。那么如何使用Maven尝试这个功能呢?
3个回答

60

步骤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。

步骤2

执行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

(截图来自上一次执行。)

GitHub上的源代码


编辑:从一次不必要的调试会话中学到的经验,使用以下格式中的参数:

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

原因是,如果您指定了两个不同的参数,则在配置验证期间不会失败,并且后面找到的参数将覆盖有效配置:

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

1
maven-compiler-plugin的文档https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html指出,除非fork=true,否则不会使用compilerArgs,而默认情况下fork=false。因此,我不确定上述内容是如何工作的。 - William
1
我的配置文件中发生了变化:下面提到的插件行“<!-- This is just to make sure the class is set as main class to execute from the jar-->”不在我的配置文件中,但其余步骤相同,这些步骤对我没有起作用,我仍然需要在执行“javac”时指定“--enable-preview --release 13”。否则我会遇到错误。 - niranjan_harpale

19
自从Maven编译器插件的版本3.10.1以来,就有一个特定参数可以启用预览功能:dedicated parameter

<enablePreview>

将其设置为true以启用Java编译器的预览语言功能。

  • 类型: 布尔值
  • 自从: 3.10.1
  • 必需:
  • 用户属性: maven.compiler.enablePreview
  • 默认值: false

示例:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.1</version>
    <configuration>
        <release>${java.version}</release>
        <enablePreview>true</enablePreview>
    </configuration>
</plugin>

注意,对于Surefire(和Failsafe),没有这样的参数,您必须使用<argLine>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>

2
感谢分享!不幸的是,IntelliJ IDEA 2022.2 在导入 Maven 项目时仍需要编译器参数“--enable-preview”,尚未支持此参数。 - Radzivon Hrechukha
1
很遗憾,对于IntelliJ IDEA 2022.3.2,这仍然是真实的:IDEA-296303 - beatngu13

16
为启用预览特性,您必须在compilerArgs下的pom.xml中定义--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>

1
需要配置surefire插件才能运行单元测试并避免出现错误,例如:Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for YourTest - Donald Duck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接