Ant antcall a target that defines a property

ant
32,046

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>
Share:
32,046
alem0lars
Author by

alem0lars

Updated on July 09, 2022

Comments

  • alem0lars
    alem0lars almost 2 years

    In Ant I want to define a target (called A) that define a property and antcall it from another target (called B). I want that the target B, after antcalling the target A, can access the property defined in the target A.

    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 print myvalue (I think because the property myprop isn't defined in B).

    Is there any way to do that?

  • SOUser
    SOUser about 13 years
    That 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
    alem0lars about 13 years
    Thanks. 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
    rajah9 about 13 years
    Thank you for clarifying the question.
  • kevinarpe
    kevinarpe over 12 years
    Also, you can try task 'AntCallBack' from Ant-Contrib package: ant-contrib.sourceforge.net/tasks/tasks/index.html
  • thekbb
    thekbb over 11 years
    This is the best approach. ant properties are not variables, they're immutable. an antcall into the same build.xml always smells bad.