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.
广告