问题
We have a collection of Java module projects (using JDK11).
There is a separate project for our integration tests. This project needs to be able to access the main application project to run its tests, but we don't want to add an export to the main application module because it's only needed when we're running our tests.
The solution has been to add the export using compilerArgs in our integration test project:
<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>
This works for a command line mvn clean install
. But sadly it doesn't work for eclipse.
For whatever reason eclipse ignores these compilerArgs and keeps giving errors that the main application class is not accessible.
Is there some way to force eclipse to use these compilerArgs? Or is this some aspect of the module system that Eclipse hasn't gotten around to covering yet?
回答1:
The --add-exports
compiler argument can be set manually for Maven dependencies in the .classpath
file by replacing
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
with
<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>
Unfortunately, right-clicking the project and choosing Maven > Update Project... reverses the manual settings.
See: Eclipse bug 543631 - Eclipse - Maven - JPMS (please comment and vote there if you would like to have this feature)
To restore your manual settings after updating the project, you can replace the .classpath
file with its version before updating the project.
Please consider the following alternatives to a separate project for test code that uses JPMS but accesses internal stuff of a module:
- Put the test code into
src/test/java/
instead of into a separate project (accessing internals indicates that it is an unit test rather than an integration test) - In the separate (integration) test project do not access interal stuff of required module
- Do not use JPMS in the separate (integration) test project
来源:https://stackoverflow.com/questions/55917386/eclipse-maven-compiler-not-supporting-compiler-plugin-compilerargs