Ant - 使用令牌過濾器



Ant 過濾器允許為當前專案設定令牌過濾器。令牌由 @ 符號分隔,也可以使用屬性檔案讀取。

步驟

  • 步驟 1 − 使用 @@ 定義令牌。

This is a sample text written in @year@.
  • 步驟 2 − 設定過濾器。

<filter token="year" value="2021"/>
  • 步驟 3 − 使用過濾器。所有任務都將用 2021 替換 @year@ 的出現。

<copy todir="${dest.dir}" filtering="true">
   <fileset dir="${src.dir}"/>
</copy>

過濾任務屬性

以下是主要屬性 −

序號 屬性與說明
1

令牌

不帶分隔符 chars (@) 的令牌字串

2

在複製檔案時用於替換令牌的字串。

3

filtersfile

必須從中讀取過濾器的檔案。此檔案必須格式化為屬性檔案。

令牌和值都提供給過濾器任務才能正常工作。

示例

使用以下內容建立一個包含 text1.txt 檔案的 src 資料夾 −

This is a sample text written in @year@.

使用以下內容建立 build.xml −

<?xml version="1.0"?>
<project name="sample" basedir="." default="copy">
   <property name="src.dir" value="src"/>
   <property name="dest.dir" value="build"/>
   <target name="copy">
      <filter token="year" value="2021"/>
      <copy todir="${dest.dir}" filtering="true">
         <fileset dir="${src.dir}"/>
      </copy>
   </target>
</project>

輸出

在上述構建檔案上執行 Ant 會產生以下輸出 −

F:\tutorialspoint\ant>ant
Buildfile: F:\tutorialspoint\ant\build.xml

copy:
   [copy] Copying 1 file to F:\tutorialspoint\ant\build

BUILD SUCCESSFUL
Total time: 1 second

F:\tutorialspoint\ant>

驗證複製到構建資料夾的檔案內容。

This is a sample text written in 2021.
廣告