Guide to using attached tests

Many times you may want to reuse the tests that you have created for a project in another. For example if you have written foo-core and it contains test code in the ${basedir}/src/test/java it would be useful to package up those compiled tests in a JAR and deploy them for general reuse. To do this you would configure the maven-jar-plugin as follows:

<project>
  <build>
    <plugins>
     <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>
    </plugins>
  </build>
</project>

Installing the attached test JAR

In order to install the attached test JAR you simply use the standard install phase by executing the following command:

mvn install

Deploying the attached test JAR

In order to deploy the attached test JAR you simply use the standard deploy phase by executing the following command:

mvn deploy

Using the attached test JAR

In order to use the attached test JAR that was created above you simply specify a dependency on the main artifact with a specified type of test-jar:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.myco.app</groupId>
      <artifactId>foo</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>

Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).