How to Install Ant on Mac OS X


Table of Contents:

Summary

This document describes how I installed Ant 1.5.2 and various tasks on my iMac running Mac OS X 10.1.5 (Puma).
The instructions also work with 10.2 (Jaguar) and 10.3 (Panther). This is one way to do so; others are possible. Everyone has his 'One True Way' to organize software. Improvements are welcome!

Return to TOC

Prerequisites

Some program capable of downloading from the internet. These instructions will reference wget but almost anything will do, including your web browser. You will also need to use sudo, an application which gives a user the ability to run commands as root while logging all commands and arguments.

Return to TOC

Description

Apache Ant is a Java-based build tool. It processes configuration files, named build.xml by convention, written in XML.

If you're planning to write your own build files, I highly recommend the purchase of Java Development with Ant, written by Erik Hatcher and Steve Loughran. Ant comes with documentation, but it'll make much more sense after reading this book.

Return to TOC

Installation

Start Terminal.app which can be found in /Applications/Utilities and get ready to type...

Download

Download the compressed binary distribution and save it to /usr/local, where it would usually be installed by default. Most Un*x install scripts use either /usr/local or /opt as their default installation path. Don't swim upstream if you don't have to.

The /usr/local directory is owned by root; administrator authorization will be required in order to affect changes. Having it owned by another account protects against accidental changes.

$ cd /usr/local
$ sudo wget http://www.rge.com/pub/infosystems/apache/ant/binaries/apache-ant-1.5.2-bin.tar.gz

Unarchive

Unpack the compressed binary distribution. The resulting folder will be /usr/local/apache-ant-1.5.2

$ sudo gunzip apache-ant-1.5.2-bin.tar.gz
$ sudo gnutar xf apache-ant-1.5.2-bin.tar

Delete the binary distribution file as it is no longer needed.

$ sudo rm apache-ant-1.5.2-bin.tar

Setup

Create symbolic link from /usr/local/apache-ant-1.5.2 to /usr/local/ant. This will map the physical directory to a logical directory. We do this so settings do not have to be changed with each upgrade.

$ sudo ln -s apache-ant-1.5.2 ant

Make sure symbolic link exists from /usr/local/bin/ant to /usr/local/ant/bin/ant.

$ cd /usr/local/bin

If the output looks like this, a link already exists and nothing further need be done.

$ ls ant
ant

If the output looks like this instead, create the symbolic link.

$ ls ant
ls: ant: No such file or directory
$ sudo ln -s /usr/local/ant/bin/ant

Mac OS X Integration

It can be handy to integrate the Un*x installation with the Mac OS X style file hierarchy. The layout looks like this:

$HOME
..../Developer
......../Documentation
............/Tools
................/Ant@-> ../../Tools/Ant/Home/docs
......../Tools
............/Ant
................/Home@-> Versions/Current
................/Extensions@-> Home/lib
................/Versions
..................../Current@-> 1.5.2
..................../1.5.2@-> /usr/local/apache-ant-1.5.2

Here are the commands to create the integration hierarchy.

$ cd ~
$ mkdir -p Developer/Documentation/Tools
$ cd Developer/Documentation/Tools
$ ln -s ../../Tools/Ant/Home/docs Ant
$ cd ../../..
$ mkdir -p Developer/Tools/Ant/Versions
$ cd Developer/Tools/Ant
$ ln -s Versions/Current Home
$ ln -s Home/lib Extensions
$ cd Versions
$ ln -s /usr/local/apache-ant-1.5.2 1.5.2
$ ln -s 1.5.2 Current

Whew! You're finished. Wanna see what you made?

$ cd ~/Developer
$ open .

Return to TOC

Configuration

Fortunately, configuration of Ant is fairly trivial. Two environment variables, ANT_HOME and JAVA_HOME, must be set to properly use Ant. While they can be set manually when needed, that's a pain. Normal setup will place them into one of the various shell startup files.

Using a text editor (like pico or vi), append these lines accordingly. Edit the two files below as shown to automatically have these environment variables set (upon login) for either csh-based or sh-based shell. Note that the different shells use similar but not identical syntax.

$ pico ~/.login
  UW PICO(tm) 2.3       File: /Users/johndoe/.login    

setenv JAVA_HOME /Library/Java/Home
setenv ANT_HOME /usr/local/ant

