Compile a JDK12 preview feature with Maven

前提是你 提交于 2019-11-27 08:51:46

Step 1: One can make use of the following maven configurations to compile the code using the --enable-preview along with --release 12 argument.

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

Note:- I had to also ensure on my MacOS that my ~/.mavenrc file was configured to mark java 12 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/jdk12-updates-1.0.0-SNAPSHOT.jar #the last argument being the path to my jar

This produces the output as expected as:

Source on GitHub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!