[ACCEPTED]-How can I allow an Ant property file to override the value set in another?-ant

Accepted answer
Score: 53

The initial problem with your set up is 7 that you've got build.properties and build-defaults.properties reversed.

Ant Properties are set 6 once and then can never be overridden. That's 5 why setting any property on the command 4 line via a -Dproperty=value will always override anything 3 you've set in the file; the property is 2 set and then nothing can override it.

So 1 the way you want this set up is:

<property file="build.properties" description="local build configuration overrides"/>
<property file="project.properties" description="Project configuration properties"/>
<property file="build-defaults.properties" description="default build configuration."/>

This way:

  1. Anything set at the command line takes precedence over build.properties
  2. Anything set in build.properties overrides other values
  3. etc. on down the line.
Score: 17

Actually ant properties may be overriden. See the documentation of the 21 property task:

Normally property values can not be changed, once 20 a property is set, most tasks will not 19 allow its value to be modified.

One of the 18 tasks that are able to override the property 17 value is script. Also any custom task may use 16 this backdoor. Other proposals are in question 15 Ant loadfile override property. This is against the spirit of ant and 14 usually unnecessary. But it's good to know 13 that, because I just had an opposite problem: why 12 the property value changed although it is immutable.

Here 11 is a sample target that uses script task 10 to change the value of a property. It shows 9 the basic methods to work with properties. All 8 methods are described in Ant Api which is 7 not available online. You need to download 6 the Ant Manual. In its api directory there is the api 5 documentation.

  <target name="t1">
    <property name="a" value="one" />
    <script language="javascript">
      sProp = project.getProperty("a");
      sProp = sProp.replace("e", "ly");
      project.setProperty("a", sProp);
      project.setNewProperty("a", "new value");
    </script>
    <property name="a" value="two" />
    <echo>a=${a}</echo>
  </target>

How to easily setup the script task? Making the script task running 4 with beanshell language is a bit tricky and non-trivial, but 3 it's explained in this answer. However as Rebse noted, using 2 javascript language is supported out of the box in 1 jdk 6.

Score: 6

Ant property can't be overwritten unless 4 using macro and javascript plug-in to do:

Step 3 1: define a macro function to overwrite 2 property

 <!--overwrite property's value-->
    <macrodef name="set" >
        <attribute name="name"/>
        <attribute name="value"/>
        <sequential>
            <script language="javascript">
                <![CDATA[
                project.setProperty("@{name}", "@{value}");
                ]]>
            </script>
        </sequential>
    </macrodef>

Step 2: use the macro in the ant 1 xml

<set
    name="your_target_property"
    value="your_value" or "${another_property}"     
</set>

More Related questions