Reputation: 23
I have several ant tasks in my .bat file to execute.
My .bat file is like below:
call ant -buildfile task.xml target1
call 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().
I know the exit code can be received by using resultproperty in ant configuration.
How can I get the exit code in my .bat file from calling an ant task?
Upvotes: 2
Views: 8141
Reputation: 1180
You can use the failonerror
attribute of the java or exec tasks. For example:
<java
classpath="..."
classname="..."
failonerror="true"
>
...
</java>
This will cause ant to terminate with an exit value the same as the value passed to System.exit() from the Java app.
This value can then be handled in your batch script with ERRORLEVEL
. For example:
call ant -buildfile task.xml target1
IF NOT ERRORLEVEL 0 GOTO ...
call ant -buildfile task.xml target2
IF NOT ERRORLEVEL 0 GOTO ...
Upvotes: 0
Reputation: 28726
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
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:
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
...
Upvotes: 6