Saturday, April 20, 2013

Apache Ant: Executing a target in another target

As my current workplace we were using 'Jenkins' and 'Apache Ant' to automate and release the updated files of the respective projects once in a while, therefore I ended up trying out something on the 'Apache Ant' script. :P

So how to run a 'target' in another 'target'?
First of all, you probably have a 'target' like the following.
 <target name="building" description="Building a project">
 </target>
And then, you probably have a added a 'sequential' node, because you wanted to execute a series of tasks.
 <target name="building" description="Building a project">
  <sequential>
  </sequential>
 </target>
So how to execute another 'target' in the current 'target'? Well you can either add a 'antcall' or a 'antcallback' node. But what's the difference between the both of them? 'antcall' will execute another 'target' while 'antcallback' will execute another 'target' but at the same time allows you to return the 'property' that was created in the executed 'target'.
 <target name="building" description="Building a project">
  <sequential>
   <antcallback target="generateTimeStamp" return="timestamp"/>
   <antcall target="echoMsg"/>
  </sequential>
 </target>
Finally, you might end up with something like the following. I'm running 2 'target' in another 'target'. The 1st 'target' will create a 'property' and the 'property' will be passed into the 2nd 'target' and 'echo' the value of the 'property' as a message.
 <target name="building" description="Building a project">
  <sequential>
   <antcallback target="generateTimeStamp" return="timestamp"/>
   <antcall target="echoMsg"/>
  </sequential>
 </target>

 <target name="generateTimeStamp">
  <tstamp>
   <format property="timestamp" pattern="ddMMyyyyHHmmss"/>
  </tstamp>
 </target>
 
 <target name="echoMsg">
  <echo message="Generated Timestamp: ${timestamp}"/>
 </target>
* Click here to find out more about 'Jenkins'.
^ Click here to find out more about 'Apache Ant'.

1 comment:

  1. Ah nice, I didn't realize you could pass a property using the antcallback. Good to know, thanks.

    I have recently started tinkering with Jenkins. I'd like to start using it in my workflow. Could you possibly post an article touching a bit more on this subject?

    ReplyDelete