oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Integrating Ant with Xcode

by Derek Haidle

It was a dark and hazy night, and you had just crunched out your latest round of Java code on Mac OS X using Xcode. The phone rings. It's your boss calling from her vacation in Mexico. She wants your code to build seamlessly on your Mac as well as under Windows. You hang up the phone as thunder begins to clap outside and lightning blinks around you. You start to sweat. You feel moisture pouring down your face.

You wake up to find your cat licking your face, your light bulb flickering badly, and “Thunder Rolls” by Garth Brooks blaring in the background. Wow, your boss isn't even on vacation and you don't listen to country. You gotta stop coding at 3 in the morning!

OK, over-dramatizations aside, building Java code on multiple platforms is a problem facing many Java developers. The natural answer tends to be Ant, an open-source tool hosted by the Apache Group.

Why Use Ant?

Ant has a number of advantages:

  • Standards Based: The build file is written in XML, making it easy to edit.
  • Cross-Platform: Ant works the same on Mac OS X, Windows, or Linux.
  • Easily Extendable: Custom Ant tasks can be written to extend the functionality of Ant. There has already been a custom Ant task created to automatically create a disk image (.dmg file) after a build.
  • IDE Support: A number of IDEs (Integrated Development Environments) provide strong integration with Ant, including Eclipse.
  • Open Source: If you want to get in under the hood to see how Ant goes about building projects -- no problem. The source code is readily available.

I assume the following in this article:

  • You are a developer on Mac OS X.
  • You have the developer's tools installed, specifically Xcode.
  • You have a basic understanding of Java.

Installing Ant

Installing Ant is very straightforward. This previous article by David Miller contains instructions for installing Ant on Mac OS X.

Understanding the Build File

One of the easiest ways to get started using Ant is to use it at the command line through the Terminal. Ant automatically looks for the build.xml file in the base directory of your project. After creating a simple Xcode project, you can build it using Ant from the command line.

Within Xcode, you can create a build.xml file. The build.xml file to go along with a simple Xcode project might look like the following:

<?xml version="1.0"?>

<project name="HelloWorld" default="myApp" basedir=".">
	<description>HelloWorld build file.</description>
	<property name="src" location="."/>
	<property name="build" location="build"/>

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

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


Let's step through this simple build file:

<project name="HelloWorld" default="myApp" basedir=".">

We start by creating the <project> node. This node must be the uppermost node in the build.xml file. Only one <project> node is allowed per build.xml file. The project has a name, which can be anything short and descriptive. The default attribute tells Ant the first task to kick-off if no task is specified. We'll examine tasks in a moment. The base directory is the root of the project.

<description>HelloWorld build file.</description>
    <property name="src" location="."/>
    <property name="build" location="build"/>

The <description> is the description for the project. It is a great place to include a bit of documentation about the build.xml file and the project in general. The <property> tag defines variables that can be used later on in the build file. In this case, we define two variables for the location of source files and the destination of the build files.

Properties are easy to reference by using a syntax very similar to Java Standard Tag Library (JSTL). The src variable is referenced like this: ${src} . Other system variables are also available, such as the OS name (${}) and the project name (${}).

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

The target is a set of items to do. In this case the init item makes sure that the build directory is available.

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

The next target called build has a dependency on the init target. Hence the init target will be called before the build target. The description is another great place to add a little documentation. Next, Ant calls the javac task, just as the mkdir was also a task. The attributes are fairly self-explanatory. The srcdir is the source directory for the code. The destdir is the destination directory for the build.

Executing the build file from the command line is easy. Simply open Terminal and go to your project directory. If you added Ant to your terminal configuration file, then all you have to do is type in ant once you have gone to the base of your project directory. Ant automatically looks for the build.xml file and executes the default target. Since the default target, called build, depends on the init target, the init target is executed first. It's that easy!

Here is the output from a simple build.xml file executed at the command line:

Buildfile: build.xml


     [echo] Start dBuild.
    [javac] Compiling 1 source files to /Users/dhaidle/dev/antTest/build
     [echo] End dBuild.

Total time: 3 seconds

If you have a larger Ant file, you might want to execute a particular target. In the aforementioned example, you can execute just the init target by entering ant init.

Pages: 1, 2

Next Pagearrow