使用Maven编译JDK12预览功能

时间:2018-09-08 06:31:14

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

通过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尝试此功能?

2 个答案:

答案 0 :(得分:16)

第1步:人们可以利用以下maven配置使用--enable-preview--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>
                <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>com.stackoverflow.nullpointer.expression.SwitchExpressions</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

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

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

mvn clean verify 

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

java --enable-preview -jar target/jdk12-updates-1.0.0-SNAPSHOT.jar #the last argument being the path to my jar

这将产生预期的输出:

enter image description here

Source on GitHub

答案 1 :(得分:1)

要启用预览功能,必须在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>