Creating a test jar in Maven

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>

Now run

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>
Advertisements

One thought on “Creating a test jar in Maven

  1. Thanks, for some reason this was harder to find in docs/a cookbook site than it should have been. You just saved me a bunch of time! ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s