Maven Exec Plugin with Preview Features

1.4k views Asked by At

It's easy to compile your Java sources with --enable-preview:

<!-- Enable preview features -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <release>15</release>
        <compilerArgs>--enable-preview</compilerArgs>
    </configuration>
</plugin>

But how can you then run exec:java? Using

<!-- Exec plugin.. run with `mvn exec:java` -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <configuration>
        <mainClass>${mainClass}</mainClass>
        <commandlineArgs>--enable-preview</commandlineArgs>
        <arguments>
            <argument>--enable-preview</argument>
        </arguments>
        </systemProperties>
    </configuration>
</plugin>

Still results in the following error:

An exception occured while executing the Java class. 
Preview features are not enabled for Main (class file version 59.65535). 
Try running with '--enable-preview'
1

There are 1 answers

1
kantianethics On BEST ANSWER

The problem is that exec:java runs in the same maven java process, which by default isn't started with --enable-preview.

You could instead switch to exec:exec, but one way to still use exec:java is to create a .mvn/jvm.config file containing --enable-preview. You can put this in your project's root directory and check into git. Or create a MAVEN_OPTS environment variable.

Reference: https://maven.apache.org/configure.html