I had an integration test utilty class in one project that I needed to use in another project. The class was located in src/test/java. Follow the steps below to package up these test classes into a jar for use as a dependency in another project.
1. Add the following plugin to the project with the test classes you want to package:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin>
mvn clean package install
This will create a *SNAPSHOT-tests.jar in your repository. Make sure you don’t have skipTest=true otherwise the test jar will not be created.
2. From the project that needs to reference the contents of the test jar, add the following:
<dependency> <groupId>com.myco.app</groupId> <artifactId>foo</artifactId> <version>1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency>