5

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?

b10y
  • 839
  • 9
  • 19

3 Answers3

4

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.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • "depends" somewhat works. but the problem with it is not being able to pass parameters to the task. I tried to define a global property in the parent, which can be retrieved in the child, but it limits the ability to pass different parameters for multiple calls. I will try "variable" tasks in ant-contrib in order to exchange variables. thx btw. – b10y Nov 30 '10 at 12:45
  • @b10y - Another option that comes to mind is to continue to use antcall but, in the child, write the properties you want to propagate back to the parent to a file using 'echo file='. Then, in the parent, read the 'saved' properties from that file using 'property file='. – martin clayton Nov 30 '10 at 12:48
1

It is a different approach but you may use macrodef.

parent.xml

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

child.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="child" basedir=".">
<macrodef name="child-macro">
    <attribute name="myid" default=""/>
    <sequential>
        <property name="child-property" value="i am child value" />
        <echo message="(From Child) ${child-property}" />
        <echo message="Received params: myId=@{myid}"/>
    </sequential>
</macrodef>
</project>

output

parent-target:
 [echo] (From Child) i am child value
 [echo] Received params: myId=test
 [echo] (From Parent) i am child value
Erol Ozcan
  • 31
  • 3
1

Ant-contrib's runtarget task has sufficiently solved my problem. It mignt not be suitable for others since it runs the target in parent's context. Guess there is no "one solution for all" for these kind of problems until Ant officially supports variables.

Parent

<project name="parent" basedir=".">
    <!-- insert necessary antcontrib taskdef here-->
    <include file="child.xml" />
    <target name="parent-target">
        <var name="x" value="x"/>
        <runtarget target="child.child-target"/>
        <echo message="From Parent: ${x}"/>
    </target>
</project>

Child

<project name="child" basedir=".">
    <property name="pre" value="childpre" />
    <target name="child-target">
        <var name="x" value="${x}y" />
    </target>
</project>

Output

parent-target:

child.child-target:
     [echo] From Parent: xy
b10y
  • 839
  • 9
  • 19