Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I allow an Ant property file to override the value set in another?

Tags:

ant

I have an ant file that does the following:

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

I want to have defaults set in build-defaults.properties (which is checked in to SCM) but allow developers to override values in a local build.properties so that they can work with local paths.

The problem is, it doesn't seem to be working; I've set this up, created an override in build.properties, but the value of my path remains the one set in build-defaults.properties. How do I accomplish this?

like image 911
Chris R Avatar asked Nov 17 '10 15:11

Chris R


People also ask

What is Ant propertyfile?

Apache Ant provides an optional task for editing property files. This is very useful when wanting to make unattended modifications to configuration files for application servers and applications. Currently, the task maintains a working property file with the ability to add properties or make changes to existing ones.

How do I set system properties in Ant?

You can declare system properties in the xml with <sysproperty key="key" value="value"/> . This can only be used on java tasks.

What is Ant Basedir?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.


2 Answers

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

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

So 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.
like image 60
Tim Visher Avatar answered Nov 12 '22 22:11

Tim Visher


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

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

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

Here is a sample target that uses script task to change the value of a property. It shows the basic methods to work with properties. All methods are described in Ant Api which is not available online. You need to download the Ant Manual. In its api directory there is the api 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 with beanshell language is a bit tricky and non-trivial, but it's explained in this answer. However as Rebse noted, using javascript language is supported out of the box in jdk 6.

like image 35
Jarekczek Avatar answered Nov 13 '22 00:11

Jarekczek