Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a Maven plugin with --enable-preview in POM

I have a custom Maven plugin which makes use of JDK 12 preview features. I compile the plugin setting --enable-preview as compiler arg, i.e.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerArgs>
            <compilerArg>--enable-preview</compilerArg>
        </compilerArgs>
    </configuration>
</plugin>

When I want to execute the plugin, I add the plugin like this in the POM:

<plugin>
    <groupId>my.group</groupId>
    <artifactId>my-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>my-goal</goal>
            </goals>
        </execution>
    </executions>
</plugin>

But this fails with:

Preview features are not enabled for MyPluginMojo. Try running with '--enable-preview'

How can I enable preview features in a plugin execution?

like image 936
Giovanni Lovato Avatar asked Oct 16 '22 11:10

Giovanni Lovato


2 Answers

For me, I had to add a config file to my build directory at:

.mvn/jvm.config

containing:

--enable-preview

This will make sure that Maven passes the correct parameters to JVM

like image 92
Randgalt Avatar answered Oct 19 '22 01:10

Randgalt


You made a mistake in your pom. <compilerArgs> takes nested <arg>, like so:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <compilerArgs>
                    <arg>--enable-preview</arg>
                 </compilerArgs>
            </configuration>
        </plugin>
like image 43
Aleksandr Dubinsky Avatar answered Oct 19 '22 02:10

Aleksandr Dubinsky