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=""  
            <!-- This creates the test-jar as an attachement -->
XML ElementDescription
<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.

            <!-- create a complete jar for testing in other environments -->
XML ElementDescription
<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

comments powered by Disqus