Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant - difference between task and target

Tags:

java

build

ant

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 targets and tasks in Ant?

like image 595
SexyBeast Avatar asked Jan 10 '23 18:01

SexyBeast


2 Answers

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:

  1. Make a directory (using the mkdir task)

  2. 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).

like image 121
DNA Avatar answered Jan 13 '23 06:01

DNA


Another fundamental difference is that when you run ant you indicate a target (not a task) to be executed. So, when you call ant via command line, you specify ant [options] [target]. If you don't specify the target, the one indicated as default in your build file (build.xml) is executed.

If you open an ant build file on the ant view in Eclipse, the executable options are the targets, not the tasks.

For practical purposes, targets are further divided into private (aka, internal) and public. The difference is that the <target> declaration of a public target contains the description attribute. I mention this because you may want to decompose/refactor a target in sub-steps in your build.xml. The sub-steps can be internal targets.

like image 29
Paulo Merson Avatar answered Jan 13 '23 06:01

Paulo Merson