15

I have a project with src/main/java and src/test/java structure, and I managed to use maven-jar-plugin to build a jar of the test branch. However, I want to package the test jar so that all the dependencies are resolved. Is there a way I can tell maven-jar-plugin to include the dependencies??

Thanks!

Frank

Frank
  • 4,341
  • 8
  • 41
  • 57
  • Your question is unclear. Do you want to include all test dependencies with the test jar or do you want then to be correctly resolved as test deps? – Michael-O Aug 09 '11 at 18:41
  • I want to add the dependencies to the test jar, so that I can call a class in the test jar, and not need any other jar on the classpath. – Frank Aug 09 '11 at 19:14
  • The [maven-assembly-plugin](http://maven.apache.org/plugins/maven-assembly-plugin/) should provide a way to achieve what you want. – Nicola Musatti Aug 09 '11 at 19:47
  • I'm trying with the assembly-plugin, but it doesn't let me have 2 assemblies, one without the tests, and one with the tests. I'm very new at Maven, and I find it very confusing. – Frank Aug 09 '11 at 19:55

6 Answers6

43

I had a similar problem with integration tests I need to run on Hadoop. Our integration tests are located in the test folder of a separate integration test module, so what is required is a test-jar-with-dependencies to make our life easier.

I'm using the assembly plugin as mentioned by Michael-O. My assembly descriptor is located in src/main/assembly/test-jar-with-dependencies.xml and is a modification of the standard jar-with-dependencies descriptor that's part of the plugin:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>test-jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <!-- we're creating the test-jar as an attachement -->
            <useProjectAttachments>true</useProjectAttachments>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
</assembly>

This assembly relies on the test-jar being created as part of the module build. So I added the following to the module's pom.xml:

<!-- create a complete jar for testing in other environments -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>                    
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/test-jar-with-dependencies.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
ericschwarzkopf
  • 651
  • 5
  • 5
  • This should be the correct answer. In addition, you can use this to build a main jar assembly by adding the following below the tag: jar-with-dependencies. Then the assembly plugin will build both. – Locutus May 18 '20 at 16:43
2

You can do this: Create a jar assembly with the assembly plugin, have the dependencies unpacked, pack a new test jar and attach it to the reactor. You're done.

The descriptor for the packaging could look like this.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • That didn't quite work. The jar has about the same size as before, and I still can't execute its main class, with an exception NoClassDefFoundError. I looked in the tar, and indeed it does not contain the class in question. Any idea? – Frank Aug 09 '11 at 19:41
  • I have to learn what are "packing" and "reactors". I started working Maven very, very recently :-) Thanks for your help. – Frank Aug 09 '11 at 20:01
0
    <dependency>
        <groupId>me.wener.xxx</groupId>
        <artifactId>xxx-core</artifactId>
        <version>${xxx.version}</version>
        <type>test-jar</type>
        <!-- <scope>test</scope> -->
    </dependency>

I use this to include the test jar.the important line is <type>test-jar</type>.I am not sure this is what you need.

3 years ago, but may help others.At least, it helped me. :-)

wener
  • 7,191
  • 6
  • 54
  • 78
0

to include a test-jar dependency in your assembly specify the include filter of the assembly debendencySet as bellow:

...
<dependencySet>
    <outputDirectory>/</outputDirectory>
    <includes>
        <include>*:jar:*</include>
        <include>*:test-jar:*</include>
    </includes>
</dependencySet>
...
shruti1810
  • 3,920
  • 2
  • 16
  • 28
0

The following worked for Maven 3

POM.XML

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
    </executions>
</plugin>

ASSEMBLY FILE

<dependencySet>
    <outputDirectory>demo/test-lib</outputDirectory>
    <includes>
        <!--test only dependencies (like netty)-->     
        <include>io.netty:netty-all</include>
        <!-- the actual test jar-->
        <include>${project.groupId}:${project.artifactId}:test-jar</include>
    </includes>
    <useProjectAttachments>true</useProjectAttachments>
    <scope>test</scope>
</dependencySet>
Mike Precup
  • 4,148
  • 21
  • 41
qwerty
  • 3,801
  • 2
  • 28
  • 43
0

In a similar situation I ended up moving my test code to a separate jar and made it depend on the original one. You can use an aggregator project to ensure that tests are run when you build the main jar.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • The actual situation is that I need to pass the jar to Hadoop so that unit tests that can only run in Hadoop are run with hadoop jar myMainClass. It's currently not finding in the jar classes that are exercised by the unit tests... – Frank Aug 09 '11 at 19:43