Check if two antcalls are successfull

2019-07-04 02:30发布

I'm extremely new to ant script and i want to find out if my build is successful or not. my main target has two antcalls and i don't know how to check if they were successful or not, in order to evaluate the main target.

run all is my main target

<target name="run all">
<antcall target="Run basic configuration" />
<antcall target="Run custom configuration"/>

i want to add a fail condition for the "run all" target. Each target does check individually if they are successful, but I want to know if the target that calls the ant is unsuccessful in case those two fail. Also, if one fails does the other one get called?

标签: ant
1条回答
Root(大扎)
2楼-- · 2019-07-04 02:44

To determine if an antcall is successful requires a more careful definition of what success is. It can mean:

  1. The code executed without throwing an exception.
  2. The code did what you wanted it to do.

In the first case, you can wrap the execution of your antcall with a trycatch block to catch exceptions. The trycatch task is part of ant-contrib, but that is frequently used in Ant coding. You'd do:

<trycatch property="exception.message" reference="exception.object">
<try>
   <antcall target="targetName"/>
</try>
<catch>
   <!-- handle exception logic here -->
</catch>
<finally>
   <!-- do any final cleanup -->
</finally>
</trycatch>

In the second case, you need to pass back to the caller some state that indicates that the code did what you wanted it to do.

Note that the antcall task reloads the ant file (e.g., build.xml), so it can be an expensive operation. There are alternatives to using the antcall task:

  • Use the antcallback task (ant-contrib). The antcallback task specifically addresses the need to pass back data.
  • Use the depends attribute when defining a target to call dependent tasks.
  • Use the runtarget task (ant-contrib). With runtarget, all of your properties are passed and any properties you set in your target are available to the caller.
  • Use the macrodef task. It avoids reparsing of the ant file, can be passed attributes with default values, can be passed nested elements, and more. As such, this is the preferred solution in most cases.

In each of the cases above, just set return properties that you can inspect in the calling target to determine whether the called or dependent targets did what you expected them to do.

查看更多
登录 后发表回答