^G Get Help   ^O WriteOut   ^R Read File  ^Y Prev Pg    ^K Cut Text   ^C Cur Pos
^X Exit ^J Justify ^W Where is ^V Next Pg ^U UnCut Text ^D Del Char
$ pico ~/.profile
  UW PICO(tm) 2.3       File: /Users/johndoe/.profile    

export JAVA_HOME=/Library/Java/Home
export ANT_HOME=/usr/local/ant

^G Get Help ^O WriteOut ^R Read File ^Y Prev Pg ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where is ^V Next Pg ^U UnCut Text ^D Del Char

Check that this worked with the following commands. If not, make the appropriate corrections before proceeding.

$ . ~/.profile
$ echo $JAVA_HOME
/Library/Java/Home
$ echo $ANT_HOME
/usr/local/ant
$ csh
% source ~/.login
% echo $JAVA_HOME
/Library/Java/Home
% echo $ANT_HOME
/usr/local/ant
% exit
$

The last thing to check is your PATH environment variable. Apple changed the default paths after Puma, which dropped /usr/local/bin from certain setups. If the output doesn't include it, make appropriate changes to your setup.

$ echo $PATH
/Users/johndoe/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin
$ which ant
/usr/local/bin/ant

Return to TOC

Checkout

Let's do a basic checkout to make sure everything is installed correctly.
Create a simple project named GettingStarted.

$ cd ~
$ mkdir -p Projects/GettingStarted/src
$ cd Projects/GettingStarted

Download the source code and build file. Put the source code in the src directory. Arrange them as such.

$HOME
..../Projects
......../GettingStarted
............/src
................/Main.java
............/build.xml

Now invoke Ant to build this simple project.

$ ant
Buildfile: build.xml

init:
    [mkdir] Created dir: /Users/johndoe/Projects/GettingStarted/build/classes
    [mkdir] Created dir: /Users/johndoe/Projects/GettingStarted/dist

compile:
    [javac] Compiling 1 source file to /Users/johndoe/Projects/GettingStarted/build/classes

execute:
     [java] a
     [java] b
     [java] /Users/johndoe/Projects/GettingStarted

default:

BUILD SUCCESSFUL
Total time: 13 seconds

Return to TOC

Upgrade

Anyone wondering how much effort will be required to upgrade when the next version comes out? Here are the instructions required to update to Ant 1.5.4.

Download

Nothing new here as compared with the installation instructions except the version change.

$ cd /usr/local
$ sudo wget http://www.rge.com/pub/infosystems/apache/ant/binaries/apache-ant-1.5.4-bin.tar.gz

Unarchive

Unpack the compressed binary distribution. The resulting folder will be /usr/local/apache-ant-1.5.4

$ sudo gunzip apache-ant-1.5.4-bin.tar.gz
$ sudo gnutar xf apache-ant-1.5.4-bin.tar

Delete the binary distribution file as it is no longer needed.

$ sudo rm apache-ant-1.5.4-bin.tar

Setup

Create symbolic link from /usr/local/apache-ant-1.5.4 to /usr/local/ant. This will remap the logical directory to use the new version.

$ sudo rm ant
$ sudo ln -s apache-ant-1.5.4 ant

Mac OS X Integration

This will be much simpler than creating the Mac OS X layout was originally. To upgrade, create a new directory under 'Versions' and change the 'Current' symbolic link to point to the new release.

$ cd ~
$ cd Developer/Tools/Ant/Versions
$ ln -s /usr/local/apache-ant-1.5.4 1.5.4
$ rm Current
$ ln -s 1.5.4 Current

Return to TOC

External Tasks

Since Ant build files are extensible, many third party tools are also available as Ant tasks. This allows invoking them as though they were an integral part of Ant. There are several methods to allow this but the simplest is to make use of Ant's library directory. This usage corresponds to how $JAVA_HOME/lib/ext (or /Library/Java/Extensions on Mac OS X) works to extend Java. Essentially, JAR files copied to this directory are automatically in the classpath of Ant's JVM.

I recommend appending the tool's version number to the basename of each JAR file copied to this location. It makes it a quick and easy task to determine what version is being used.

Return to TOC


[Valid CSS!] [Valid XHTML 1.1!] Back to Home Page