Automate iOS Builds With ANT
This is a simple tutorial on how to automate an iOS build with ANT. ‘Why ANT,’ you ask? Because ANT rocks. Coming from the world of Flex development, I am very comfortable with ANT. I have used ANT to compile and deploy Flex application many times. ANT is simple. Install it, write a simple xml file, and run it.
What you need
Step 1: Install TextMate
If you haven’t done so already install TextMate. For those of you who don’t know, TextMate is a kick ass text editor. I won’t go into how awesome it is because that could take up an entire blog post. Just know that it rocks and you can install bundles to do different things.
Step 2: Install the ANT bundle for TextMate
Download, unzip and double click the TextMate ANT bundle.
Step 3: Verify that you have ANT installed
Open terminal and type “ant -v”. ANT comes installed on Mac so it should be there. If not you can google it and find out how to do that.
Step 4: Create a build file.
For those of you new to ANT, an ANT build file is nothing more than an xml file. Here is an example of a basic build file.
<project name="iOS Compile" default="Compile" basedir=".">
<property name="MY_PROP" value="myprop"/>
<target name="Compile">
<echo level="info">${MY_PROP}</echo>
</target>
</project>
Each build file contains a root “project” tag and any number of “target” tags. Each target can be run individually or you can run the build file and it will run whichever target is specified as default in the project tag. “Property” tags are also very useful for storing frequently used values. “Echo” tags are used to output text. In this example we are outputting the property MY_PROP.
Step 5: Run the build file.
In TextMate, open your build file and hit command+r. This will run the default target of your build file. Alternatively, you can go to bundles>ant>build target to run a specific target in the build file.
That’s great, but how do I compile an iPhone project? Well that’s simple…
<project name="iOS Compile" default="compress" basedir=".">
<target name="clean">
<echo level="info">Cleaning ...</echo>
<exec executable="xcodebuild" dir="${IOS_PROJECT_ROOT}"
failonerror="true">
<arg line="-project AppName.xcodeproj"/>
<arg line="-alltargets clean" />
</exec>
</target>
<target name="compile" depends="clean">
<echo level="info">Compiling...</echo>
<exec executable="xcodebuild" dir="${IOS_PROJECT_ROOT}"
failonerror="true">
<arg line="-project Branded.xcodeproj"/>
<arg line="-alltargets" />
<arg line="-configuration Release" />
</exec>
</target>
<target name="compress" depends="compile">
<echo level="info">Compressing...</echo>
<exec executable="zip" dir="${PATH_TO_IOS_BUILD_DIR}"
failonerror="true">
<arg line="-y -r AppName.zip AppName.app"/>
</exec>
</target>
</project>
Ok, lets go through this step by step. First, we have 3 targets, Clean, Compile, and Compress. You will probably notice the depends attribute. When a target is run and it has a depends attribute the target referenced in that attribute is run first. So in this case our default target is compress. Compress depends on compile and compile depends on clean. So when the build file is run it will go… Clean - Compile - Compress.
You’ll notice that all these targets have something in common, which is the exec task. The exec task can do any number of command line functions. In the case of clean and compile we are calling the xcodebuild command which is xcodes command line interface. In the case of compress we are calling the zip command. So you can see with this, the sky is the limit. The exec command also has a dir attribute which will specify the directory where the command will execute. In the clean and compile targets we are setting a property that points to the xcode project directory. The failonerror attribute will just kill the build if the command hits an error. Each exec command can have any number of arg nodes. Each of these nodes is used to pass arguments to the command. To see a list of what’s available for xcodebuild just add the following arg node.
<arg line="-help" />
Conclusion
This is by no means a thorough tutorial on ANT. It is meant to be a simple introduction to help you get started. Below is a list of resources to help you get more out of ANT. As always, feedback is appreciated and don’t hesitate to ask questions.
Note: TextMate isn’t required since you can run an ANT build file from the command line as well.