Sunday, March 30, 2008

Generic retry target in ANT

I think the latest ANT trunk code has a new retry target. I wrote my own retry target using ANT 1.7 and ANT-contrib. It's kinda hackish, but it works well.
<target name="retry-task">
<var name="task.return" value="false" />
<for list="0,1,2" param="retry">
<sequential>
<if>
<equals arg1="${task.return}" arg2="false" />
<then>
<trycatch>
<try>
<var name="task.return" value="true" />
<antcall target="${retry-target}"/>
</try>
<catch>
<var name="task.return" value="false" />
</catch>
</trycatch>
<echo message="return ${task.return}" />
</then>
<else>
</else>
</if>>
</sequential>
</for>
<if>
<equals arg1="${task.return}" arg2="false" />
<then>
<fail message="${retry-target} failed after 3 retries" />
</then>
</if>
</target>

To use this target:
<antcall target="retry-task">
<param name="retry-target" value="some-other-ant-task" />
</antcall>

3 comments:

Servidores said...
This comment has been removed by a blog administrator.
Julian said...

Hi there. Interesting post.

I don't use antcall or it's cousin 'call' in Nant in build files, but if you do, you want to make sure that the target that you call has no dependencies - well, if it does, you want to be happy with them being run again and again :)

More on my post at:
http://www.build-doctor.com/2008/02/antcall-is-evil.html

Best,

Julian.

Anonymous said...

It is certainly interesting for me to read this article. Thanks for it. I like such topics and everything connected to them. I would like to read a bit more soon.