Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java9 Multi-Module Maven Project Test Dependencies

I have a multi-module maven project with three modules core, utils and test-utils

Core has the following dependencies definition

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

I have added Java 9 module-info.java definitions for all three modules and core's looks like this:

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

However I cannot figure out how to get core's test classes to be able to see the test-utils classes during test execution. When maven-surefire-plugin attempts the test run I get class not found.

If I add a requires my.project.testutils; to core's module-info.java:

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

Then at compile time I get an error that the my.project.testutils module can't be found (presumably because it's only brought in as a test dependency).

How does one work with test dependencies in a Java 9 modular world? For obvious reason's I don't want my main code to pull in test dependencies. Am I missing something?

like image 809
kiiadi Avatar asked Nov 06 '17 21:11

kiiadi


People also ask

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

How do I run a multi-module project in Maven?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

What is test dependency in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.

Can two modules depend on each other?

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.


1 Answers

With maven and java9, if your my.project.testutils is a test scope dependency, you don't need to explicitly include(requires) it in the module descriptor.


The test dependencies are taken care via the classpath itself. So you can simply remove the testutils and it would be patched by maven while executing tests.

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

Refer to the slide 30 pertaining to maven-compiler-plugin.

enter image description here

I would also suggest you take a look at Where should I put unit tests when migrating a Java 8 project to Jigsaw and this comment by Robert confirming on the implementation that maven follows.

Edit: Created a sample project drawing an analogy that the main module is same as your core, the dependency on guava is same as your utils and the junit dependency is same as your testutils.

like image 154
Naman Avatar answered Sep 22 '22 16:09

Naman