Mike G
Mike G

Reputation: 41

Overwriting ant property from custom ant task

So the overall problem is this:

We have multiple property files

<property file="prop1"/>
<property file="prop2"/>

prop1 contains a property looking like:

mg.prop = ${mg2.prop}

prop2 contains mg2.prop

mg2.prop = Hello

If they were in the same file and I queried mg.prop, I'd get "Hello" back. Since they are in separate files this does not work (I need to load prop1 before prop2!)

I wrote a custom ant task that does the following:

String resolved = resolveProperty(propertyName);
getProject().setProperty(propertyName, resolved);

If I run

log("Resolved property value = " + getProject().getProperty(propertyName)); 

Right after, I get the correct value.

However in the Ant script, if I do

<echo message="${mg.prop}"/> 

it shows me the original value.

Any thoughts on how to solve this?

Upvotes: 3

Views: 3829

Answers (3)

oers
oers

Reputation: 18714

You can also use the var task of ant-contrib to reset values.

From the doc:

The next example shows a property being set, echoed, unset, then reset:

<property name="x" value="6"/>
<echo>${x}</echo>   <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo>   <!-- will print 12 -->

Upvotes: 2

Mike G
Mike G

Reputation: 41

Here's how I ended up resolving this - I turfed the custom ant task.

I ended up concatenating all the properties files into one, in the reverse order of precedence.

So if I wanted properties from 3.properties to override those in 2.properties and 1.properties, I did the following:

<concat destfile="resolved.properties">
    <fileset file="1.properties" />
    <fileset file="2.properties" />
    <fileset file="3.properties" />
</concat>

<property file="resolved.properties"/>

Upvotes: 1

Russell Zahniser
Russell Zahniser

Reputation: 16364

From the Ant manual:

"Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables."

http://ant.apache.org/manual/Tasks/property.html

Depending on your situation, you might be able to accomplish what you want by loading prop1 twice, using loadproperties and a filter chain that the first time takes only lines not containing "{mg2.prop}", and the second time takes only lines that do contain it.

http://ant.apache.org/manual/Tasks/loadproperties.html http://ant.apache.org/manual/Types/filterchain.html#linecontains

Upvotes: 3

Related Questions