Ant - Using Token Filter



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.

Either token and value to be provided or filtersfile to Filter task to work properly.

Example

Create a src folder with text1.txt file with following contents −

This is a sample text written in @year@.

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

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>

Verify the content of copied file to build folder.

This is a sample text written in 2021.
Advertisements