In Ant, how can I dynamically build a property that references a property file

ant

I am using Input tasks to collect specific property values and I want to concatenate those into one property value that references my properties file.

I can generate the format of the property but at runtime it is treated as a string and not a property reference.

Example properties file:

# build.properties

# Some Server Credentials
west.1.server = TaPwxOsa
west.2.server = DQmCIizF
east.1.server = ZCTgqq9A

Example build file:

<property file="build.properties"/>
<target name="login">
 <input message="Enter Location:" addproperty="loc" />      
 <input message="Enter Sandbox:" addproperty="box" />
 <property name="token" value="\$\{${loc}.${box}.server}" />
 <echo message="${token}"/>
</target>

When I call login and provide "west" and "1" for the input values, echo will print ${west.1.server} but it will not retrieve the property value from the properties file.

If I hardcode the property value in the message:

<echo message="${west.1.server}"/>

then Ant will dutifully retrieve the string from the properties file.

How can I get Ant to accept the dynamically generated property value and treat it as a property to be retrieved from the properties file?

Best Answer

The props antlib provides support for this but as far as I know there's no binary release available yet so you have to build it from source.

An alternative approach would be to use a macrodef:

<macrodef name="setToken">
  <attribute name="loc"/>
  <attribute name="box"/>
  <sequential>
    <property name="token" value="${@{loc}.@{box}.server}" />
  </sequential>
</macrodef>
<setToken loc="${loc}" box="${box}"/>
Related Topic