5

我有一个使用 JDK 12 预览功能的自定义 Maven 插件。我将插件设置--enable-preview编译为编译器 arg,即

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

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

4

2 回答 2

2

对我来说,我必须在我的构建目录中添加一个配置文件:

.mvn/jvm.config

包含:

--enable-preview

这将确保 Maven 将正确的参数传递给 JVM

于 2019-12-16T19:31:04.107 回答
1

你在你的pom中犯了一个错误。<compilerArgs>需要嵌套<arg>,如下所示:

        <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>
于 2021-10-20T12:02:14.097 回答