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)
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. However, we can create an 'uber' jar file using the maven-shade-plugin that will include the dependency classes inside of the uber jar file. As a result, the uber jar file is self-contained in that it contains all of the dependencies that it needs in order to execute classes in the uber jar file. I'll demonstrate this with a "mytest" project. Its pom.xml file contains a reference to the maven-shade-plugin. It attaches the 'shade' goal to the package phase of the mavenSW default lifecycle. In addition it specifies to give the generated uber jar a customized final name. pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maventest</groupId> <artifactId>mytest</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>mytest</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <finalName>uber-${artifactId}-${version}</finalName> </configuration> </plugin> </plugins> </build> </project> The "mytest" project contains a "MyTest.java" class, shown below. This class utilizes the StringUtils class in the commons-lang dependepency. MyTest.javapackage com.maventest; import org.apache.commons.lang.StringUtils; public class MyTest { public static void main(String[] args) { sayHello(); sayHelloAgain(); } public static void sayHello() { System.out.println("MyTest says hello!"); } public static void sayHelloAgain() { System.out.println(StringUtils.swapCase("MyTest says hello again!")); } } Now, I'll perform a "mvn clean package" to build the regular jar file and the uber jar file for the project. The console output is shown here. Console output for 'mvn clean package' on 'mytest' project[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building mytest [INFO] task-segment: [clean, package] [INFO] ------------------------------------------------------------------------ [INFO] [clean:clean] [INFO] Deleting directory C:\dev\workspace\mytest\target [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Compiling 2 source files to C:\dev\workspace\mytest\target\classes [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] Compiling 1 source file to C:\dev\workspace\mytest\target\test-classes [INFO] [surefire:test] [INFO] Surefire report directory: C:\dev\workspace\mytest\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.maventest.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [jar:jar] [INFO] Building jar: C:\dev\workspace\mytest\target\mytest-1.0-SNAPSHOT.jar [INFO] [shade:shade {execution: default}] [INFO] Including commons-lang:commons-lang:jar:2.3 in the shaded jar. [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing C:\dev\workspace\mytest\target\uber-mytest-1.0-SNAPSHOT.jar with C:\dev\workspace\mytest\target\mytest-1.0-SNAPSHOT-shaded.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 seconds [INFO] Finished at: Tue Feb 19 23:48:03 PST 2008 [INFO] Final Memory: 12M/22M [INFO] ------------------------------------------------------------------------ (Continued on page 2) |