Over a million developers have joined DZone.

Sharing Test Classes Between Multiple Modules in a Multi-module Maven Project

Using Maven to effectively test software when test classes are not already packaged by Maven.

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs


Project A has a class generated in its test branch. Project B (or C, D, etc..) wants to utilize the class generated in Project A’s test scope.

The Problem

Typically classes in the test branch are not packaged by Maven, therefore, Project B has no means to find the class/resource in Project A.


Edit the package phase of the jar plugin in Project A so a jar file containing the compiled test classes can be made available to other modules/projects.

Addition to the build section in Project A:

      <id>Jar Package</id>
  <id>Jar Tests Package</id>

In this example, two jars are created. One will be the typical packaged product associated with the “jar” goal of the “package” phase (a jar file containing compiled source under “src/main/java”).  A second jar is constructed containing the compiled source under “src/test/java” this is associated with the “test-jar” goal of the p”package” phase.

Files Generated

  • ExampleJar-0.0.1-SNAPSHOT.jar

  • ExampleJar-0.0.1-SNAPSHOT-tests.jar

Project B will contain 2 references to the ExampleJar dependency. One for the compilation process and another for the “test” scope of Project B.

Note: if the only requirement is for test dependencies than the dependency that is under the default scope wouldn’t be needed.

Example of how to reference the “test-jar” in Project B:



Additional Points

Generally it’s not a good idea for test source bases to have dependencies. This scenario came to me while converting projects to Maven and mocks being shared amongst different projects.

Another way to solve this problem would be to place mocks classes and other test resources into a compiled dependency that is available to all modules.

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

java ,maven

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}