passing properties defined inside antcall target back to calling target

ant
22,793

Solution 1

Use antcallback from the ant-contrib jar instead of antcall

<target name="testCallback">
    <antcallback target="capitalize2" return="myKey">
    </antcallback>
    <echo>a = ${myKey}</echo>
</target>

<target name="capitalize2">
    <property name="myKey" value="it works"/> 
</target>

Output:

testCallback:

capitalize2:
     [echo] a = it works

BUILD SUCCESSFUL

Solution 2

One approach is to write out a property to a temp file using "echo file= ...." or PropertyFile task. Then read the property back in where required. Kludge but works.

Solution 3

Ant tasks are all about stuff goes in, side effect happens. So trying to program in terms of functions (stuff goes in, stuff comes out) is going to be messy.

That said what you can do is generate a property name per invocation and store the result value in that property. You would need to pass in a indentifier so you do not end up trying to create copies of the same property. Something like this:

<target name="default">
  <property name="key" value="world"/>
  <antcall target="doSomethingElse">
     <param name="param1" value="${key}"/>
  </antcall>
  <echo>${result-${key}}</echo>
</target>
<target name="doSomethingElse">
   <property name="hello-${param1}" value="it works?"/>
</target>

But I believe the more typical approach -instead of antcalls- is to use macros. http://ant.apache.org/manual/Tasks/macrodef.html

Share:
22,793
hhamalai
Author by

hhamalai

I &lt;3 Python, JS, &amp; AWS

Updated on December 04, 2020

Comments

  • hhamalai
    hhamalai over 3 years

    I'm rather new to Ant but I have experienced it's quite good pattern to create generic ant targets which are to be called with antcall task with varying parameters.

    My example is compile target, which compiles multiple systems using complex build command which is a bit different for each system. By using pattern described above it's possible not to create copy paste code for that compile command.

    My problem here is, that I'm not aware of any way to pass return value (for example the return value of compiler) back to target which called the antcall task. So is my approach pathological and it's simply not possible to return value from antcall task or do you know any workaround?

    Thanks,

  • martin clayton
    martin clayton over 13 years
    Doesn't work. From the antcall docs: "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."