我们有一个 Java 模块项目集合(使用 JDK11)。
我们的集成测试有一个单独的项目。该项目需要能够访问主应用程序项目来运行其测试,但我们不想向主应用程序模块添加导出,因为只有在运行测试时才需要它。
解决方案是在我们的集成测试项目中使用compilerArgs添加导出:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<release>${java.version}</release>
<parameters>true</parameters>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>--add-exports</arg>
<arg>com.example.application/com.example.application=com.example.integration_tests</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
这适用于命令行mvn clean install
。但遗憾的是它不适用于 eclipse 。
无论出于何种原因,eclipse 都会忽略这些编译器参数并不断给出主应用程序类不可访问的错误。
有没有办法强制 Eclipse 使用这些编译器参数?或者这是 Eclipse 尚未涵盖的模块系统的某些方面?
最佳答案
--add-exports
编译器参数可以通过替换在 .classpath
文件中为 Maven 依赖项手动设置/strong>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
与
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="add-exports" value="com.example.application/com.example.application=com.example.integration_tests"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
不幸的是,如果选项 ,右键单击项目并选择 Maven > 更新项目... 会反转手动设置已选择从 pom.xml 更新项目配置(默认情况下)。
参见:Eclipse bug 543631 - Eclipse - Maven - JPMS (如果您想拥有此功能,请在此发表评论并投票)
要在更新项目后恢复手动设置,您可以将 .classpath
文件替换为更新项目之前的版本。
请考虑使用以下替代方案来替代使用 JPMS 但访问模块内部内容的单独项目的测试代码:
- 将测试代码放入
src/test/java/
中,而不是放入单独的项目中(访问内部表明这是单元测试而不是集成测试) - 在单独的(集成)测试项目中不要访问所需模块的内部内容
- 不要在单独(集成)测试项目中使用 JPMS
关于java - Eclipse maven编译器不支持编译器插件compilerArgs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55917386/