Maven test-jar and test code reuse

In a multi module Maven project you might want to reuse test-related code (in src/test) across modules.

The recommended way is described here:
http://maven.apache.org/guides/mini/guide-attached-tests.html

You might want to exclude test classes from test-jar, to prevent that they run multiple times. Add this to your maven-jar-plugin definition (right after the <goals>..</goals> section) :

<configuration>
    <excludes>
        <exclude>**/*Test.class</exclude>
    </excludes>
</configuration>

This assumes that your test classes and only your test classes are named “…Test”.

More info on maven-jar-plugin configuration options for the test-jar goal:
http://maven.apache.org/plugins/maven-jar-plugin/test-jar-mojo.html

Leave a comment