Go to content Go to sidebar

Ant Hello World

There is a long standing tradition in software of starting with "Hello World." We'll start our series of Ant tutorials with the same. I'm going to assume that you've successfully installed Ant and that you understand the basics of XML syntax. And just in case it makes a difference, I'm using version 1.5.1:


$ ant -version
Apache Ant version 1.5.1 compiled on October 2 2002

Copy the following lines into a file named "build.xml" (the default name assumed by ant).


<project default="hello">
  <target name="hello">
    <echo message="Hello, World"/>
  </target>
</project>

And execute ant.


$ ant
Buildfile: build.xml

hello: [echo] Hello, World

BUILD SUCCESSFUL Total time: 2 seconds

Now that we've got a working build file, let's take a closer look at it's contents:

Next, let's create another build file and examine some common command line options in ant. Copy the following lines into a file named "echo.xml" (the new lines are shown in bold).


<project default="hello">
  <target name="hello">
    <echo message="Hello, World"/>
  </target>
<target name="goodbye"> <echo message="Goodbye, Cruel World"/> </target> </project>

Execute ant using:


$ ant -f echo.xml goodbye
Buildfile: echo.xml

goodbye: [echo] Goodbye, Cruel World

BUILD SUCCESSFUL Total time: 2 seconds

Finally, let's take a look at the target depends attribute. Edit "echo.xml" and add the target "all" as shown below:


<project default="hello">
  <target name="hello">
    <echo message="Hello, World"/>
  </target>

<target name="goodbye"> <echo message="Goodbye, Cruel World"/> </target>
<target name="all" depends="hello,goodbye" /> </project>

The target depends attribute specifies targets that should be achieved prior to executing the current target. Ant will attempt to execute depends targets left to right, but this may be altered by their respective depends targets. In this case "hello" and "goodbye" have no dependencies, so they will execute in that order.


$ ant -f echo.xml all
Buildfile: echo.xml

hello: [echo] Hello, World

goodbye: [echo] Goodbye, Cruel World

all:

BUILD SUCCESSFUL Total time: 2 seconds

Disclaimer: I don't claim to be an expert on ant. Please send comments and corrections.