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

I am trying to use ant to enable or import tasks in order to use a common build file. I am fixated on extracting properties from the included file.

These are my broken patterns, trying to restore the "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 includes

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

as stated 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 the "child-property" from the parent?

+5
source share
3 answers

When you use the task antcall, a new Ant loop is launched for the antcall'ed task, but this does not affect the context of the caller:

; , , .., .

- :

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

.

, , .

+4

, 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>

parent-target:
 [echo] (From Child) i am child value
 [echo] Received params: myId=test
 [echo] (From Parent) i am child value
+1

Ant -contrib runtarget . , . , " ", Ant .

<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>

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

parent-target:

child.child-target:
     [echo] From Parent: xy
+1
source

All Articles