Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve property value from include'd / import'ed project

Tags:

ant

I am trying to use ant's include or import tasks to use a common build file. I am stuck at retrieving properties from included file.

These are my non-working samples, trying to retrieve "child-property"

Using ant import

parent file

<?xml version="1.0" encoding="UTF-8"?>
<project name="parent" basedir=".">
    <import file="child.xml" />
    <target name="parent-target">
        <antcall target="child-target" />
        <echo message="(From Parent) ${child-property}" />
    </target>
</project>

child file

<?xml version="1.0" encoding="UTF-8"?>
<project name="child" basedir=".">
    <target name="child-target">
        <property name="child-property" value="i am child value" />
        <echo message="(From Child) ${child-property}" />
    </target>
</project>

output

parent-target:

child-target:
     [echo] (From Child) i am child value
     [echo] (From Parent) ${child-property}

Using ant include

parent file

<project name="parent" basedir=".">
    <include file="child.xml" />
    <target name="parent-target">
        <antcall target="child.child-target" />
        <echo message="(From Parent) ${child-property}" />
        <echo message="(From Parent2) ${child.child-property}" />
    </target>
</project>

child file

same as above

output

parent-target:

child.child-target:
     [echo] (From Child) i am child value
     [echo] (From Parent) ${child-property}
     [echo] (From Parent2) ${child.child-property}

How can I access "child-property" from parent?

like image 514
b10y Avatar asked Nov 30 '10 12:11

b10y


1 Answers

When you use the antcall task a new Ant cycle is started for the antcall'ed task - but that doesn't affect the context of the caller:

The called target(s) are run in a new project; be aware that this means properties, references, etc. set by called targets will not persist back to the calling project.

One way to make your simple example to work would be to change the first parent to:

<target name="parent-target" depends="child-target">
    <echo message="(From Parent) ${child-property}" />
</target>

Then the child-target will be executed in the parent context before the parent-target.

But, you may find that there are side affects to running the child task in the context of the parent that you don't want.

like image 149
martin clayton Avatar answered Nov 15 '22 10:11

martin clayton