I am currently testing to migrate a Java 8 application to Java 9 / Jigsaw, using jdk-9+149.
The project has been laid out in standard Maven directory layout, i.e. ha
As already suggested here, using the updated version of the maven-compiler-plugin:3.7.0 shall help you fix this:-
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
Where in you can place your unit tests under the same directory as it used to be prior to modularisation. Here is a sample project from me which is based out of JDK9 and Maven3+. The project follows the directory structure as depicted in the screenshot:-
And post this all you need to do is execute tests using the command:
mvn test
from the root directory of the project to find the tests would be executed as usual.
Maven support for Java 9 in general and tests in particular is still under development - many things work but others might not. Without seeing the stack trace for the NPE, it is of course speculation, but I assume you ran into this error.
More generally, the question of how exactly unit tests will work with Jigsaw is still being discussed - even on the Jigsaw mailing list.
Here's my opinion on the matter:
As you note, putting tests into a separate module would mean that only public types in exported packages would be testable, which is definitely not enough. There could be workarounds but those require to either edit the module declaration (source code; module-info.java
) or descriptor (byte code, module-info.class
) on the fly or add a ton of --add-exports
command line flags to the javac
and java
commands compiling and running the tests. None of these sound particularly fun, especially if you want to do it by hand.
Moving tests into the source tree is a bad idea as well for obvious reasons, not least among them that creating a JAR without tests would then require a lot of fiddling.
Another option is to use the --patch-module
option that allows to add class
-files or the content of a JAR to an existing module. This way the testCompile
step could create a JAR containing the source and test files. Unfortunately, unless it manipulates the module declaration/description as described above, the resulting JAR can not be executed without adding reads edges with java --add-reads
for the test dependencies. Still, better than above.
As a kind of last resort, there are ways to have the production JAR be treated like a regular JAR instead of as a module. The easiest would probably be to dump it on the class path together with the test JAR. This way everything works as in Java <9. Unfortunately this will break code that uses features under the assumption that it is inside a named module (e.g. certain kinds of interactions with the reflection API).
Personally, I consider 3. to be the best of the above options. It would require no changes in project layout and only comparatively minor additions to javac
and java
commands.
You must at least use maven-compiler-plugin 3.6.0 to have jigsaw support. However, since build +148 the binary structure of a class file has changed, so the plugin cannot extract the modulename as before (the modulename is required to be able to compile the tests). I'm working on a fix for that, but I probably depend on a new version of ASM.
UPDATE: maven-compiler-plugin-3.6.1 has been released, so support for jigsaw is restored.