
- Apache ANT Tutorial
- ANT - Home
- ANT - Introduction
- ANT - Environment Setup
- ANT - Build Files
- ANT - Property Task
- ANT - Property Files
- ANT - Data Types
- ANT - Building Projects
- ANT - Build Documentation
- ANT - Creating JAR files
- ANT - Create WAR Files
- ANT - Packaging Applications
- ANT - Deploying Applications
- ANT - Executing Java code
- ANT - Eclipse Integration
- ANT - JUnit Integration
- ANT - Extending Ant
- Apache ANT Useful Examples
- ANT - Using Token
- ANT - Using Command Line Arguments
- ANT - Using If Else arguments
- ANT - Custom Components
- ANT - Listeners and Loggers
- Apache ANT Resources
- ANT - Quick Guide
- ANT - Useful Resources
- ANT - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.