Maven - test jar

About

A test jar is a jar file used to share test code between module.

Steps

In two steps:

Creation of the test jar

  • Creating the test jar that is shared with the other modules (ie all test class will be package in a test jar)
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<executions>
		<execution>
			<goals>
				<goal>test-jar</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Adding it as a dependency

  • Then in the module that uses this class, add it as dependency
<dependency>
    <groupId>group-id</groupId>
    <artifactId>module1</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <scope>test</scope>
    <type>test-jar</type>
</dependency>

Using <classifier>tests</classifier> instead of <type>test-jar</type> will not 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.

Documentation / Reference


Powered by ComboStrap