Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Ant properties resolve other properties?

Tags:

properties

ant

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 using 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?

like image 325
glutz Avatar asked Jun 07 '11 14:06

glutz


2 Answers

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.

like image 66
sudocode Avatar answered Oct 07 '22 01:10

sudocode


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".
like image 35
Eugen Mihailescu Avatar answered Oct 07 '22 01:10

Eugen Mihailescu