44

With JDK/12 EarlyAccess Build 10, the JEP-325 Switch Expressions has been integrated as a preview feature in the JDK. A sample code for the expressions (as in the JEP as well):

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

where Day being an enum as

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

The Preview Language and VM Features JEP-12 already elaborate how a feature can be enabled during compile and runtime using javac and java.

How can one try out this feature using Maven?

Naman
  • 27,789
  • 26
  • 218
  • 353

3 Answers3

58

Step 1

One can make use of the following maven configurations to compile the code using the --enable-preview along with --release 12+ (e.g. 13, 14, 15) argument.

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

Note:- I had to also ensure on my MacOS that my ~/.mavenrc file was configured to mark java 13 as the default java configured for maven.

Step 2

Execute the maven command to build the jar from the module classes

mvn clean verify 

Step 3

Use the command line to execute the main class of the jar created in the previous step as :

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

the last argument is the path to the jar built by maven.

This produces the output as expected as:

enter image description here

(screenshot is from a previous execution.)

Source on GitHub


Edit: A learning from an unwanted debugging session, use the arguments in the format as follows:

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

Reason being, if you specify two different arguments it doesn't fail during the configuration validation and the one found later overrules the effective config:

<compilerArgs>--enable-preview</compilerArgs>
<compilerArgs>-Xlint:all</compilerArgs>
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    maven-compiler-plugin doco https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html states that compilerArgs aren't used unless fork=true, and fork=false by default. So I'm not sure how the above is working. – William Nov 12 '19 at 22:57
  • 1
    Change in my conf is: plugin mentioned below line `` are not present in my conf, but rest steps are same, and these steps did not work for me, I still have to specify `--enable-preview --release 13` while doing `javac`. Otherwise I get the errors. – niranjan_harpale Apr 09 '20 at 14:17
15

Since version 3.10.1 of the Maven Compiler Plugin, there is a dedicated parameter for enabling preview features:

<enablePreview>

Set to true to Enable preview language features of the java compiler

  • Type: boolean
  • Since: 3.10.1
  • Required: No
  • User Property: maven.compiler.enablePreview
  • Default: false

Example:

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

Note that for Surefire (and Failsafe), there is no such parameter and you have to use <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>
beatngu13
  • 7,201
  • 6
  • 37
  • 66
  • 2
    Thanks for sharing! Unfortunately IntelliJ IDEA 2022.2 does not respect this parameter [yet] when importing a Maven project, it still requires compilerArgs --enable-preview – Radzivon Hrechukha Aug 07 '22 at 19:15
  • 1
    @RadzivonHrechukha unfortunately, this is still true for IntelliJ IDEA 2022.3.2: [IDEA-296303](https://youtrack.jetbrains.com/issue/IDEA-296303) – beatngu13 Feb 08 '23 at 22:30
14

To enable preview feature you must define --enable-preview in pom.xml under compilerArgs

in below I mention how to enable preview feature with 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>
ricky
  • 325
  • 2
  • 4
  • surefire plugin config piece is requred to be able to run unit-tests and avoid errors like:```Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for YourTest```. – Donald Duck Feb 21 '21 at 18:04