Ant - difference between task and target

ant, build, java

Solution

Targets contain one or more tasks.

A target has a user-defined name, and usually does something high-level like "compile the code", or "build a deployable jar file". It is just a convenient container for tasks (and also allows you to specify dependencies upon other targets).

A task is provided and named by Ant (or plug-ins) and is generally something lower-level like "copy a file", "create a directory". You can create new tasks (see the Ant manual) if the built-in ones don't do what you need.

An example from the Ant tutorial:

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes"/>
</target>

The target is called "compile" (because it is intended to compile some code. However, the name is arbitrary - I could just as well call it "doUsefulStuff"). To complete this target, we specify that we want to execute two tasks:

Make a directory (using the `mkdir` task)

Compile some code, and put the compiled classes into the directory from step 1, using the `javac` task

(Disclaimer - it might be possible to create targets with zero tasks - I haven't checked - but they wouldn't be much use).

Problem

I am new to Ant, and having difficulty in understanding some of its basic things like `task` and `target`. Online documentation and books say that `target` is a stage of the entire build process, while `task` is the smallest unti of work. However, I find it very difficult to understand what exactly is meant by this, Can someone explain in depth with examples what are `target`s and `task`s in Ant?

Original source