Java builds made easy, using Ant

Michael McCabe

Revision History
Revision 1.0October 25th, 2005
Ready for release/presentation
Revision 0.6October 20th, 2005
Added introduction
Revision 0.5October 18th, 2005
Initial draft with 1st example

Abstract

This tutorial will serve as a brief introduction to Ant, a Java build tool that is designed as a Make replacement.


Table of Contents

Feedback
Copyright
Disclaimer
Introduction
Installing Ant
A Simple Example
A Bigger Example
Other very usefull Tasks
Other Resources

Feedback

Comments on thie Guide may be directed to Michael McCabe ().

Copyright

This document, Java builds made easy, using Ant is copyright (c) 2005 by the Clarkson Open Source Institute. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is available at http://www.gnu.org/copyleft/fdl.html.

Disclaimer

No liability for the contents of this document can be accepted. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies, that could be damaging to your system. Proceed with caution, and although this is highly unlikely, the author(s) do not take any responsibility.

All copyrights are held by their by their respective owners, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Naming of particular products or brands should not be seen as endorsements.

Introduction

Tired of using Makefiles for your Java programs. When you switch Operating Systems or shells you need to sometimes rework your Makfiles because of incompatabilities between different versions of Make and your shell. This was a problem that was frustrating many people. And this is where Ant comes in. It's a Java based build tool that is cross platform and XML based. No more trying to figure out whether that your command isn't working due to a tab/space problem or dealing with the other problems in Make. Ant just makes builds easy.

Installing Ant

Ant doesn't come preinstalled on most Unix based systems. It is extremely easy to install though, all you've got to do is set a couple of environmental variables after you unpackage the binary. You can also build it from source, but that usually is unnecessary. The first thing that you'll need to do is download the Ant tarball. It is available here.

After you have downloaded and extracted the tarball there is some additional setup tasks that you will need to perform. You will need to add the Ant bin directory to your path, set the ANT_HOME environment variable, and make sure that the JAVA_HOME environmental variable is set. After these are setup you are all set to run ant build scripts.

A Simple Example

For the first example in this tutorial I have written a very simple build file for a Hello World style program. The build file is completely overkill for this program but it does show a style that is effective for writing build files for most projects, even if they are extremely large in size.

Example 1. Property listing in the build file

  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

In the above code snippet several properties are set that are used in other places throughout the build file. These properties are the equivalent of shell variables in Makefiles. They can prove extremely usefull when you have a large build environement.

Example 2. Initilization Target

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
    <mkdir dir="${dist}"/>
  </target>

This target is extremely simple. Its sole purpose is to prepare the source tree for the build. In this example it creates the necessary directories and also creates a timestamp.

Example 3. Compilation Target

  <target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

This target builds the sources files in the specified directories. In more advanced examples you can specify which Java Virtual Machine the code is being compiled for, whether it should be optimized and other things like that. Also you may need to specify your classpath inside this section of the build file if you are using libraries that are not on your classpath.

Example 4. Jarfile Target

  <target name="dist" depends="compile">
    <jar jarfile="${dist}/example-one-${DSTAMP}.jar" basedir="${build}"/>
  </target>

For this example we do not really need this target. I don't think anyone would actually use a jarfile for our hello world program. But it serves as an example of how to build a jarfile using ant.

Example 5. Clean Target

  <target name="clean">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

A clean target should be included in each of your build files. It specifies what should be removed when you want to clean your source tree.

A Bigger Example

For the second example I will be showing you a real ant build file. The file I will be explaining is from my jXenophilia project.

Other very usefull Tasks

Ant like most build systems can be used for many different tasks. These tasks can prove extremely usefull and help you improve your efficiency and software quality. Here is a short listing of some of the tasks that I have previously used.

  • Manipulate Unix file permissions

  • Run JUnit Unit Tests

  • Precompile Java Server Pages (JSP)

  • Copy files using SCP

  • Build RPMs of your software

  • Do cvs and subversion commits and checkouts

Other Resources

Here's a listing of some other very usefull resources.