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"
<?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>
<?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>
parent-target:
child-target:
[echo] (From Child) i am child value
[echo] (From Parent) ${child-property}
<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>
same as above
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With