views:

113

answers:

2

I have a set of nested Ant build files, and I need to control which properties are inherited by each "sub" task. I'm trying to define these as propertysets (to keep the code manageable) but these are not inherited by subtasks, unlike properties.

The example below demonstrates the problem, foo.* get copied into the middle project but not to the bottom project. If I define each property to be inherited explicitly, like bar.*, they get inherited by the bottom project too.

Is there any way to get a group of properties to inherit all the way down, in the same way individual properties do? Without rewriting the sub-processes, is there something else I could try?

[top.xml]

<?xml version="1.0"?>
<project name="test-top">
    <property name="foo.1" value="1"/>
    <property name="foo.2" value="2"/>
    <property name="bar.1" value="1"/>
    <property name="bar.2" value="2"/>

    <ant antfile="middle.xml" inheritall="false">
        <propertyset>
            <propertyref prefix="foo."/>
        </propertyset>
        <property name="bar.1" value="${bar.1}"/>
        <property name="bar.2" value="${bar.2}"/>
    </ant>
</project>

[middle.xml]

<?xml version="1.0"?>
<project name="test-middle">

    <echo>foo ${foo.1} ${foo.2}</echo>
    <echo>bar ${bar.1} ${bar.2}</echo>

    <ant antfile="bottom.xml" inheritall="false"/>
</project>

[bottom.xml]

<?xml version="1.0"?>
<project name="test-bottom">

    <echo>foo ${foo.1} ${foo.2}</echo>
    <echo>bar ${bar.1} ${bar.2}</echo>

</project>

[OUTPUT OF ant -f top.xml]

 [echo] foo 1 2
 [echo] bar 1 2
 [echo] foo ${foo.1} ${foo.2}
 [echo] bar 1 2
+1  A: 

In top.xml you can create a file with inheritable properties using <propertyfile> task.

Then you can load this file with <property file="..."/>in each of your submodules.

Alexander Pogrebnyak
+1  A: 

I think Alexander's solution is close. How about this though, doesn't need any change in middle.xml or bottom.xml.

The idea is to use the echoproperties task to 'unroll' the propertyset to individual properties, then to use that in the ant task call.

Before calling middle.xml, write the property set out using something like this:

<echoproperties destfile="myproperties.txt">
    <propertyset>
        <propertyref prefix="foo."/>
        <propertyref prefix="bar."/>
    </propertyset>
</echoproperties>

Then make the call to middle.xml:

<ant antfile="middle.xml" inheritall="false">
    <property file="myproperties.txt" />
</ant>

Properties supplied to the ant task inherit all the way down as you say, so you only need to change top.xml:

These properties become equivalent to properties you define on the command line. These are special properties and they will always get passed down, even through additional <ant> tasks with inheritall set to false (see above).

martin clayton
Thanks Martin, and Alexander.I had wondered about writing the properties to a file but hadn't seen any way to do it without explicitly re-listing all the properties I wanted to pass down (i.e., no better than the solution for `bar.*` above). I'd missed the "echoproperties" task due to it being listed under "Optional Tasks" rather than "Core Tasks" in the Ant documentation... now I know!
Paul Fenney