Replace a token with a variable using ANT
Published by Nicholas Dunbar on November 15th, 2014
You may be looking for the following:
How do you replace a string in a file using a property in ANT?
How do you replace a string using a variable in ANT?
There are a few ways to do this. First question is what do you mean by a variable? Do you mean a variable set in a property file like the following?
prompt> ls -l
-rw-r--r--@ 1 nicholasdunbar staff 2245 Nov 15 13:42 dev2.properties
The contents of dev2.properties being something like the following:
prompt> cat dev2.properties
crontabPath=/some/dir/
or do you mean a variable set in a property in your build file like the following?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<project default="main" basedir="."> | |
<!-- This is the example of creating a variable or inline property, directly in the build file--> | |
<property name="CRONTAB_PATH" value="/some/dir/"/> | |
</project> |
First we will talk about how to do it with the property file and then with the property tag/task.
Example 1 property file:
So lets say we have the following files:
prompt> ls -l
-rw-r--r--@ 1 nicholasdunbar staff 2245 Nov 15 13:42 dev2.properties
-rw-r--r-- 1 nicholasdunbar staff 369 Nov 13 18:39 crontab
prompt> cat dev2.properties
crontabPath=/some/dir/
prompt> cat crontab
00 00 * * * echo '25 hour record written by the crontab:' > @cronPath@crontab.log;
If we want to replace @cronPath@ in the file crontab with '/some/dir/' from dev2.properties we would use the following ANT task:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<copy file="crontab" | |
tofile="crontabTest.txt" | |
overwrite="true"> | |
</copy> | |
<replace file="crontabTest.txt" | |
propertyFile="dev2.properties"> | |
<replacefilter token="@cronPath@" | |
property="cronPath"/> | |
</replace> | |
<!-- | |
Code excerpt from http://boulderapps.co/replace-a-token-with-a-variable-using-ant | |
--> |
So we have generated a file called crontabTest.txt and replaced the tokens with our "variable" as we can see in the following:
prompt> cat crontabText.txt
00 00 * * * echo '25 hour record written by the crontab:' > /some/dir/crontab.log;
Example 2 using the property task to replace a token with a variable:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<property name="CRONTAB_PATH" value="/some/dir/"/> | |
<task name="generateCrontab"> | |
<copy file="crontab" | |
tofile="crontabTest.txt" | |
overwrite="true"> | |
<filterchain> | |
<replacetokens> | |
<token key="cronPath" value="${CRONTAB_PATH}"/> | |
</replacetokens> | |
</filterchain> | |
</copy> | |
</task> | |
<!-- | |
Code excerpt from http://boulderapps.co/replace-a-token-with-a-variable-using-ant | |
--> |
So if we were to look in the file crontabTest.txt we would see the following:
prompt> cat crontabText.txt
00 00 * * * echo '25 hour record written by the crontab:' > /some/dir/crontab.log;
As you can see the token has successfully been replaced. Thank you for reading. Please, leave a comment if this helped you or if you have a question.