How do I build a jar file that contains its dependencies?
Author: Deron Eriksson
Description: This maven tutorial describes how to build a jar file that contains its dependencies using the maven-shade-plugin plugin.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)


Page: < 1 2

(Continued from page 1)

After the mavenSW command has completed, we can see that the target directory now contains a "mytest-1.0-SNAPSHOT.jar" regular jarW file and an "uber-mytest-1.0-SNAPSHOT.jar" uber jar file.

jar file and uber jar file have been generated

We can see that the "mytest-1.0-SNAPSHOT.jar" file contains the regular expected contents.

regular jar contains expected contents

If we examine the contents of the "uber-mytest-1.0-SNAPSHOT.jar" jar file, we can see that it actually contains the classes from the commons-lang dependency.

uber jar contains classes for commons-lang dependency

We would expect that executing the MyTest class in the mytest-1.0-SNAPSHOT.jar file without adding additional jar files to the classpathW would result in a NoClassDefFoundError. This is exactly what happens. The command is shown here:

C:\dev\workspace\mytest\target>java -cp mytest-1.0-SNAPSHOT.jar com.maventest.MyTest

We would also expect that executing the MyTest class in the uber-mytest-1.0-SNAPSHOT.jar file would work since it contains the commons-lang dependency classes. This is what happens. The command is shown here:

C:\dev\workspace\mytest\target>java -cp uber-mytest-1.0-SNAPSHOT.jar com.maventest.MyTest

The execution of these commands in a command prompt window is shown below.

Execution of commands at command prompt
Page: < 1 2