简体   繁体   English

JUnit 测试似乎无法使用 --add-modules=jdk.incubator.vector 从 Maven 运行

[英]JUnit tests do not seem to get run with --add-modules=jdk.incubator.vector from Maven

I've added SIMD code to a Java application that uses Maven to build, and now I have to run it like this:我已经将 SIMD 代码添加到使用 Maven 构建的 Java 应用程序中,现在我必须像这样运行它:

mvn exec:java -Dexec.mainClass="com.path.to.app.MainClass" -Dexec.classpathScope=runtime -Dexec.systemProperties="-da --add-modules=jdk.incubator.vector"

I also created .mvn/jvm.config file with the following content:我还创建了包含以下内容的.mvn/jvm.config文件:

--add-modules=jdk.incubator.vector

Finally, in pom.xml I added the following under project/build/plugins :最后,在pom.xml中,我在project/build/plugins下添加了以下内容:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <compilerArgs>
                    <arg>--add-modules=jdk.incubator.vector</arg>
                </compilerArgs>
            </configuration>
        </plugin>

The application itself runs from Eclipse and from command-line with Maven as shown above.应用程序本身从 Eclipse 运行,并从命令行运行 Maven,如上所示。 However, if I run mvn install , the JUnit tests fail:但是,如果我运行mvn install , JUnit 测试会失败:

java.lang.NoClassDefFoundError: jdk/incubator/vector/Vector
...
Caused by: java.lang.ClassNotFoundException: jdk.incubator.vector.Vector
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

All the above steps were found on the inte.net.以上步骤都是在互联网上找到的。 I couldn't find how to fix JUnit. Please, suggest.我找不到如何修复 JUnit。请提出建议。

OpenJDK 19, Maven 3.8.6, Ubuntu 20.04, JUnit is enabled in pom.xml as follows: OpenJDK 19、Maven 3.8.6、Ubuntu 20.04、JUnit在pom.xml中启用如下:

<junit.version>4.13.2</junit.version>
...
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

As far as I can judge, the compilation is fine.据我判断,编译没问题。 That means the maven-compiler-plugin is properly configured, ie <arg>--add-modules=jdk.incubator.vector</arg> is picked up.这意味着maven-compiler-plugin已正确配置,即<arg>--add-modules=jdk.incubator.vector</arg>被选中。

The java.lang.NoClassDefFoundError happens at runtime, ie when the test is running. java.lang.NoClassDefFoundError发生在运行时,即测试运行时。 That obviously means --add-modules=jdk.incubator.vector is not picked up.这显然意味着--add-modules=jdk.incubator.vector没有被拾取。 I believe the reason is the maven-surefire-plugin not configured properly so that it doesn't propagate that value to the JVM that actually runs the test.我认为原因是maven-surefire-plugin配置不正确,因此它不会将该值传播到实际运行测试的 JVM。 By default, that JVM is different from the one running Maven itself.默认情况下,JVM 不同于运行 Maven 本身的那个。 This is managed by the forkCount parameter :这是由forkCount参数管理的:

The default setting is forkCount=1/reuseForks=true, which means that maven-surefire-plugin creates one new JVM process to execute all tests in one Maven module.默认设置为forkCount=1/reuseForks=true,这意味着maven-surefire-plugin会创建一个新的JVM进程来执行一个Maven模块中的所有测试。

So, if you want to keep it that way ( forkCount > 0) then you have to properly configure the maven-surefire-plugin .所以,如果你想保持这种状态( forkCount > 0),那么你必须正确配置maven-surefire-plugin Like in the following example:就像下面的例子:

<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M7</version> <!-- pick your own version -->
    <configuration>
        <forkCount>1</forkCount>
        <argLine>--add-modules=jdk.incubator.vector</argLine>
    </configuration>
  </plugin>
[...]
</plugins>

.mvn/jvm.config doesn't work in your case because this configuration is for Maven JVM as sort of alternative for MAVEN_OPTS : .mvn/jvm.config在您的情况下不起作用,因为此配置适用于 Maven JVM 作为MAVEN_OPTS的替代方案:

So no need anymore for MAVEN_OPTS... You don't need to use these options in MAVEN_OPTS or switch between different configurations.所以不再需要 MAVEN_OPTS ...您不需要在 MAVEN_OPTS 中使用这些选项或在不同配置之间切换。

About MAVEN_OPTS:关于 MAVEN_OPTS:

This variable contains parameters used to start up the JVM running Maven and can be used to supply additional options to it.此变量包含用于启动运行 Maven 的 JVM 的参数,并可用于为其提供附加选项。

So, if forking is disabled ( forkCount = 0) then it will work because there will be only one JVM for everything.因此,如果禁用分叉 ( forkCount = 0),那么它将起作用,因为所有内容都只有一个 JVM。 Otherwise, only the system properties get passed to a forked JVM :否则,只有系统属性会传递给分叉的 JVM

System property variables from the main maven process are passed to the forked process as well.来自主进程 maven 的系统属性变量也被传递给分叉进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM