14

I'm pretty sure this is a simple question to answer and ive seen it asked before just no solid answers.

I have several properties files that are used for different environments, i.e xxxx-dev, xxxx-test, xxxx-live

The properties files contain something like:

server.name=dummy_server_name server.ip=127.0.0.1 

The template files im using look something like:

<...> <server name="@server.name@" ip="@server.ip@"/> </...> 

The above is a really primitive example, but im wondering if there is a way to just tell ANT to replace all tokens based on the properties file, rather than having to hardcode a token line for each... i.e

<replacetokens> <token key="server.name" value="${server.name}"/> <token key="server.ip" value="${server.ip}"/> </replacetokens> 

Any help would be great!

3 Answers 3

15

You can specify the properties file from which to read the list of tokens for the 'replace' task using replacefilterfile:

<replace file="input.txt" replacefilterfile="properties.txt"/> 

Similarly, in a filter chain, you can use 'replacetokens' propertyfile:

This will treat each properties file entry in sample.properties as a token/key pair:

<loadfile srcfile="${src.file}" property="${src.file.replaced}"> <filterchain> <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"> <param type="propertiesfile" value="sample.properties"/> </filterreader> </filterchain> </loadfile> 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to do this if you need properties to come from > 1 different property file? The only way I know is to create a new file which concatenates the file together - do you know of a better way?
@MarkRhodes - Sorry for the delay ... I can't think of anything that isn't equivalent to concatenating. It should be simple enough to do that though. Replacetokens doesn't support a file set for the replacements.
2

With the replace task by itself I missed the @ delimiters around tokens so I came up with the following solution. You can use any ant property in the template file

<project name="replace" default="replace"> <property file="build.properties" /> <target name="replace"> <!-- create temp file with properties --> <tempfile property="temp.replace" suffix=".properties"/> <echoproperties destfile="${temp.replace}" /> <!-- replace name=value with @name@=value --> <replaceregexp file="${temp.replace}" match="([^=]*)=" replace="@\1@=" byline="true" /> <!-- copy template and replace properties --> <copy file="template.txt" tofile="replaced.txt" /> <replace file="replaced.txt" replacefilterfile="${temp.replace}" /> </target> 

with a template

ANT home @ant.home@ ANT version @ant.java.version@ server name @server.name@ ip @server.ip@ 

this results in

ANT home /usr/share/ant ANT version 1.7 server name dummy_server_name ip 127.0.0.1 

Comments

0

Using fileset form ant-contrib you can read the tokens form properties file and replace multiple tokens over multiple files.

<project name="MyProject" default="replaceToklens" basedir="."> <property name="profilesProperties" value="${basedir}/environment.properties" /> <property name="build.dir" location="build"/> <!-- File to Load/ Accessable --> <property file="${profilesProperties}" /> <target name="replaceToklens"> <taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <pathelement location="${basedir}/ant-contrib-1.0b3.jar" /> </classpath> </taskdef> <mkdir dir="${build.dir}"/> <filter filtersfile="${profilesProperties}" /> <copy todir="${build.dir}" filtering="true" overwrite="true"> <fileset dir="${basedir}"> <!-- target/MyProject --> <include name="*.xml" /> <exclude name="build.xml" /> </fileset> </copy> </target> </project> 

folder structure:

ANT \_ build.xml \_ environment.properties \_ server.xml \_ build \_ server.xml [replaced with token value] 

In order to replace single toke use the following:

<replace file="build/server.xml" token="@keyName@" value="${keyValue}" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.