Ant 简明教程
Ant - Using Token Filter
Ant 过滤器允许为当前项目设置一个标记过滤器。一个标记会用 @ 符号分隔,并且可以用属性文件读取。
Ant Filter allows to set a token filter for current project. A token is seperated by @ symbol and can be read using properties file as well.
Steps
-
Step 1 − Define a token using @@.
This is a sample text written in @year@.
-
Step 2 − Set the filter.
<filter token="year" value="2021"/>
-
Step 3 − Use the filter. All tasks will replace the occurence of @year@ with 2021.
<copy todir="${dest.dir}" filtering="true">
<fileset dir="${src.dir}"/>
</copy>
Filter Task Properties
以下是关键属性 −
Following are the key attributes −
Sr.No |
Attribute & Description |
1 |
token the token string without the separator chars (@) |
2 |
value the string that should be put to replace the token when the file is copied. |
3 |
filtersfile The file from which the filters must be read. This file must be a formatted as a property file. |
为了让 Filter 任务正常工作,必须提供标记和值或过滤器文件。
Either token and value to be provided or filtersfile to Filter task to work properly.
Example
使用以下内容创建一个 src 文件夹和 text1.txt 文件 −
Create a src folder with text1.txt file with following contents −
This is a sample text written in @year@.
使用以下内容创建 build.xml −
Create build.xml with the following content −
<?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>
Output
在上述构建文件上运行 Ant 会生成以下输出:
Running Ant on the above build file produces the following output −
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>
查看复制到 build 文件夹的文件的内容。
Verify the content of copied file to build folder.
This is a sample text written in 2021.