简体   繁体   English

将退出代码从ant返回到批处理文件

[英]return exit code from ant to a batch file

I have several ant tasks in my .bat file to execute. 我的.bat文件中有几个蚂蚁任务要执行。
My .bat file is like below: 我的.bat文件如下所示:

call ant -buildfile task.xml target1 调用ant -buildfile task.xml target1
call ant -buildfile task.xml target2 调用ant -buildfile task.xml target2

For each ant task, it will execute a java program and the program will return an exit code by using System.exit() . 对于每个ant任务,它将执行一个Java程序,并且该程序将通过使用System.exit()返回退出代码。

I know the exit code can be received by using resultproperty in ant configuration. 我知道可以通过在ant配置中使用resultproperty来接收退出代码。
How can I get the exit code in my .bat file from calling an ant task? 如何通过调用ant任务获取.bat文件中的退出代码?

You can try something like this: 您可以尝试如下操作:

1) In your ant task.xml: make it failing if the resultproperty is not 0. To do it, you can use the fail task with 1)在您的ant task.xml文件中:如果resultproperty不为0,则使其失败。为此,可以将fail任务

  • the condition that the resultProperty is not 0 条件resultProperty不为0
  • a status code equals to the status code returned from your java program 状态代码等于从Java程序返回的状态代码

Here is sample code: 这是示例代码:

<exec executable="cmd" resultproperty="javaReturnCode" ...>
    ...
</exec>

<fail message="java program execution failure" status="${javaReturnCode}">
   <condition>
      <not>
        <equals arg1="${javaReturnCode}" arg2="0"/>
      </not>
   </condition>
</fail>

2) In your batch file: the %errorlevel% contains the return code of the last command so something like this can work: 2)在您的批处理文件中:%errorlevel%包含最后一个命令的返回码,因此可以执行以下操作:

call ant -buildfile task.xml target1
IF NOT ERRORLEVEL 0 GOTO javaProgramErrorHandlingTarget1
call ant -buildfile task.xml target2
IF NOT ERRORLEVEL 0 GOTO javaProgramErrorHandlingTarget2
REM both ant targets exit normally so continue normal job
...
:javaProgramErrorHandlingTarget1
...
:javaProgramErrorHandlingTarget2
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM