2.2. Dependencies
Stringing together tasks within a target solves part of the problem of a complex build process. A target could run one task to generate any needed Java files, then another to compile them, another to collect the resulting class files into an archive, and so on.Ant provides a much more powerful and hence more commonly used mechanism for connecting tasks, called dependencies. Task B is said to depend on task A if A must complete successfully in order for B to run. One task may depend on many others and may be depended on by many others. Dependencies are represented within a build file by adding the depends attribute to a target, as in
If one target depends on multiple other targets, their names appear separated by commas in the depends attribute.A hierarchy of dependencies implies an order in which tasks will be executed, but it is a logical ordering rather than the explicit one given by placing multiple tasks in a target. Conceptually the programmer specifies what each individual target needs in order to do its job, and Ant figures out an ordering in which to execute the targets such that these requirements are met.To illustrate the idea of dependencies, consider a set of abstract targets designated A, B, C, and D with the relationships illustrated in Figure 2.1. In this figure an arrow between two tasks indicates that the task at the tail depends on the task at the headfor example A
<target name="B" depends="A">
...
</target>
Figure 2.1. Task dependencies.

Listing 2.1. Dependencies between tasks
When this build file is run, Ant will produce the following output:
<?xml version="1.0"?>
<project default="sampleA">
<target name="sampleA" depends="sampleB,sampleC">
<echo message="This is a simple target A"/>
</target>
<target name="sampleB">
<echo message="This is a simple target B"/>
</target>
<target name="sampleC" depends="sampleD">
<echo message="This is a simple target C"/>
</target>
<target name="sampleD">
<echo message="This is a simple target D"/>
</target>
</project>
This shows that B was run first, then D, then C, and finally A. Ant has some flexibility in the precise order in which it executed the targets, as long as B and C happened before A and D happened before C. If B also depended on D, this order would be further constrained. By changing the target definition for B to <target name="sampleB" depends="sampleD"> the order would be restricted to either DBCA or DCBA.
Buildfile: build.xml
sampleB:
[echo] This is a simple target B
sampleD:
[echo] This is a simple target D
sampleC:
[echo] This is a simple target C
sampleA:
[echo] This is a simple target A
BUILD SUCCESSFUL
Total time: 9 seconds