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?
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With