跳轉到內容

Apache Ant/依賴關係

來自華夏公益教科書,開放的書籍,為開放的世界

depends 屬性可以包含在 target 標籤中,以指定此目標需要在執行之前執行另一個目標。可以指定多個目標,並用逗號隔開。

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

在此,目標“one”只有在名為“two”和“three”的目標先被執行後才會被執行。

使用 depends 屬性的示例

[編輯 | 編輯原始碼]

以下是一個構建檔案的示例,該檔案按順序執行三個目標: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

下一章下一章食譜

華夏公益教科書