Create a fat Jar file Maven Assembly Plugin

摘要: In this tutorial, we will show you how to create a fat/uber jar with Maven Assembly Plugin. Which means create a Jar together with its dependency Jars into a single executable Jar file.

In this tutorial, we will show you how to create a fat/uber jar with Maven Assembly Plugin. Which means create a Jar together with its dependency Jars into a single executable Jar file.

Note
Maven assembly plugin is not good in producing fat/uber jar, it may cause name conflict issue, it is better to use other Maven plugins like :

  1. Maven shade plugin solve this by using technique like class relocating.
  2. Maven one-jar plugin, add the dependency jar file directly into project jar, and loads it with custom class loader.

1. Review a Java project

Previous Java project (dateutils) will be reused, see following folder structure

Note
This project has a single dependency – joda-time.jar

2. Pom.xml

Read below comment for self-explanatory.

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.mkyong.core.utils</groupId>
	<artifactId>dateUtils</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>dateUtils</name>
	<url>http://maven.apache.org</url>
	<properties>
		<jdk.version>1.7</jdk.version>
		<jodatime.version>2.5</jodatime.version>
		<junit.version>4.11</junit.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>${jodatime.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>dateutils</finalName>
		<plugins>
			<!-- download source code in Eclipse, best practice -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>false</downloadJavadocs>
				</configuration>
			</plugin>
			<!-- Set a compiler level -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
				</configuration>
			</plugin>
			<!-- Maven Assembly Plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.4.1</version>
				<configuration>
					<!-- get all project dependencies -->
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<!-- MainClass in mainfest make a executable jar -->
					<archive>
					  <manifest>
						<mainClass>com.mkyong.core.utils.App</mainClass>
					  </manifest>
					</archive>
				</configuration>
				<executions>
				  <execution>
					<id>make-assembly</id>
                                        <!-- bind to the packaging phase -->
					<phase>package</phase> 
					<goals>
						<goal>single</goal>
					</goals>
				  </execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

3. Package It

Above “Maven Assembly Plugin” is bind to the Maven’s packaging phase, to produces the final Jar, just package it :

$ mvn package
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ dateUtils ---
[INFO] Building jar: /Users/mkyong/dateUtils/target/dateutils.jar
[INFO] 
[INFO] --- maven-assembly-plugin:2.4.1:single (make-assembly) @ dateUtils ---
[INFO] Building jar: /Users/mkyong/dateUtils/target/dateutils-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.325s
[INFO] Finished at: Tue Oct 21 13:44:41 MYT 2014
[INFO] Final Memory: 17M/42M
[INFO] ------------------------------------------------------------------------

Two jar files will be created in the target folder.

  1. dateutils.jar – Only your project classes
  2. dateutils-jar-with-dependencies.jar – Project and dependency classes in a single jar.

4. Review It

List out the content of dateutils-jar-with-dependencies.jar

$ jar tf target/dateutils-jar-with-dependencies.jar 
META-INF/
META-INF/MANIFEST.MF
org/
org/joda/
org/joda/time/
org/joda/time/base/
org/joda/time/chrono/
org/joda/time/tz/ZoneInfoCompiler$DateTimeOfYear.class
org/joda/time/tz/ZoneInfoCompiler$Rule.class
org/joda/time/tz/ZoneInfoCompiler$RuleSet.class
org/joda/time/tz/ZoneInfoCompiler$Zone.class
org/joda/time/tz/ZoneInfoCompiler.class
org/joda/time/tz/ZoneInfoProvider.class
org/joda/time/UTCDateTimeZone.class
org/joda/time/Weeks.class
org/joda/time/YearMonth$Property.class
org/joda/time/YearMonth.class
org/joda/time/YearMonthDay$Property.class
org/joda/time/YearMonthDay.class
org/joda/time/Years.class
META-INF/maven/
META-INF/maven/joda-time/
META-INF/maven/joda-time/joda-time/
META-INF/maven/joda-time/joda-time/pom.xml
META-INF/maven/joda-time/joda-time/pom.properties
com/
com/mkyong/
com/mkyong/core/
com/mkyong/core/utils/
com/mkyong/core/utils/App.class
META-INF/maven/com.mkyong.core.utils/
META-INF/maven/com.mkyong.core.utils/dateUtils/
META-INF/maven/com.mkyong.core.utils/dateUtils/pom.xml
META-INF/maven/com.mkyong.core.utils/dateUtils/pom.properties
MANIFEST.MF
Manifest-Version: 1.0
Built-By: mkyong
Build-Jdk: 1.7.0_05
Created-By: Apache Maven 3.1.1
Main-Class: com.mkyong.core.utils.App
Archiver-Version: Plexus Archiver

Run it

$ java -jar target/dateutils-jar-with-dependencies.jar 
2014-10-21

References

  1. Apache Maven Assembly Plugin
  2. Create A Fat Jar File – Maven One-JAR Example
  3. Maven shade plugin
  4. How to create a jar file with Maven

上一篇: Ant How to create a Java Project
下一篇: Maven Exclude log4j.properties in Jar file
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号