我们有一组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会忽略这些compilerArgs并不断给出主应用程序类无法访问的错误。
有没有办法强制eclipse使用这些compilerArgs?或者这是Eclipse尚未解决的模块系统的某些方面?
可以通过替换--add-exports
文件中的Maven依赖项手动设置.classpath
编译器参数
<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> Update Project ...会反转手动设置。
请参阅:Eclipse bug 543631 - Eclipse - Maven - JPMS(如果您想拥有此功能,请在那里发表评论并投票)
要在更新项目后恢复手动设置,可以在更新项目之前将.classpath
文件替换为其版本。
对于使用JPMS但访问模块内部内容的测试代码,请考虑以下替代单独项目:
src/test/java/
而不是单独的项目(访问内部表明它是单元测试而不是集成测试)