Uptodate

說明

如果目標檔案或目標檔案組比來源檔案或來源檔案組更新,則設定屬性。使用 srcfile 屬性指定單一來源檔案。使用巢狀的 <srcfiles> 元素指定來源檔案組。這些是 FileSet,而使用巢狀的 <mapper> 元素指定多個目標檔案。

預設情況下,如果來源檔案的時間戳記不比對應目標檔案的時間戳記新,則屬性的值設定為 true。您可以透過指定 value 屬性,將值設定為非預設值。

如果使用 <srcfiles> 元素,但未同時指定 <mapper> 元素,則預設行為是使用 合併對應器,其中 to 屬性設定為 targetfile 屬性的值。

通常,此任務用於設定屬性,這些屬性有助於避免目標執行取決於指定檔案的相對舊有程度。

參數

屬性 說明 必要
property 要設定的屬性名稱。
value 要將屬性設定為的值。 否;預設為 true
srcfile 要針對目標檔案檢查的檔案。 是,除非存在巢狀的 <srcfiles><srcresources> 元素。
targetfile 我們要確定其狀態的檔案。 是,除非存在巢狀的 <mapper> 元素。

指定為巢狀元素的參數

srcfiles

巢狀的 <srcfiles> 元素是 fileset,允許您指定一組要針對目標檔案檢查的檔案。

注意:您可以指定 srcfile 屬性或巢狀的 <srcfiles> 元素,但不能同時指定。

請注意,任務將完全忽略看似與 srcfiles fileset 匹配的任何目錄,它只會考慮一般檔案。如果您需要也適用於目錄的邏輯,請使用巢狀的 srcresources 元素和 dirset(例如)。

srcresources

自 Apache Ant 1.7 起

巢狀的 <srcresources> 元素是一個 聯合,允許您指定要對目標檔案進行檢查的資源集合。

mapper

巢狀的 <mapper> 元素允許您指定一組目標檔案,以檢查它們是否相對於一組來源檔案是最新的。

mapper to 屬性與目標檔案相關,或與巢狀 srcfiles 元素的 dir 屬性相關。

自 Ant 1.6.3 起,可以使用 filenamemapper 類型來取代 mapper 元素。

範例

如果 ${deploy}/xmlClasses.jar 檔案比 ${src}/xml 目錄中的任何 DTD 檔案都更新,則將屬性 xmlBuild.notRequired 設為 true

<uptodate property="xmlBuild.notRequired" targetfile="${deploy}\xmlClasses.jar">
  <srcfiles dir="${src}/xml" includes="**/*.dtd"/>
</uptodate>

這可以寫成

<uptodate property="xmlBuild.notRequired">
  <srcfiles dir= "${src}/xml" includes="**/*.dtd"/>
  <mapper type="merge" to="${deploy}\xmlClasses.jar"/>
</uptodate>

也可以寫成

然後可以在 <target> 標籤的 unless 屬性中使用 xmlBuild.notRequired 屬性,以有條件地執行該目標。例如,執行下列目標

<target name="xmlBuild" depends="chkXmlBuild" unless="xmlBuild.notRequired">
  ...
</target>

將會先執行 chkXmlBuild 目標,其中包含 <uptodate> 任務,用來判斷是否設定 xmlBuild.notRequired。接著檢查 unless 屬性中指定的屬性是否已設定/未設定。如果已設定(即 jar 檔案是最新的),則不會執行 xmlBuild 目標。

下列範例顯示檢查單一來源檔案與單一目標檔案

<uptodate property="isUpToDate"
          srcfile="/usr/local/bin/testit"
          targetfile="${build}/.flagfile"/>

如果 /usr/local/bin/testit 不比 ${build}/.flagfile 新,則將屬性 isUpToDate 設為 true

下列範例顯示如何使用相對應的 mapper。

<uptodate property="checkUptodate.uptodate">
  <srcfiles dir="src" includes="*"/>
  <mapper type="merge" to="../dest/output.done"/>
</uptodate>
<echo message="checkUptodate result: ${checkUptodate.uptodate}"/>

前一個範例可能會有點令人困惑,因此最好使用絕對路徑

<property name="dest.dir" location="dest"/>
<uptodate property="checkUptodate.uptodate">
  <srcfiles dir="src" includes="*"/>
  <mapper type="merge" to="${dest.dir}/output.done"/>
</uptodate>