Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing src/test classes between modules in a multi-module maven project

People also ask

What are the advantages of multi-module Maven project?

Multi modules can help you with re-use your code. It's one of the best benefits you'll feel in work. Imagine if you have 3 web projects with a security layer, You'll have to copy paste your code 3 times and trying connect it with each project. But what if you create a security module a project with a specific job.

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>


Your Consumer project depends upon your Data project, therefore we are happy that Data must be built prior to Consumer. As a result, using the techniques suggested in the comments, I would ensure your Data project contains all the test code that you wish to share and configure the POM to produce a test JAR:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Your Consumer project would then depend upon both the normal Data JAR artifact, plus the additional test-jar artifact, with test scope of course:

<dependency>
  <groupId>com.foo</groupId>
  <artifactId>data</artifactId>
  <version>1.0</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

I've used this approach on many occasions and it works well.


So the problem is that (some) tests in the data module depend on the SampleDataHelper class? You can move the SampleDataHelper class to src/main of the data-test module, if you at the same time move the tests (that depend on the specific class) to the src/test of the data-test module. Consequently, there would be no more circular dependencies.