macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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:

  1. 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.
  2. Prepare the project: The files in your project should look like the following:

    • Delete the antExample target. When you delete the antExample target, the antExample executable 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.
  3. 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.
  4. Your Groups & Files area should look like the following:

  5. 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:

  6. Add a build.xml file: 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 it build.xml. Click Finish.
    • Select the build.xml file 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 src property appropriately for the location of your source files.

  7. 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.class file 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.