Ant antcall a target that defines a property
Solution 1
According to the Apache Ant FAQ:
<target name="cond" depends="cond-if"/>
<target name="cond-if" if="prop1">
<antcall target="cond-if-2"/>
</target>
<target name="cond-if-2" if="prop2">
<antcall target="cond-if-3"/>
</target>
<target name="cond-if-3" unless="prop3">
<echo message="yes"/>
</target>
Note: <antcall> tasks do not pass property changes back up to the environment they were called from, so you wouldn't be able to, for example, set a result property in the cond-if-3 target, then do <echo message="result is ${result}"/> in the cond target.
In this respect, it is impossible to do what you want using antcall.
========== edit ===========
Try antcallback
: AntCallBack is identical to the standard 'antcall' task, except that it allows properties set in the called target to be available in the calling target.
http://antelope.tigris.org/nonav/docs/manual/bk03ch20.html
Sample code pasted from the above page:
<target name="testCallback" description="Test CallBack">
<taskdef name="antcallback" classname="ise.antelope.tasks.AntCallBack" classpath="${antelope.home}/build" />
<antcallback target="-testcb" return="a, b"/>
<echo>a = ${a}</echo>
<echo>b = ${b}</echo>
</target>
<target name="-testcb">
<property name="a" value="A"/>
<property name="b" value="B"/>
</target>
Solution 2
Another approach is to refactor your targets into macros. You are trying to use targets like functions and they are just not intended to be used that way. I typically write the bulk of my logic as macros, so that I can compose it more easily into more complicated macros. Then I write simple wrapper targets for the command-line entry points that I need.
Solution 3
Rather than using <antcall>
, why not just have target B depend on target A?
<target name="B" depends="A">
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>
alem0lars
Updated on July 09, 2022Comments
-
alem0lars almost 2 years
In Ant I want to define a target (called
A
) that define a property andantcall
it from another target (calledB
). I want that the targetB
, after antcalling the targetA
, can access the property defined in the targetA
.For example:
<target name="B"> <antcall target="A" inheritAll="true" inheritRefs="true" /> <echo>${myprop}</echo> </target> <target name="A"> <property name="myprop" value="myvalue" /> </target>
However it doesn't work and
<echo>${myprop}</echo>
doesn't printmyvalue
(I think because the propertymyprop
isn't defined inB
).Is there any way to do that?
-
SOUser about 13 yearsThat is
not
what OP wants. He wants to define the property in target A, and echo it in target B. Here, you define it in A and echo it in A. -
alem0lars about 13 yearsThanks. It was the issue. How do you implement that behaviour in a different way? I mean I would like to divide the logic of a target into subtargets
-
rajah9 about 13 yearsThank you for clarifying the question.
-
kevinarpe over 12 yearsAlso, you can try task 'AntCallBack' from Ant-Contrib package: ant-contrib.sourceforge.net/tasks/tasks/index.html
-
thekbb over 11 yearsThis is the best approach. ant properties are not variables, they're immutable. an antcall into the same build.xml always smells bad.