问题 运行 在 surefire 和 failsafe 中使用启用的预览功能进行测试

Problem running tests with enabled preview features in surefire and failsafe

我正在尝试将项目迁移到 Java 12,--enable-preview

我在编译器设置中添加了--enable-preview

        <plugin>                                                            
            <artifactId>maven-compiler-plugin</artifactId>                  
            <version>3.8.0</version>                                        
            <configuration>                                                 
                <release>12</release>                          
                <compilerArgs>                                                                                  
                    <arg>--enable-preview</arg>                             
                </compilerArgs>                                                                      
            </configuration>                                                
        </plugin>                                                                                                                                         

并且还将其添加到 argLine 中以确保万无一失和故障安全:

<properties>                                                                                             
    <argLine>--enable-preview</argLine>                        
</properties> 

并执行 mvn clean verify 结果:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test (default-test) on project lombok-jdk10: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test failed: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/kirela/lombok/BarTest (class file version 56.65535). Try running with '--enable-preview' -> [Help 1]

我也试过直接将 argLine 添加到 surefire/failsafe 配置中,但结果是一样的。

我在这里错过了什么?

这是 surefire/failsafe 中的错误吗?

EDIT2:Surefire 和故障保护配置:

        <plugin>                                                            
            <groupId>org.apache.maven.plugins</groupId>                     
            <artifactId>maven-surefire-plugin</artifactId>                  
            <version>3.0.0-M3</version>                                     
            <configuration>                                                 
                <forkCount>2</forkCount>                                    
            </configuration>                                                
        </plugin>                                                           
        <plugin>                                                            
            <groupId>org.apache.maven.plugins</groupId>                     
            <artifactId>maven-failsafe-plugin</artifactId>                  
            <version>3.0.0-M3</version>                                     
            <executions>                                                    
                <execution>                                                 
                    <goals>                                                 
                        <goal>integration-test</goal>                       
                        <goal>verify</goal>                                 
                    </goals>                                                
                </execution>                                                
            </executions>                                                   
            <configuration>                                                 
                <forkCount>2</forkCount>                                    
            </configuration>                                                                                                                              
        </plugin> 

编辑3: 最小的工作示例在这里:https://github.com/krzyk/lombok-jdk10-example

该项目因 --enable-preview 而失败,但在我删除它时可以正常工作。

有两种解决方案:

--enable-preview 添加到 MAVEN_OPTS 环境变量。

Explanation by the maintainer of surefire:

The argLine does what it has to do without any issue. The plugin runs JUnit filter which finally selects relevant classes to run in one or multiple JVMs. So the JUnit engine runs twice. Once in plugin JVM, and second in the forked JVM.

Due to the classes are compiled with different major or minor version (in bytecode of *.class files) than the version of Java runtime supports in Maven, this JRE fails because Java in Maven does not understand the bytecode. So, it is curious that the same JVM (javac) produced two major versions depending on JVM option and java from the same JVM does not understand it been incompatible for itself. Although version in forked JVM is totally fine and understands the the classes compiled by javac because javac and forked JVM start with the same option --enable-preview. It is the same situation as if you compiled your sources with Java 12 by maven-compiler-plugin using the toolchain and run the whole Maven build with Java 11. So the classes would be compiled with higher version (in bytecode) than the JRE could understand in Maven process.

我们希望重新设计提供程序并在分叉的 JVM 内部执行过滤,但这是非常复杂的更改并且仍然存在疑问。

问题是我使用了 forkCount,看来 surefire 没有将参数传递给 fork 中的 JVM 运行。

从 surefire/failsafe 配置中删除 forkCount 参数

这当然会导致在单个 JVM 中进行测试 运行,因此如果您想使用 fork 加速测试,它现在不起作用。

这对我有用:

  • mvn clean install 有效(使用 junit 测试)
  • IDEA 将模块语言级别正确识别为 12 (Preview) - Switch expressions
  • IDEA 工作中的 junit 测试
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <release>12</release>
            <compilerArgs>
                <arg>--enable-preview</arg>
            </compilerArgs>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <argLine>--enable-preview</argLine>
        </configuration>
    </plugin>

环境:

  • Ubuntu 18.04.3 x64

  • IDEA 2019.2.1

  • Maven 3.6.0

  • jdk:

     IMPLEMENTOR="Oracle Corporation"
     JAVA_VERSION="12"
     JAVA_VERSION_DATE="2019-03-19"
    

ADDITION:类似地,此方法适用于 java 13.

ADDITION:类似地,此方法适用于 java 17.

为 surefire 和 failsafe maven 插件添加配置

<plugin>
   <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <argLine>--enable-preview</argLine>
    </configuration>
</plugin>