René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Ant, properties, precedence and a surprise

Here's something for developers using Ant. Consider the following build.xml file:
build.xml
<project default="print_some_properties" name="Use Property Files">

  <property name="prop1" value="coke" />

  <property file="build_1.properties" />
  <property file="build_2.properties" />

  <property name="prop5" value="fanta" />

  <target name="print_some_properties" >
    <echo message="prop1: ${prop1}" />
    <echo message="prop2: ${prop2}" />
    <echo message="prop3: ${prop3}" />
    <echo message="prop4: ${prop4}" />
    <echo message="prop5: ${prop5}" />
  </target>

</project>
As can be seen, if Ant is invoked with this build.xml file, it tries to build the default target print_some_properties. This target does nothing except printing the values of the five properties prop1 through prop5.
This build.xml alse references two property files that set some properties. Here's their content:
build1.properties
prop3=apple
prop4=pear
prop5=banana
build2.properties
prop1=Chigaco
prop2=New York
prop3=San Francisco
Now, what do you think, will this print if Ant is invoked like so:
c:\> Ant
I didn't read the specification and I assumed that properties can and are overridden, that is, prop1 would be Chigaco (because it is overriden by build_2.properties, and that prop3 would be San Francisco. However, the output was:
Buildfile: build.xml

print_some_properties:
     [echo] prop1: coke
     [echo] prop2: New York
     [echo] prop3: apple
     [echo] prop4: pear
     [echo] prop5: banana

BUILD SUCCESSFUL
Total time: 0 seconds
I missed the following sentence in the Ant manual: Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definately not variable.