Apache Ant/依賴關係
外觀
depends 屬性可以包含在 target 標籤中,以指定此目標需要在執行之前執行另一個目標。可以指定多個目標,並用逗號隔開。
<target name="one" depends="two, three">
在此,目標“one”只有在名為“two”和“three”的目標先被執行後才會被執行。
以下是一個構建檔案的示例,該檔案按順序執行三個目標:first、middle 和 last。請注意,目標在構建檔案中的順序並不重要。
<?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>
示例輸出
Buildfile: build.xml
one:
[echo] Running One
two:
[echo] Running Two
three:
[echo] Running Three
BUILD SUCCESSFUL
Total time: 0 seconds
Ant 會跟蹤已經執行的目標,並跳過自上次在檔案中執行以來未發生更改的目標,例如
<?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>
將產生與上面相同的輸出 - 目標“one”不會被執行兩次,即使“two”和“three”目標都已執行,並且每個目標都指定對 one 的依賴關係。
類似地,Ant 會防止迴圈依賴 - 一個目標依賴於另一個目標,而另一個目標直接或間接地依賴於第一個目標。因此,構建檔案
<?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>
將產生錯誤
Buildfile: build.xml BUILD FAILED Circular dependency: one <- two <- one Total time: 1 second