Search code examples
ant

In what order are Ant target's "if" and "depends" evaluated?


That is, will calling the following target when testSetupDone evaluates to false, execute the targets in dependency chain?

<target name="-runTestsIfTestSetupDone" if="testSetupDone" depends="-runTests" />

Solution

  • Yes, the dependencies are executed before the conditions get evaluated.

    From the Ant manual:

    Important: the if and unless attributes only enable or disable the target to which they are attached. They do not control whether or not targets that a conditional target depends upon get executed. In fact, they do not even get evaluated until the target is about to be executed, and all its predecessors have already run.


    Here is an example:

    <project>
      <target name="-runTests">
        <property name="testSetupDone" value="foo"/>
      </target>
      <target name="runTestsIfTestSetupDone" if="testSetupDone" depends="-runTests">
        <echo>Test</echo>
      </target>
    </project>
    

    The property testSetupDone is set within the target in depends, and the output is:

    Buildfile: build.xml
    
    -runTests:
    
    runTestsIfTestSetupDone:
         [echo] Test
    
    BUILD SUCCESSFUL
    Total time: 0 seconds
    

    Target -runTests is executed, even though testSetupDone is not set at this moment. runTestsIfTestSetupDone is executed afterwards, so depend is evaluated before if.