I am calling a batch file using an Ant exec
task and setting the result in resultpropery
. But the return value never comes to Ant. Below is my code
<property name="BuildErrorCode" value="abc"/>
<exec executable="cmd" resultproperty="BuildErrorCode" failonerror="false"
dir="C:\workspace\build\">
<arg value="/c"/>
<arg value="cmake_cross_compile.bat"/>
</exec>
<echo message="Error Code:=${BuildErrorCode}" />
I exit my batch script by:
if %errorlevel% neq 0 exit /b %errorlevel%
When the script runs, i always get abc
as value instead of return value from batch file. My batch file returns 2
for now and I have to stop the build
I want to do the following:
Any idea how I can get he return value and make the ant build fail?
If you run the build script in verbose mode (ant -v
), you will notice the line
Override ignored for property "BuildErrorCode"
Essentially once an ant property has been set its value cannot be changed. This SO question has details.
A possible workaround is to not declare
the property.
...
<!--property name="BuildErrorCode" value="abc"/-->
<exec executable = "cmd" resultproperty="BuildErrorCode" failonerror="false" dir="D:\work">
<arg value="/c"/>
<arg value="cmake_cross_compile.bat"/>
</exec>
...
The exec
task resultproperty
will capture the exit code of the cmd interpreter. The way you are calling exit in the batch file though is not terminating cmd, it is only exiting the script. The exit code from cmd will be unaffected, and stay zero. If you simply remove the \b
option of the exit command you will terminate the interpreter as well and see the exit code you supply propagated.
if %errorlevel% neq 0 exit %errorlevel%
To fail, you could use a fail
task, perhaps something like this:
<fail message="cmake_cross_compile.bat exited non-zero">
<condition>
<not>
<equals arg1="${BuildErrorCode}" arg2="0"/>
</not>
</condition>
</fail>
Or you could set failonerror="true"
in the exec
task to fail immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With