Build Maven Standalone Tests

Maven projects are not only used for building standalone or web applications they are also used for providing robust test frameworks using Java.

Running a standalone test is difficult, because the problem is that Maven packages the JAR file with only the main classes or test classes separately without the full dependencies.

The following describes how to create a standalone JAR file with test and main classes as well as all the dependencies.

1. Create a new XML file, e.g. test-jar-with-dependencies.xml , in the project path, e.g. src/main/assembly

2. Edit the test-jar-with-dependencies.xml file and copy the following:

<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>
<!-- This creates the test-jar as an attachement -->
<useProjectAttachments>true</useProjectAttachments>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
</assembly>
XML Element Description
<id> This appears as the suffix to the JAR file, e.g. myproject-SNAPSHOT-1.0-test-jar-with-dependencies.jar
<includeBaseDirectory> Includes the files in the project directory.
<outputDirectory> Outputs to the default output directory, usually the target directory.
<useProjectArtifact> Attach artifacts created during the build, i.e. the JAR files created by the maven-jar-plugin
<unpack> This property will unpack all module packages into the specified output directory, i.e. the packaged JAR files as well as the dependencies.

3. In the pom.xml add the following build command.

	<build>
<plugins>
<!-- 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>
</plugins>
</build>
XML Element Description
<goal>test-jar</goal> Instructs the maven-jar-plugin to package the test classes.
<descriptor> Instructs the maven-assembly-plugin to use the specified XML to configure the assembly of the dependencies.
<goal>single</goal> Places the assembled files into a single zip archive.

4. Perform a Maven Install, optionally using the skip test option, i.e. -DskipTests=true


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.