I’ve written a few utility classes for unit tests in one project and want to reuse these in the tests for another project. I’m too lazy to separate these out into a proper test-helper project and don’t want the tests to be in the main jar of the first project. The Maven2 docs aren’t very clear about how this can be achieved, but it is quite simple using test dependencies:

  1. In the first project, create a test-jar by adding the following to the pom.xml:
    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>
  2. Then in the second project add a dependency to that test jar by adding the tests classifier:
    <dependency> <groupId>mygroup</groupId> <artifactId>myartifact</artifactId> <version>0.1-SNAPSHOT</version> <classifier>tests</classifier> <scope>test</scope> </dependency>

You’ll need a relatively recent version of maven for this to work - 2.0.7 definitely works, I think 2.0.5 onward should work but haven’t tested with these.