Let's say I have a Java project using Maven 3 and junit. There are src/main/java
and src/test/java
directories which contain main sources and test sources, respectively (everything is standard).
Now I want to migrate the project to Java 9. src/main/java
content represents Java 9 module; there is com/acme/project/module-info.java
looking approximately like this:
module com.acme.project {
require module1;
require module2;
...
}
What if test code needs module-info.java
of its own? For example, to add a dependence on some module that is only needed for tests, not for production code. In such a case, I have to put module-info.java
to src/test/java/com/acme/project/
giving the module a different name. This way Maven seems to treat main sources and test sources as different modules, so I have to export packages from the main module to the test module, and require packages in the test module, something like this:
main module (in src/main/java/com/acme/project
):
module prod.module {
exports com.acme.project to test.module;
}
test module (in src/test/java/com/acme/project
):
module test.module {
requires junit;
requires prod.module;
}
This produces
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project test-java9-modules-junit: Compilation failure: Compilation failure:
[ERROR] /home/rpuch/git/my/test-java9-modules-junit/src/test/java/com/acme/project/GreeterTest.java:[1,1] package exists in another module: prod.module
because one package is defined in two modules. So now I have to have different projects in main module and test module, which is not convenient.
I feel I follow wrong path, it all starts looking very ugly. How can I have module-info.java
of its own in test code, or how do I achieve the same effects (require
, etc) without it?
module-info
in the test are does from my point of view not make sense? Special requirement / achievement behind that? – Edmon