Java 9 multi module Maven project test dependencies

I have a multi module Maven project, which contains three modules: core, utils and test utils

The core has the following dependency definitions

<dependency>
   <groupId>my.project</groupId>
   <artifactId>utils</artifactId>
</dependency>
<dependency>
   <groupId>my.project</groupId>
   <artifactId>test-utils</artifactId>
   <scope>test</scope>
</dependency>

I added Java 9 module info for all three modules Java definition. The core content is as follows:

module my.project.core {
   requires my.project.utils;
}

But I can't figure out how to make the core test class see the test utils class during test execution When Maven surefire plugin tried to run the test, I couldn't find the class

If I add one that needs my project. testutils; To the module info. Of the core java:

module my.project.core {
   requires my.project.utils;
   requires my.project.testutils; //test dependency
}

Then at compile time, I get an error and can't find my project. Testutils module (probably because it was introduced only as a test dependency)

How do I use test dependencies in the Java 9 modular world? For obvious reasons, I don't want my main code to introduce test dependencies Did I miss anything?

Solution

Use Maven and java9, if my project. If testutils is a test scope dependency, it does not need to be explicitly included (required) in the module descriptor

Handle test dependencies through the classpath itself Therefore, you can simply delete testutils and have Maven patch it when the test is executed

module my.project.core {
   requires my.project.utils;
}

See slide 30 maintaining to Maven compiler plugin

I also suggest you take a look at where should I put unit tests when migrating a Java 8 project to jigsaw and this comment. Robert confirms that Maven is implemented as follows

Edit: a sample project similar to the main module and the core is created. The dependency on guava is the same as your utils, and the dependency on JUnit is the same as testutils

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>