使用Maven编译JDK12预览功能

Nam*_*man 23 java switch-statement maven 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!");
}
Run Code Online (Sandbox Code Playgroud)

在哪里Day作为一个枚举

public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Run Code Online (Sandbox Code Playgroud)

预览语言和VM特点JEP-12已经阐述了如何功能可以编译和使用运行时期间启用javacjava.

如何使用Maven试用这个功能?

Nam*_*man 27

步骤1:可以使用以下maven配置使用--enable-previewwith with --release 12参数编译代码.

<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</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>
Run Code Online (Sandbox Code Playgroud)

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

第2步:执行maven命令从模块类构建jar

mvn clean verify 
Run Code Online (Sandbox Code Playgroud)

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

java --enable-preview -jar target/forty-bits-of-java-1.0.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

这会产生预期的输出:

在此输入图像描述

来自GitHub


bea*_*u13 22

从 Maven Compiler Plugin 3.10.1 版本开始,有一个专用参数用于启用预览功能:

<enablePreview>

设置为 true 以启用 java 编译器的预览语言功能

  • 类型: boolean
  • 自从: 3.10.1
  • 必需的: No
  • 用户属性: 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>
Run Code Online (Sandbox Code Playgroud)

请注意,对于 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>
Run Code Online (Sandbox Code Playgroud)

  • 感谢分享!不幸的是,在导入 Maven 项目时,IntelliJ IDEA 2022.2 尚未考虑此参数,它仍然需要compilerArgs --enable-preview (2认同)

ric*_*cky 10

要启用预览功能,您必须在编译器参数下的 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>
Run Code Online (Sandbox Code Playgroud)