Ant目标的"if"和"depends"的评估顺序是什么?

Tah*_*tar 20 ant

也就是说,当testSetupDone求值为false时会调用以下目标,在依赖链中执行目标吗?

<target name="-runTestsIfTestSetupDone" if="testSetupDone" depends="-runTests" />
Run Code Online (Sandbox Code Playgroud)

Pet*_*ang 20

是的,在评估条件之前执行依赖项.


来自Ant手册:

重要说明: if和unless属性仅启用或禁用它们所附加的目标.它们不控制条件目标所依赖的目标是否被执行.实际上,在目标即将执行之前,它们甚至都没有得到评估,并且它的所有前辈都已经运行了.


你也可以试过自己:

<project>
  <target name="-runTests">
    <property name="testSetupDone" value="foo"/>
  </target>
  <target name="runTestsIfTestSetupDone" if="testSetupDone" depends="-runTests">
    <echo>Test</echo>
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)

testSetupDone在依赖目标中设置属性,输出为:

Buildfile: build.xml

-runTests:

runTestsIfTestSetupDone:
     [echo] Test

BUILD SUCCESSFUL
Total time: 0 seconds
Run Code Online (Sandbox Code Playgroud)

目标-runTests执行,即使此时testSetupDone未设置,并runTestsIfTestSetupDone在之后执行,因此在之前depend进行评估(使用Ant 1.7.0). if


Bra*_*rks 5

来自文档:

Ant tries to execute the targets in the depends attribute in the order they 
appear (from left to right). Keep in mind that 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.

Call-Graph:  A --> B --> C --> D
Run Code Online (Sandbox Code Playgroud)

  • 这不是对所提问题的回答. (3认同)