Neriyan
Neriyan

Reputation: 155

Dynamically build file name in Ant

I have a requirement where I want to dynamically load property files into Ant. I have property files like deploy_DEV.properties, deploy_QA.properties, deploy_UAT.properties and so on. The current environment (i.e. dev, QA or UAT is passed in as a parameter). Based on this, I want to load the appropriate properties file. my code is as follows:

<for list="${deployEnvs}" param="site">
   <property file="${deploy_@{site}}"/>
</for>

However this fails to work. Is there any easy way to accomplish this. I also have access to ant-contrib if needed.

Upvotes: 0

Views: 338

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

First, <for> requires a <sequential> nested under it.

Second, the ${ in ${deploy_@{site}} causes Ant to search for a property named deploy_.... Drop the surrounding ${ and } parts.

Third, the .properties file extension is missing.

Below are two property files and an Ant script that reads the property files and outputs their values.

deploy_DEV.properties

key1=value1

deploy_QA.properties

key2=value2

build.xml

<project name="ant-properties-files-with-for" default="run" basedir=".">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <target name="run">
        <property name="deployEnvs" value="DEV,QA"/>
        <for list="${deployEnvs}" param="site">
            <sequential>
                <property file="deploy_@{site}.properties"/>
            </sequential>
        </for>

        <echo>key1: ${key1}</echo>
        <echo>key2: ${key2}</echo>
    </target>
</project>

Output

run:
     [echo] key1: value1
     [echo] key2: value2

Upvotes: 1

Related Questions