0

For Ant Script I have following myBuild.properties file

p.buildpath=c:\production\build

d.buildpath=c:\development\build

I wrote following build.xml:

<?xml version="1.0"?>
<project name="Test Project" default="info">
    <property file="myBuild.properties"/>
      <target name="info">
        <input
            message="Please enter the Server Name(p: production, d: development)?"
            validargs="p,d"
            addproperty="do.Server"
        />
    <echo>Your Server type: ${do.Server} </echo>

    <property name="myserv.buildpath" value="${do.Server}.buildpath" />
    <property name="newProperty" value="${myserv.buildpath}" />

    <echo>New Property Value: ${newProperty}</echo>
    <!-- Following is an incorrect syntax -->
    <echo>Build Path: ${${newProperty}}</echo>

  </target>
</project>

When I run it using:

c:\>ant

I get following Output:

Buildfile: C:\build.xml

info: [input] Please enter the Server Name(p: production, d: development)? (p, d)
p
[echo] Your Server type : p
[echo] New Property Value: p.buildpath
[echo] Build Path: ${${newProperty}}

BUILD SUCCESSFUL
Total time: 2 seconds

I want to echo the "Build Path" value same as p.buildpath value. How is it possible to do in above case?

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
  • possible duplicate of [How to acess property within a property in ant](http://stackoverflow.com/questions/10703148/how-to-acess-property-within-a-property-in-ant) – Mark O'Connor Sep 08 '14 at 11:05

4 Answers4

3

Taking the macrodef advice of Ant Faq you need no Ant addons like antcontrib, it's foolproof :

<project>

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

  <property file="myBuild.properties"/>

   <input
    message="Please enter the Server Name(p: production, d: development)?"
    validargs="p,d"
    addproperty="do.Server"
   />
    <echo>Your Server type: ${do.Server}</echo>

   <cp_property name="myserv.buildpath" from="${do.Server}.buildpath"/>  

   <echo>$${myserv.buildpath} : ${myserv.buildpath}</echo>

</project>

output :

[input] Please enter the Server Name(p: production, d: development)? (p, d)
p
[echo] Your Server type: p
[echo] ${myserv.buildpath} : c:/production/build

btw. Within your propertyfile you need to change your path separator to either unix style '/' (when on windows ant will handle it correctly) or double '\\' , otherwise with :

p.buildpath=c:\production\build
d.buildpath=c:\development\build

you'll get something like :

[input] Please enter the Server Name(p: production, d: development)? (p, d)
p
[echo] Your Server type: p
[echo] ${myserv.buildpath} : c:productionbuild

Also see https://stackoverflow.com/a/25681686/130683 for a very similar problem solved with Props antlib

Community
  • 1
  • 1
Rebse
  • 10,307
  • 2
  • 38
  • 66
1

You can do it without additional tools this way (edited/revised version):

<project name="Test Project" default="info">
    <property file="myBuild.properties"/>
        <target name="info">
            <input
                message="Please enter the Server Name(p: production, d: development)?"
                validargs="p,d"
                addproperty="do.Server"
            />
        <echo>Your Server type: ${do.Server} </echo>

        <property name="buildstage.production" value="p.buildpath" />
        <property name="myserv.buildpath" value="${do.Server}.buildpath" />

        <echo>New Property Value: ${myserv.buildpath}</echo>

        <condition property="isProduction">
            <equals arg1="${buildstage.production}" arg2="${myserv.buildpath}" />
        </condition>

        <antcall target="production" />
        <antcall target="development" />
    </target>

    <target name="production" if="${isProduction}">
        <echo>Build Path: ${p.buildpath}</echo>
    </target>

    <target name="development" unless="${isProduction}">
        <echo>Build Path: ${d.buildpath}</echo>
    </target>
 </project>

Core idea is using a <conditional> task for a test on the selected property key and calling two tasks named production and development, where the first runs if the property isProduction is avaluated to true, the second if not (unlesss).

The condition is checked against a property named buildstage.production which is set to the property key p.buildpath as discriminator.

With "property key" I reference to the keys in the properties file. A hole bunch of "properties" here, could be confusing :)

By the way: you need to escape the backslashs in the property values as follows, otherwise they won't be echoed:

p.buildpath=c:\\production\\build
d.buildpath=c:\\development\\build
codefan-BK
  • 310
  • 2
  • 9
0

How it says in this post (http://ant.apache.org/faq#propertyvalue-as-name-for-property), without any external help, it's tricky.

With AntContrib (external task library) you can do <propertycopy name="prop" from="${anotherprop}"/>.

Anyway, there are other alternatives in the same post. Hope it helps you.

troig
  • 7,072
  • 4
  • 37
  • 63
0

There is a much simpler way that does not require macrodef or antcontrib:

<property name="a" value="Hello"/>

<property name="newProperty" value="a"/>
<loadresource property="b">
    <propertyresource name="${newProperty}"/>
</loadresource>

<echo message="a='${a}'"/>
<echo message="b='${b}'"/>
VGR
  • 40,506
  • 4
  • 48
  • 63