Ant - 使用 token 过滤器
Ant Filter 允许为当前项目设置一个 token 过滤器。 标记由 @ 符号分隔,也可以使用属性文件读取。
步骤
步骤 1 − 使用@@ 定义标记。
This is a sample text written in @year@.
步骤 2 − 设置过滤器。
<filter token="year" value="2021"/>
步骤 3 − 使用过滤器。 所有任务都将@year@ 的出现替换为 2021。
<copy todir="${dest.dir}" filtering="true">
<fileset dir="${src.dir}"/>
</copy>
过滤任务属性
以下是关键属性 −
| 序号 | 属性 & 描述 |
|---|---|
| 1 | token 不带分隔符 (@) 的 token 标记字符串 |
| 2 | value 复制文件时应放置以替换 token 令牌的字符串。 |
| 3 | filtersfile 必须从中读取过滤器的文件。 此文件必须格式化为属性文件。 |
要提供的 token 令牌和值或过滤器文件以使过滤任务正常工作。
示例
用 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>
V验证复制文件的内容到构建文件夹。
This is a sample text written in 2021.

