Basic Ant scripts

What’s an Ant script? Do I need bug spray?

Ant is a scripting tool commonly used to build, compile and deploy projects. This is in no way an all encompassing inventory of what Ant can do. It is extensible and its instructions are expressed in an xml format whose nodes comprise a framework of abilities designed to make menial tasks automated.

From a developer’s perspective, the most basic Ant tasks are the compile, package and copy tasks. All java projects must do these three things many, many, many times during a development cycle. Its very boring and tedious if you do it by hand through the command prompt. First you’d have to type in a bunch of commands like “javac [ options ] [ sourcefiles ] [ @argfiles ]” detailing all the class paths you want to use, all the source files, and all then the other supporting parameters you need to enter to get it to compile your program correctly. If you’re only writing one class, its probably not that bad. But when you have hundreds of classes, multiple projects and dependencies, and a slew of directories to configure and lay out for compiling, it quickly becomes ridiculous. In fact, I would claim that it becomes ridonkulous.

Ant lets you define tasks that break up these chores into a series of chained events. An Ant task is broken up into what are called “targets”. Each target is meant to perform one task, or unit of work. If we break up the compilation/deploy process it could look something like this:

  1. clean out the scrub/temporary directory
  2. compile all the java files into class files
  3. package up all the class files into a jar file, or some kind of deployable artifact
  4. copy the new jar file to the deploy directory

We can define each one of these steps with an Ant task. This is a good thing because it allows us to chain them like stairs, one task leading into the next. If any one of the tasks fail, the script would fail and Ant would tell us where the problem happened (with line number, and the exact problem or exception).

Here are what these steps might look like:

1) Clean up the build directories

<!-- *************** -->
<!-- * Preparation * -->
<!-- *************** -->

<target name="prepare" depends="clean">
	<mkdir dir="${build.dir}"/>
	<mkdir dir="${build.dir}/jars"/>
	<mkdir dir="${build.dir}/openscope"/>

Here, the “clean” depends attribute references a previous ant target that deletes all these scrub directories. This “prepare” target creates the scrub directories we’re going to use in our build. mkdir creates a directory.

2) Compile all the java files into class file

<!-- *************** -->
<!-- * Compilation * -->
<!-- *************** -->	

<target name="compile" depends="prepare">
	<javac destdir="${build.dir}/openscope"
		<src path="${src.dir}"/>
	<classpath refid="build.classpath"/>

	<copy todir="${build.dir}/openscope">
		<fileset dir="${props.dir}">
			<include name="*.properties"/>

Ant compiles things with the “javac” target. It takes a few parameters and optional flags we can use to customize the actual compile command. This task also copies any properties files into the scrub directory.

3) Package up all the class files into a jar file, or some kind of deployable artifact

<!-- *************** -->
<!-- *   Building  * -->
<!-- *************** -->

<!-- Package the logic module -->
<target name="package-logic" depends="compile">
	<jar jarfile="${build.dir}/jars/${logic.file}">
		<fileset dir="${build.dir}/openscope">
			<include name="com/openscope/**"/>
			<include name="*.properties"/>

		<metainf dir="${resources.dir}">
			<include name="persistence.xml"/>

	<copy todir="${basedir}/deploy/${ear.file}">
		<fileset dir="${build.dir}/jars">
			<include name="${logic.file}"/>

<target name="build-war" depends="package-logic">

	<jar jarfile="${build.dir}/jars/${war.file}"


The “jar” task jars up the contents of a directory. We can add files to the META-INF directory with a file include directive under the “metainf” task as part of the “jar” task.

4) Copy the new jar file to the deploy directory

<!-- **************** -->
<!-- * Make the Ear * -->
<!-- **************** -->

<!-- Creates the application ear file. -->
<target name="assemble-app" depends="package-logic,build-war">

	<ear destfile="${build.dir}/${ear.file}"

		<attribute name="Built-By"
			value="Openscope Networks"/>
		<attribute name="Implementation-Vendor"
			value="Openscope Networks"/>
		<attribute name="Implementation-Title"
		<attribute name="Implementation-Version"



The “ear” task as you can imagine packages up an ear file for deployment. It works very similar to the jar task and offers a few more optional tasks that relate directly to the ear file. Find more tasks on the Ant documentation page.

If you put these basic steps together and add some properties, you will end up with a simple ant script that can build most of your java projects. Customization of course, is where the power of any scripting tool will end up earning its keep. Java docs can be generated as part f the builds, FindBugs can do code analaysis, deployable artifacts can be ftp/scp’d across the network, heck you can even write you own Ant task to do whatever automated unit of work you want to define.

Here’s the complete ant script that I use for one of my simple projects.
The Ant task documentation page

Comments (1)

  1. 7:22 AM, July 26, 2012Danny  / Reply

    Many thanks!

    I didn’t realise there was a metainf task for Ant :).

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.