Ant Targets

Jakob Jenkov
Last update: 2015-08-18

An Ant target is a sequence of tasks to be executed to perform a part (or whole) of the build process. Ant targets are defined by the user of Ant. Thus, what tasks an Ant target contains depends on what the user of Ant is trying to do in the build script. Similarly the name of an Ant target is also decided by the creator of the Ant build script.

Some of the more commonly occurring Ant targets are:

  • clean: An Ant target for cleaning up the build output directory - e.g. deleting compiled class files, deleting generated source code, or just deleting the whole build output directory completely.
  • compile: An Ant target for compiling the Java source code in the project.
  • jar: An Ant target for creating a JAR file from the compiled classes.
  • test: An Ant target for running all unit tests for the Java code.
  • javadoc: An Ant target for creating JavaDoc comments from the Java code.

Not every project will have exactly these targets. As mentioned earlier, it depends on what you are trying to do with your Ant build script. If you are compiling Java code, then yes, perhaps you will have targets similar to the above. But if you are doing something else, like adding HTML pages into an EPUB book (essentially a ZIP file with XML and HTML files inside), then you will most likely have different Ant targets in your build script.

Defining an Ant Target

You define an Ant target using the target element. Here is an Ant target example:

<project name="MyProject">

    <target name="firstTarget">
        
    </target>
</project>

This Ant target example defines a single target named firstTarget . The target does not contain any tasks, meaning the target does not do anything. It is just an empty target definition. The target element is nested inside the project element as a direct child of the project element.

In case your Ant build script contains both targets and properties, it is common (but not required) that the properties are listed before the targets in the Ant build script. Here is an example of an Ant build script with both an Ant target and an Ant property:

<project name="MyProject">
    
    <property name="myProperty">theValue</property>

    <target name="firstTarget">
        
    </target>
</project>

Adding Tasks to Targets

To make an Ant target actually do something you must add some tasks to it. Ant comes with a lot of built-in tasks that can help you do all kinds of things, like creating directories, copy files, compile Java code, generate JavaDoc, create ZIP and JAR files, upload files to servers via SSH, and many, many other tasks.

In this example I will use a simple Ant task named echo. The echo task simply prints a message out to the command prompt. This may sometimes be useful inside larger builds, to give a status of what build work has been carried out. Here is an example of an Ant target with an echo task inside:

<project name="MyProject">
    
    <property name="version">1.2.3</property>

    <target name="firstTarget">
        <echo>This is version ${version}</echo>
    </target>
</project>

If you run the Ant target firstTarget the echo task will get executed. The echo task as defined here will print out the message "This is version 1.2.3" to the console. Notice the ${version} property reference inside the echo element body. This reference is substituted for the value of the version property before the message is printed. Here is an example of what Ant outputs when running the firstTarget target:

D:\data\projects\build-experiments>ant firstTarget
Buildfile: D:\data\projects\build-experiments\build.xml

firstTarget:
     [echo] This is version 1.2.3

BUILD SUCCESSFUL
Total time: 1 second

D:\data\projects\build-experiments>

Of course the directories depends on where you have your Ant build script located, but the output should be the same as shown above (or very similar at least).

You will see examples of more useful tasks in later texts in this Ant tutorial.

Target Dependencies

An Ant target can depend on other targets. If an Ant target B depends on another target A, Ant will not execute target B until target A has been executed. If you instruct Ant to execute target B then Ant will first execute target A and then target B.

Target dependencies can be useful to guarantee that one target has been executed before another target is executed. For instance, that your compile target is executed before your jar target, as it may make no sense to generate a JAR file if the Java source code has not been compiled first.

The targets an Ant target depends on are listed inside the depends attribute of the target element. Here is an example target with a dependency:

<project name="MyProject">
    
    <target name="A">
        <echo>Target A</echo>
    </target>
    
    <target name="B" depends="A">
        <echo>Target B</echo>
    </target>
</project>

Notice how target B has a depends attribute which contains the name of target A.

If a target needs to depend on multiple other targets, list all the names of the targets separated by commas inside the depends attribute. Here is an example of multiple target dependencies:

<project name="MyProject">

    <target name="A">
        <echo>Target A</echo>
    </target>

    <target name="B" >
        <echo>Target B</echo>
    </target>

    <target name="C" depends="A, B">
        <echo>Target B</echo>
    </target>
</project>

Notice how target C depends on both target A and B.

In case a target is listed more than once in the full chain of dependencies, it will still only get executed once. In the following example target C depends on target A and B, and target B also depends on target A. But target A will only get executed once, not twice, even if listed twice in target dependencies.

<project name="MyProject">

    <target name="A">
        <echo>Target A</echo>
    </target>

    <target name="B" depends="A">
        <echo>Target B</echo>
    </target>

    <target name="C" depends="A, B">
        <echo>Target B</echo>
    </target>
</project>

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next