Search code examples
targetnantdepends

Order of execution of targets in NAnt


From the NAnt help page (http://nant.sourceforge.net/release/latest/help/fundamentals/targets.html):

NAnt tries to execute the targets in the depends attribute in the order they appear from left to right. It is possible that a target can get executed earlier when an earlier target depends on it:

<target name="A"/>
<target name="B" depends="A" />
<target name="C" depends="B" />
<target name="D" depends="C,B,A" /

Suppose we want to execute target D. From its depends attribute, you might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.

Since C depends on B and B depends on A, shouldn't the depends attribute of target (D) should be C only?

If I would replace depends of D with "C" alone, what would be the order of execution? Would it alter from the previous order?


Solution

  • With the targets as specified in the example:

    <target name="A" />
    <target name="B" depends="A" />
    <target name="C" depends="B" />
    <target name="D" depends="C,B,A" />
    

    If you execute D, the order of execution will be A, B, C, D. This is due to the dependency chains - a target that is depended on must execute before the target that depends on it. If you replaced the depends attribute of D with just C, you're right - the execution order would still be the same.

    What this example is highlighting however, is that the dependencies may not be executed in the same order as specified in the depends attribute. For example, if you just had these targets:

    <target name="A" />
    <target name="B" />
    <target name="C" />
    <target name="D" depends="C,B,A" />
    

    and executed D, the order of execution would now match the order of the depends attribute - ie C, B, A, D.