猿问

在 POM 中使用 --enable-preview 执行 Maven 插件

我有一个自定义 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'

如何在插件执行中启用预览功能?


一只斗牛犬
浏览 333回答 3
3回答

森栏

对我来说,我必须将配置文件添加到我的构建目录中:.mvn/jvm.config包含:--enable-preview这将确保 Maven 将正确的参数传递给 JVM

白衣非少年

你在 pom 中犯了一个错误。<compilerArgs>采用嵌套<arg>,如下所示:&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.8.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>17</source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <target>17</target>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <compilerArgs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <arg>--enable-preview</arg>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</compilerArgs>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>

MYYA

对于 JDK 17,这对我有用:<plugin>&nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; <configuration>&nbsp; &nbsp; <source>17</source>&nbsp; &nbsp; <target>17</target>&nbsp; &nbsp; <compilerArgs>--enable-preview</compilerArgs>&nbsp; </configuration></plugin>
随时随地看视频慕课网APP

相关分类

Java
我要回答