Ant Create a fat jar file

摘要: In this tutorial, we will show you how to use Ant build script to create a big far / uber Jar file, which mean include the entire project external dependencies into a single jar file.

In this tutorial, we will show you how to use Ant build script to create a big far / uber Jar file, which mean include the entire project external dependencies into a single jar file.

Technologies used :

  1. Ant 1.9.4
  2. Ant-Ivy 2.4
  3. logback 1.1.2
  4. joda-time 2.5

1. Create a Fat Jar

Previous Ant + External libraries Java project will be reused. To create a fat jar, use “Jar” task and zipfileset function.

build.xml
	<!-- Group all dependencies into a big dependency-all.jar -->
	<target name="copy-dependencies">
		<mkdir dir="${dist.lib.dir}" />
		<jar jarfile="${dist.lib.dir}/dependencies-all.jar">
			<zipgroupfileset dir="${lib.dir}">
				<include name="**/*.jar" />
			</zipgroupfileset>
		</jar>
	</target>
	<!-- jar it, extract above dependency-all.jar and zip it with project files -->
	<target name="jar" depends="compile, copy-dependencies" 
                description="package, output to JAR">
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${dist.lib.dir}" />
		<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
			<manifest>
				<attribute name="Main-Class" value="${main-class}" />
			</manifest>
			<zipfileset src="${dist.lib.dir}/dependencies-all.jar" 
                                excludes="META-INF/*.SF" />
		</jar>
	</target>

2. build.xml

Review the complete build.xml example.

build.xml
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="dateUtilsProject" default="main" basedir=".">
	<description>
		Create a Java Project (JAR) with Ant build script
	</description>
	<property name="projectName" value="DateUtils" />
	<property name="src.dir" location="src" />
	<property name="build.dir" location="bin" />
	<property name="dist.dir" location="dist" />
	<property name="dist.lib.dir" location="dist/lib" />
	<property name="lib.dir" value="lib" />
	<property name="main-class" value="com.mkyong.core.utils.DateUtils" />
	<!-- ivy start -->
	<!-- ivy to get dependencies and copy to project lib folder automatically -->
	<target name="resolve" description="retrieve dependencies with ivy">
		<ivy:retrieve />
	</target>
	<!-- install ivy -->
	<target name="ivy" description="Install ivy">
		<mkdir dir="${user.home}/.ant/lib" />
		<get dest="${user.home}/.ant/lib/ivy.jar" 
                    src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0-rc1/ivy-2.4.0-rc1.jar" />
	</target>
	<!-- ivy end -->
	<target name="init">
		<mkdir dir="${build.dir}" />
	</target>
	<path id="classpath">
		<fileset dir="${basedir}/">
			<include name="${lib.dir}/*.jar" />
		</fileset>
	</path>
	<!-- Need classpath to run this -->
	<target name="compile" depends="init" description="compile the source ">
		<javac includeantruntime="false" srcdir="${src.dir}" 
                       destdir="${build.dir}" classpathref="classpath" />
	</target>
	<!-- Group all dependencies into a big dependency-all.jar -->
	<target name="copy-dependencies">
		<mkdir dir="${dist.lib.dir}" />
		<jar jarfile="${dist.lib.dir}/dependencies-all.jar">
			<zipgroupfileset dir="${lib.dir}">
				<include name="**/*.jar" />
			</zipgroupfileset>
		</jar>
	</target>
	<!-- jar it, extract above dependency-all.jar and zip it with project files -->
	<target name="jar" depends="compile, copy-dependencies" 
                description="package, output to JAR">
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${dist.lib.dir}" />
		<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
			<manifest>
				<attribute name="Main-Class" value="${main-class}" />
			</manifest>
			<zipfileset src="${dist.lib.dir}/dependencies-all.jar" 
                                excludes="META-INF/*.SF" />
		</jar>
	</target>
	<target name="clean" description="clean up">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>
	<!-- Default, run this -->
	<target name="main" depends="clean, compile, jar" />
</project>

3. Run and Test

3.1 Install Ivy, skip this if you have installed it.

$ ant ivy

3.2 Ask Ivy to download the project dependencies.

$ ant resolve

3.3. Clean, compile and create the final fat jar.

$ ant
Buildfile: /Users/mkyong/Documents/workspace/AntDateUtils/build.xml
clean:
init:
    [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/bin
compile:
    [javac] Compiling 1 source file to /Users/mkyong/Documents/workspace/AntDateUtils/bin
copy-dependencies:
    [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/dist/lib
      [jar] Building jar: /Users/mkyong/Documents/workspace/AntDateUtils/dist/lib/dependencies-all.jar
jar:
      [jar] Building jar: /Users/mkyong/Documents/workspace/AntDateUtils/dist/DateUtils.jar
main:
BUILD SUCCESSFUL
Total time: 9 seconds

3.4 Run it

$ java -jar dist/DateUtils.jar 
12:11:23.732 [main] DEBUG com.mkyong.core.utils.DateUtils - [MAIN] Current Date : 2014-12-24
2014-12-24

References

  1. Ant Jar task
  2. Ant type zipfileset
  3. Ant – How to create a Java Project
  4. Ant – How To Create A Jar File with external libraries

上一篇: Ant Spring MVC and WAR file Example
下一篇: Java AWT Layouts
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

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

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

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