Maven Surefire插件未使用--enable-preview模式

5

这是我的 pom.xml 文件:

...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>13</source>
                <target>13</target>
                <release>13</release>
                <compilerArgs>
                    --enable-preview
                </compilerArgs>
            </configuration>
        </plugin>
...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <reuseForks>false</reuseForks>
                <argLine>--enable-preview</argLine>
            </configuration>
        </plugin>

问题在于构建过程正常,但当测试被启动时,会出现以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project foo-project: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test failed: java.lang.UnsupportedClassVersionError: Preview features are not enabled for it/project/MyTest (class file version 57.65535). Try running with '--enable-preview' -> [Help 1]
我应该在pom.xml中插入什么内容以使用启用预览模式来执行测试?
谢谢。

1
看起来类似于:https://dev59.com/cFMI5IYBdhLWcg3wy-xf#55311987 - Jorn Vernee
pom.xml文件中的surefire版本是2.22.2,但日志中的版本是3.0.0-M4,这不奇怪吗? - Arthur Noseda
1个回答

1

我遇到了类似的问题。

  • Maven版本:3.8.1
  • Surefire版本:2.22.2
  • JK:13

我使用了<reuseForks>true</reuseForks>,它正常工作了。

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <release>${java.version}</release>
                <showWarnings>true</showWarnings>
                <compilerArgs>
                    <compilerArg>-Xlint:unchecked,deprecation</compilerArg>
                    <compilerArg>--enable-preview</compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.plugin.version}</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <reuseForks>true</reuseForks>
                <argLine>--enable-preview</argLine>
            </configuration>
        </plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接