Is it possible to specify value of property as pro

2020-03-18 07:08发布

问题:

I have a property file which is generated by my ant script itself during execution. And I want to access the value of properties from this generated property file.

For e.g.,

Generated Property File:

first.prop=abcd
second.prop=pqrs

and in script, I am trying to access it like this,

I am having a property name (that I want to access and is in generated property file) from some other property. That property is name.prop. so,

<echo message="${name.prop}"/>
<echo message="${${name.prop}}"/>

gives

first.prop
${${name.prop}}

as output respectively. What can be the solution over this?

回答1:

This is from the doc:

Nesting of Braces

In its default configuration Ant will not try to balance braces in property expansions, it will only consume the text up to the first closing brace when creating a property name. I.e. when expanding something like ${a${b}} it will be translated into two parts:

  • the expansion of property a${b - likely nothing useful.
  • the literal text } resulting from the second closing brace

This means you can't use easily expand properties whose names are given by properties, but there are some workarounds for older versions of Ant. With Ant 1.8.0 and the the props Antlib you can configure Ant to use the NestedPropertyExpander defined there if you need such a feature.

So it is not available easily. Workarounds (using <script /> or <macrodef /> can be found here in the official ant faq.



回答2:

I tried this, to get similar values<br/>
prop.properties contains:

a=val1
b=val2
c=val3
batch.props=a,b,c

<for list="${batch.props}" param="prop">
  <sequential>
    <echo>@{prop} is ${batch.@{prop}}</echo>
  </sequential>
</for>

Got Ans as below:
a is val1
b is val2
c is val3

Enjoy!