开发者

Can Ant properties resolve other properties?

开发者 https://www.devze.com 2023-03-12 01:31 出处:网络
Can Ant properties set via properties file, resolve other properties from properties files? For example, I can do this:

Can Ant properties set via properties file, resolve other properties from properties files?

For example, I can do this:

<property name="prop1" value="in_test_xml1" />
<property name="prop2" value="${prop1}" />

and prop2 becomes "in_test_xml1". That's good.

But in this case, when u开发者_JS百科sing an input properties file:

prop1=sample_prop
prop2=${prop1}

prop2 is not set to "sample_prop"

So resolving properties from other properties only seems to work when the property doing the resolving is in the ant file itself.

Is this expected or am i missing something?


Ant does support in-file property expansion, see the Property File section in the manual for the Property task.

The following example shows properties getting resolved:

  • within a single properties file
  • from one properties file in another properties file
  • within a build file

First properties file:

$ cat props1.properties
prop1=world
prop2=hello ${prop1}

Second properties file:

$ cat props2.properties
prop3=goodbye ${prop1}

Build file:

<project default="test">
  <property file="props1.properties"/>
  <property file="props2.properties"/>
  <property name="prop4" value="${prop3}, good luck"/>
  <target name="test">
    <echo message="prop1 = ${prop1}"/>
    <echo message="prop2 = ${prop2}"/>
    <echo message="prop3 = ${prop3}"/>
    <echo message="prop4 = ${prop4}"/>
  </target>
</project>

Output:

$ ant
Buildfile: build.xml

test:
     [echo] prop1 = world
     [echo] prop2 = hello world
     [echo] prop3 = goodbye world
     [echo] prop4 = goodbye world, good luck

BUILD SUCCESSFUL
Total time: 0 seconds

Is there another kind of property resolution that is not working for you?

Edit

Following your comment, I now understand that you are using the -propertyfile command line option to specify a property file for Ant to load (rather than specifying the file in the buildfile itself, as I did above).

I did quick test with this and find that Ant 1.7.1 did not do in-file property expansion on files loaded using that command line option. But Ant 1.8.2 does.

This is Ant Bug 18732. You should be able to resolve by updating your version of Ant.


A simpler solution for this task would be the usage of Ant-contrib Tasks: Propertycopy.

From the manual:

<property name="org" value="MyOrg" />
<property name="org.MyOrg.DisplayName" value="My Organiziation" />
<propertycopy name="displayName" from="org.${org}.DisplayName" />

Sets displayName to "My Organiziation".
0

精彩评论

暂无评论...
验证码 换一张
取 消