我有一个自定义的Maven插件,它利用了JDK 12预览功能。我将插件设置 --enable-preview
编译为编译器参数,即
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<compilerArg>--enable-preview</compilerArg>
</compilerArgs>
</configuration>
</plugin>
当我想执行插件时,我会在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>
但这失败了:
Preview features are not enabled for MyPluginMojo. Try running with '--enable-preview'
如何在插件执行中启用预览功能?
对我来说,我必须在以下位置向我的构建目录添加一个配置文件:
.mvn/jvm.config
包含:
--enable-preview
这将确保Maven将正确的参数传递给JVM。
你在pom中犯了一个错误<代码>
<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>
对于JDK 17,这对我有用:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>