26

I want to package my test package to jar file . How to execute generate test-jar from maven plugin Surefire.

2 Answers2

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>
Dharshana
  • 1,212
  • 1
  • 9
  • 18