Jump to content

Apache Ant/Depends

From Wikibooks, open books for an open world

The depends attribute can be included in the target tag to specify that this target requires another target to be executed prior to being executed itself. Multiple targets can be specified and separated with commas.

<target name="one" depends="two, three">

Here, target "one" will not be executed until the targets named "two" and "three" are, first.

Example of using the depends attribute

[edit | edit source]

Here is an example of a build file that executes three targets in order, first, middle and last. Note that the order the targets appear in the build file is unimportant:

  <?xml version="1.0" encoding="UTF-8"?>
  <project default="three">
     <target name="one">
        <echo>Running One</echo>
     </target>
  
     <target name="two" depends="one">
        <echo>Running Two</echo>
     </target>
  
     <target name="three" depends="two">
        <echo>Running Three</echo>
     </target>
  </project>

Sample Output:

  Buildfile: build.xml
  
  one:
     [echo] Running One
  
  two:
     [echo] Running Two
  
  three:
     [echo] Running Three
  
  BUILD SUCCESSFUL
  Total time: 0 seconds

Redundant dependency

[edit | edit source]

Ant keeps track of what targets have already run and will skip over targets that have not changed since they were run elsewhere in the file, for example:

  <?xml version="1.0" encoding="UTF-8"?>
  <project default="three">
     <target name="one">
        <echo>Running One</echo>
     </target>
  
     <target name="two" depends="one">
        <echo>Running Two</echo>
     </target>
  
     <target name="three" depends="one, two">
        <echo>Running Three</echo>
     </target>
  </project>

will produce the same output as above - the target "one" will not be executed twice, even though both "two" and "three" targets are run and each specifies a dependency on one.

Circular dependency

[edit | edit source]

Similarly, ant guards against circular dependencies - one target depending on another which, directly or indirectly, depends on the first. So the build file:

  <?xml version="1.0" encoding="UTF-8"?>
  <project default="one">
     <target name="one" depends="two">
        <echo>Running One</echo>
     </target>
  
     <target name="two" depends="one">
        <echo>Running Two</echo>
     </target>
  </project>

Will yield an error:

  Buildfile: build.xml
  
  BUILD FAILED
  Circular dependency: one <- two <- one
  
  Total time: 1 second

Next Chapter, Next Cookbook Chapter