I want to package my test package to jar file . How to execute generate test-jar from maven plugin Surefire.
Asked
Active
Viewed 2.8k times
2 Answers
41
By using the following configuration you can create a jar from your tests:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
To use such kind of artifact:
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<type>test-jar</type>
<version>version</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

khmarbaise
- 92,914
- 28
- 189
- 235
-
@khmarbaise Where do it use dependency ? Is it in the same pom.xml? – fjjiaboming Jun 08 '16 at 03:13
-
No. The first code block is for the application that should generate a JAR file with test-scoped classes. The second code block is for application that want to use the generated JAR file. – Manuel Dec 03 '20 at 12:35
-
I think, we should say what "artifactId-tests.jar" will be generated only if you run mvn with phase included test phase. – pe4enko Dec 25 '22 at 20:30
24
You can add following entries to your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Sergey Vyacheslavovich Brunov
- 17,291
- 7
- 48
- 81

Dharshana
- 1,212
- 1
- 9
- 18