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.
Technologies used :
- Ant 1.9.4
- Ant-Ivy 2.4
- logback 1.1.2
- 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
Download Source Code
Download It – AntDateUtils-fat-jar.zip (8 KB)
References
About Author
Comments
Subscribe
0 Comments