How do I create a jar file using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to use the jar task to build a jar file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1

Page:    1 2 3 >

Ant's jarW task makes it very easy to build a jar file for a project. In this tutorial I'll demonstrate this using the following project:

team-cakes-utilities project in Eclipse Navigator

This project features a servletW, and since I run TomcatSW locally and deploy to Tomcat, I can use the Tomcat jar files in my build classpathW to compile the servlet. The servlet also uses log4j, and the log4j jar file is contained in the lib directory in the project's root directory. The AntSW script created in this tutorial utilizes a 'jar-project-classpath' path element, which consists of all the jar files in the lib directory and the Tomcat jars.

	<path id="jar-project-classpath">
		<fileset dir="lib" includes="*.jar" />
		<fileset dir="${tomcat-home}/bin" includes="*.jar" />
		<fileset dir="${tomcat-home}/common/lib" includes="*.jar" />
		<fileset dir="${tomcat-home}/server/lib" includes="*.jar" />

Normally EclipseSW compiles this project's JavaSW classes and places them in the bin directory. However, to be thorough, I am going to delete and recreate this directory, and then copy over any non-Java files from the src to the bin directory. After that, I shall compile the javaSW files in the src directory and place the resulting class files in the bin directory. The 'compile-jar-classes' target calls the 'clean' and 'copy-non-java-files' targets before it executes.

	<target name="clean">
		<delete dir="bin" />
		<mkdir dir="bin" />

	<target name="copy-non-java-files">
		<copy todir="bin" includeemptydirs="false">
			<fileset dir="src" excludes="**/*.java" />

	<target name="compile-jar-classes" depends="clean,copy-non-java-files">
		<javac srcdir="src" destdir="bin" classpathref="jar-project-classpath" />

The targets above are responsible for compiling our java classes and copying any other needed files over to bin. Now, we need to package the classes in bin into a jar file, which we'll put in a build directory. The 'clean-jar' target deletes and recreates the build directory. The 'jar' target is the target that actually builds the jar file using the jar task. We specify using the 'basedir' attribute that we want to jar up the files in the bin directory, and we use the 'destfile' attribute to specify the location of the generated jar file and the name of the jar file. It contains a 'manifest' element, which we use to specify in the included META-INF/MANIFEST.MF file who build the warW file, the date that the war file was built, and the time that the war file was built.

	<target name="clean-jar">
		<delete dir="build" />
		<mkdir dir="build" />

	<target name="jar">
		<jar basedir="bin" destfile="build/${project-name}.jar">
				<attribute name="Built-By" value="${builder}" />
				<attribute name="Built-On" value="${build-info.current-date}" />
				<attribute name="Built-At" value="${build-info.current-time}" />

The 'build-jar' target uses the 'antcall' task to call the 'compile-jar-classes', 'clean-jar', and 'jar' tasks.

	<target name="build-jar">
		<antcall target="compile-jar-classes" />
		<antcall target="clean-jar" />
		<antcall target="jar" />

(Continued on page 2)

Page:    1 2 3 >