Thursday, November 3, 2011

Notes on Apache Ant

I'm an Apache Ant newbie. I've known for years that it exists, but I've never used it. I just installed it, today, and now I'm trying to understand what it does and how to use it. Here are some notes:


  • An Ant build contains one project
  • That project has at least one target
  • Each target contains tasks


The build file defines these things and is written in XML and is called build.xml. It has to define at least one target. The target defines one or more tasks to perform when it is run. Here is my first build file:

<?xml version="1.0" encoding="UTF-8"?>  
<project name="helloworld" default="init" basedir=".">  
    <description>  
    Build file for the Hello World application.  
    </description>  
    <target name="init" description="Initialize the build.">
        <touch file="testinit" />
    </target>
</project>

It includes a description (of the project) and one target. The target is simply a set of tasks you want to run. It can be called whatever you want, really. The target name attribute is what you will call from the command line when you want to run those tasks. In this example, my target has the name attribute "init". In that target, the only task I've included is the "touch" task. This will execute the *nix "touch" command. The "file" attribute within the "touch" task specifies which file to touch. (See the Ant manual for more available tasks. They are legion.)

Now, to run this, I go to the directory containing the build.xml file and issue the following command:

ant init

Notice that I didn't say "ant build.xml" or "ant build." This is because I'm telling ant to run a specific target. Also, if I change the name of the file from build.xml to moo.xml, it will break. Ant expects the build file to be called build.xml.

Wasn't that fun?

No comments:

Post a Comment