Integrating Ant with Xcode
Pages: 1, 2
Setting Up Xcode
Wouldn't it be great if you could execute the Ant build.xml file from within
Xcode? Xcode can be configured to use Ant when you hit Command-B. Here are the steps:
- Create a project: The project can be anything, but for
this example we will create a simple Java tool.
- Select New Project from the File menu.
- Select a Java tool.
- For the project name we will use antExample and for the project
directory we will use
~/dev/antExample/. (Feel free to use your own naming conventions and substitute them in as we go along.) - Wait awhile, as all of the classes are indexed. Grab your favorite beverage.
- Prepare the project: The files in your project should look like the following:

- Delete the
antExampletarget. When you delete theantExampletarget, theantExampleexecutable is automatically removed as well. (Hint: Just hit the delete key to delete the target. The delete option is not in the contextual menu.) - Delete the java executable as well.
- Delete the
- New Target: We need a new target for Ant to control and
build.
- Add a new target by doing a right-click on Target, select Add-->New Target.
- Select the External Target under the Special Targets heading.
- Enter AntTarget or another appropriate name.
- Your Groups & Files area should look like the following:

- Preparing the new target: We need to let Xcode know that
we want to use Ant to build the project.
- Click the
in the toolbar. - Click on the AntTarget. The following panel should appear in
the lower right-hand corner:

(Hint: Sometimes Xcode is a bit finicky when presenting the target panel. You might have to select a file first and then the AntTarget.
- Set the build tool to the path for your installation of Ant. In my particular
case, this is
/usr/local/apache-ant-1.6.1/bin/ant, but your directory could be different. - Scrolling down, you should remove all of the Build Settings except Product_Name.
- Your target panel should look something like the following now:

- Click the
- Add a
build.xmlfile: The build file becomes part of the Xcode project and is easy to edit from within Xcode.- Right-click on the root
antExample, select Add --> New File, select Empty File in Project. Click Next and name itbuild.xml. Click Finish. - Select the
build.xmlfile and paste in the following XML:<?xml version="1.0"?> <project name="antExample" default="myApp" basedir="."> <description>Ant Example build file.</description> <property name="src" location="/Users/[yourUsername]/dev/antExample"/> <property name="build" location="/build"/> <target name="init"> <mkdir dir=""/> </target> <target name="myApp" depends="init" description="compile the source "> <javac srcdir="" destdir=""/> </target> </project>You will need to set the
srcproperty appropriately for the location of your source files.
- Right-click on the root
- If all has gone well, hit Command-b or click on the build hammer in the
toolbar. It will tell you that it is building, and then after a few moments,
depending on the speed of your processor, it will say Succeeded in the upper
right-hand corner. You should now have an
antExample.classfile in the stated build directory.
You should be all set up now. Unfortunately, when you build the file, the build window is not aware of Ant and does not respond to the <echo> tags in your build file or tell you how much time the build process took -- as it does if executed from the command line.
Calling Xcode From Ant
Say you want to log into your system remotely through SSH to do a quick build
of an Xcode project. You can use Ant to easily configure building Xcode projects
from the command line. Xcode can be easily executed from the command line using
xcodebuild
and as such can be called by Ant. Here is the build file for a simple Xcode project:
<?xml version="1.0"?>
<project name="myApp" default="xcodeBuild" basedir=".">
<target name="xcodeBuild">
<echo>Start dBuild.
<exec executable="xCodeBuild">
<arg value="-alltargets"/>
</exec>
<echo>End dBuild.
</target>
</project>
This demonstrates executing a command line instruction from Ant. Ant is very
versatile. The echo task simply instructs Ant to display the enclosed
text when executed. This can help with debugging Ant build files. The exec
has a single argument passed in using the <arg/> tag and can have additional
attributes. If no arguments are passed in, Xcode builds the default target.
Creating a Jar
Creating a .jar file is trivial with Ant. You can add a target such as the following:
<target name="jar" description="build jar file." depends="dBuild">
<echo>Build jar file.</echo>
<jar destfile="/installer.jar" basedir=""/>
<echo>Jar File Build Complete.</echo>
</target>
Of course, the jarDir is specified as a property:
<property name="jarDir" value="jar"/>
Final Thoughts
As you can see, Ant is a versatile tool for the Mac developer. Once you start working with it, you'll find all sorts of ways it can save you time. For example, if you build Java Applications with Xcode, you can automate the creation of a .dmg. A .dmg is a disk image and is currently the preferred distribution method for applications on Mac OS X, unless you have configuration files to add, which might require an installer.
If you have a clever application of this tool, let us know in the TalkBacks section at the end of this article. In the meantime, happy exploration with Ant.
Derek Haidle is a Java programmer and owner of Haidle Consulting Inc. in Maple Grove, Minnesota.
Return to MacDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 5 of 5.
-
You should also use -emacs
2005-04-27 19:13:35 sbwoodside [Reply | View]
You should use -emacs option for ant, even in XCode 1.5, so that when you are browsing build errors, you will properly jump to the correct line of code. -emacs (or just -e) removes the line adornments that ant produces that mess up XCode.
Simon Woodside
-
XCode 1.5
2004-08-12 04:58:15 loumf [Reply | View]
XCode 1.5 has much better support for ant (and Java in general). I can finally use XCode to build and fix errors in Java code.
Definitely worth the download if you are coding Java on Mac.
-
WebObjects and Ant
2004-06-02 06:56:10 kmb11PSU [Reply | View]
Great article. However, I almost exclusively work with WebObjects. How can I adapt the above information to use Ant with WebObjects?
Thanks. -
WebObjects and Ant
2004-06-02 14:04:56 rparada [Reply | View]
I know it can be done, I just don't know the details.
For example, we have a large project source tree. The source tree has a ton of frameworks, web apps and a lot of tools. Our entire build uses ant.
I know that from within Xcode I can add a WebObjects component to a framework by right clicking the file group (or whatever it's called in Xcode) and selecting New File. Or if you want to add an image you right click the Web Server Resource and select Add Existing Files...
Each folder in our file system is structured as:
WebApps
App1
App2
Frameworks
Framework1
Framework2
Framework3
Tools
Tool1
Tool2
Tool3
Each one of those folders contains an ant.xml file. If you cd into one of those folders and type ant it builds that web app, framework or tool corresponding to the folder.
This is probably not much help, but that's sort of the way it works.
We also use CVS for source code control and we have developers on Mac OS X as well as Windows. Some developers use Eclipse and some developers use Xcode.
We can build from the command line or from Xcode GUI.






Can i use it for Cocoa-Touch(Objective c) projs..(Hope i can..)
So i want to know what change i want to make for the build.xml for Cocoa-Touch(Objective c) proj...
instead of javac, i can give gcc-4.0 , right???
What is destdir?? same as srcdir ??
Please explain the fields regarding to Cocoa-Touch(Objective c) proj...