Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the test jar include dependencies in Maven?

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

like image 813
Frank Avatar asked Aug 09 '11 18:08

Frank


People also ask

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>

Does jar file include dependencies?

Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.

How do I check the dependencies of a jar file?

jar file. Use the -verbose:class option to find class-level dependencies or use the -v or -verbose option to include dependencies from the same JAR file.

What is test dependency in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.


2 Answers

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>
like image 76
ericschwarzkopf Avatar answered Sep 20 '22 16:09

ericschwarzkopf


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.

like image 39
Michael-O Avatar answered Sep 21 '22 16:09

Michael-O