Ant Dependencies Running Twice
By Adrian Sutton
Most people believe that ant will only ever execute a target once per execution of ant. So if we have targets A, B, C and D, where A depends on B and C and C and D both depend on D.
<target name="A" dependencies="B, C"/>
<target name="B" dependencies="D"/>
<target name="C" dependencies="D"/>
<target name="D"/>
When we run ‘ant A’, we expect each task to execute once, D first, then B and C, ending with A. Since there is no dependency between B and C, they may execute in any order, so we might also get D, C, B, A. All pretty straight forward.
Similarly, if we run ‘ant B’ we expect (and get) D then B and ‘ant C’ gives D then C. Most people expect that if you run ‘ant B C’, the execution path would be D, B, C but that’s not what happens. Instead we get D, B, D, C. Each target passed to the command line creates a new dependency chain and runs all targets in that chain.
The other case that catches people out is when using antcall. The antcall task actually sets up essentially a new execution of ant (by default inheriting any properties that have already been set). It never inherits the dependency chain or list of targets that have already been executed. So if our targets were:
<target name="A">
<antcall target="B"/>
<antcall target="C"/>
</target>
<target name="B" dependencies="D"/>
<target name="C" dependencies="D"/>
<target name="D"/>
Then the execution order for ‘ant A’ would be A,D,B,D,C.
The cost of running targets twice is reduced because most ant tasks do nothing if their inputs haven’t changed (so javac only compiles files once), but for a large project there can still be a significant cost in checking over all the files to determine if anything needs to be done. So whenever possible, use actual dependencies to build up a chain of targets instead of antcall or specifying multiple targets on the command line.