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

Properties [Ant]

Property files

Properties can be set in property files. In the following example, there are two property files: build_1_properties and build_2_properties:
build_1.properties
prop3=drei
prop4=vier
prop5=fuenf
build_2.properties
prop1=one
prop2=two
prop3=three
Here's the build file (property_file.xml) that uses the two property files:
property_file.xml
<project default="print_some_properties" name="Use Property Files">

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

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

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

  <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>
Now, let's use property_file.xml with Ant:
ant -f property_file.xml
Buildfile: property_file.

print_some_properties:
     [echo] prop1: uno
     [echo] prop2: two
     [echo] prop3: drei
     [echo] prop4: vier
     [echo] prop5: fuenf

BUILD SUCCESSFUL
Total time: 0 seconds
As can be seen, as soon as a property is defined, it cannot be overridden. prop1 is first defined in property_file.xml with the value uno. Although prop1 is also set in build_2.properties with one, it retains the value uno. Likewise prop3 which is first defined in build_1.properties (=drei) is not overridden from build_2.properties.

Built in properties

Ant predefines the following properties: os.name, basedir, ant.file, ant.project.name, ant.java.version:
default_properties.xml
<project default="print_default_properties" name="Print default properties">

  <target name="print_default_properties" >
    <echo message="os.name:          ${os.name}"          />
    <echo message="basdir:           ${basedir}"          />
    <echo message="ant.file:         ${ant.file}"         />
    <echo message="ant.project.name: ${ant.project.name}" />
    <echo message="ant.java.version: ${ant.java.version}" />
  </target>
</project>
ant -f default_properties.xml
Buildfile: default_properties.xml

print_default_properties:
     [echo] os.name:          Windows XP
     [echo] basdir:           C:\ant_examples\ant
     [echo] ant.file:         C:\ant_examples\default_properties.xml
     [echo] ant.project.name: Print default properties
     [echo] ant.java.version: 1.4

BUILD SUCCESSFUL
Total time: 0 seconds