0

Am trying to do something like the below..

 <property name="INSTANCE_NAME" value="${PRODUCT5_JBOSS_INSTANCE}" />

PRODUCT5_JBOSS_INSTANCE-takes the value of that key name from a property file So when i give like the above my value for INSTANCE_NAME = product5-jboss.

But the above line is repeated multiple times for different products like

  <property name="INSTANCE_NAME" value="${PRODUCT6_JBOSS_INSTANCE}" />
   <property name="INSTANCE_NAME" value="${PRODUCT7_JBOSS_INSTANCE}" />

So inside value="${PRODUCT7_JBOSS_INSTANCE}" i would like to pass product7 dynamically like the below..

value="${${PRODUCT_NUMBER}_JBOSS_INSTANCE}" --But this doesnt work.Because two $ symbols doesnt work.My target now is PRODUCT_NUMBER should get the value as PRODUCT7 and then PRODUCT7_JBOSS_INSTANCE should get the value from file as product7-jboss

Can u help me with an approach for this

Felix Christy
  • 2,179
  • 1
  • 19
  • 32
raga
  • 1,483
  • 2
  • 13
  • 13

1 Answers1

2

You can resolve this using a macrodef, as shown in the answer to the question Variables from properties file in Ant.

Here is an example of the same for your case.

<project default="test">

    <!-- override on command line -->
    <property name="PRODUCT_NAME" value="PRODUCT5"/>

    <property file="test.properties"/>

    <macrodef name="property-read">
        <attribute name="name"/>
        <attribute name="from"/>
        <sequential>
            <property name="@{name}" value="${@{from}_JBOSS_INSTANCE}"/>
        </sequential>
    </macrodef>

    <target name="test">
        <echo>PRODUCT_NAME: ${PRODUCT_NAME}</echo>
        <property-read name="instance" from="${PRODUCT_NAME}"/>
        <echo>DYNAMIC PROPERTY VALUE: ${instance}</echo>
    </target>

</project>

Property definition:

$ cat test.properties
PRODUCT5_JBOSS_INSTANCE=product5.jboss.instance
PRODUCT7_JBOSS_INSTANCE=product7.jboss.instance

Output:

$ ant -DPRODUCT_NAME=PRODUCT7
Buildfile: C:\Users\sudocode\tmp\ant\build.xml

test:
     [echo] PRODUCT_NAME: PRODUCT7
     [echo] DYNAMIC PROPERTY VALUE: product7.jboss.instance

BUILD SUCCESSFUL
Total time: 0 seconds
$
$
$ ant -DPRODUCT_NAME=PRODUCT5
Buildfile: C:\Users\sudocode\tmp\ant\build.xml

test:
     [echo] PRODUCT_NAME: PRODUCT5
     [echo] DYNAMIC PROPERTY VALUE: product5.jboss.instance

BUILD SUCCESSFUL
Total time: 0 seconds
Community
  • 1
  • 1
ewan.chalmers
  • 16,145
  • 43
  • 60
  • Thanks for the approach provided.The above works perfectly if you specify the below line in build.xml When am trying to read the same from property file it doesnt work and gives value as {PRODUCT5_JBOSS_INSTANCE} – raga Jun 15 '12 at 10:37
  • It works for me. I updated the example to load properties. I think the result you get indicates that either you don't load the required property file, or that the property key is different from expected. – ewan.chalmers Jun 15 '12 at 10:48
  • Yes i have used the wrong property file..Now it works amazing.Thanks a lot – raga Jun 15 '12 at 11:24