将模块导出添加到mvn测试执行运行时



在测试过程中出现此错误:

class javax.crypto.JceSecurity (in unnamed module @0x45da40ad) cannot access class jdk.internal.util.StaticProperty (in module java.base) because module java.base does not export jdk.internal.util to unnamed module @0x45da40ad

我尝试在根目录下创建jvm.config,在pom.xml旁边创建

--add-modules ALL-SYSTEM
--add-opens java.base/jdk.internal.util=ALL-UNNAMED
--illegal-access=permit

这不会改变任何事情。因此,我尝试将maven编译器插件配置为:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>--add-modules</arg>
<arg>ALL-SYSTEM</arg>
<arg>--add-opens</arg>
<arg>java.base/jdk.internal.util=ALL-UNNAMED</arg>
</compilerArgs>
<argLine>
--add-modules ALL-SYSTEM
--add-opens java.base/jdk.internal.util=ALL-UNNAMED
--illegal-access=permit
</argLine>
<source>${java.compiler.source}</source>
<target>${java.compiler.target}</target>
</configuration>
</plugin>

为了记录在案,我甚至试过了:

<argLine>
--add-modules ALL-SYSTEM
--add-opens java.base/jdk.internal.util=ALL-UNNAMED
--illegal-access=permit
</argLine>

什么都没有。然后我尝试了像这样的surefire插件:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<forkCount>0</forkCount>
<argLine>
--add-modules ALL-SYSTEM
--add-opens java.base/jdk.internal.util=ALL-UNNAMED
--illegal-access=permit
</argLine>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>

花了两天时间做这件事,结果惨败。请帮忙。使用OpenJdk11

我与surefire 3.0.0-M3和-M5进行了交叉检查,使用add-opens Oracle的迁移指南配置surefire应该完全足够

此外,您的格式是绝对正确的:--add-opens <module>/<package>=ALL-UNNAMED。与--illegal-access=permit结合使用应该可以正常工作。

我只看到一个选项:从你的opens参数中删除=ALL-UNNAMED,这将使VM/Surefire崩溃,并证明你的设置是活动的。

除此之外,您的测试类应该由您喜欢的运行程序通过反射来调用(测试方法包private/不带public修饰符(。这需要对测试类使用相同的open声明/导致相同的问题——除非Maven项目本身不是一个模块。

也许可以在你的问题中澄清这一点。

许多教程和指南都发布了以下解决方法

<forkCount>0</forkCount>

请不要使用它!

surefire子流程是必须的,尤其是在JPMS中。

请不要使用forkCount=0的解决方法,而是报告Apache JIRA中的错误,并与开源开发人员沟通。

感谢其他答案,它帮助我更深入地挖掘。我设法通过pom 中的以下更改来解决它

<properties>
<!-- Must be in pom's properties section. <sonar.jacoco.reportPaths>target/coverage.exec</sonar.jacoco.reportPaths> -->
<jacoco.version>0.7.7.201606060606</jacoco.version>
<!--         Used by surefire plugin run tests with openjdk11 -->
<argLine>--add-modules java.base --add-opens java.base/jdk.internal.util=ALL-UNNAMED --illegal-access=permit</argLine>
</properties>
.........
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.compiler.source}</source>
<target>${java.compiler.target}</target>
</configuration>
</plugin>
........
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>

基本上,我不得不把argline放在属性中。编译器似乎不需要它,因为它并没有从那里获取它。但万无一失的是,它正在从maven的属性中读取隐语。