First option
Put your common "test" code in a separate module (in src/main/java
) and declare a dependency on this module with a test
scope in modules that need the common code:
<dependency>
<groupId>my.group.id</groupId>
<artifactId>testing-framework</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
That's a common practice, there is nothing wrong with that.
Second option
Use the Maven JAR Plugin and its jar:test-jar
goal to build a JAR of the test classes for the current project as part of the package
phase (jar:test-jar
binds itself by default on package
):
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
To use the attached test JAR that was created above, the recommended way is to 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>
See the discussion at thee bottom of Guide to using attached tests for why <type>test-jar</type>
should be preferred over <classifier>tests</classifier>
.