I have a batch script called from Ant exec task to compile some CSharp code. The batch script is structured in the following way
msbuild.exe %ARGS%
echo %ERRORLEVEL%
Now when the task is run in Ant, I get the following result:
[exec] Time Elapsed 00:00:09.48
[exec] 0
BUILD FAILED
C:\proj\build.xml:410: exec returned: 2
How is it possible that %ERRORLEVEL% is 0, but the Ant exec gets a return code of 2? Is this some default error code set if the command does not return a code? Ant docs show:
error code 2 means 'no such program',
But clearly my batch file is being executed correctly.
Update with Ant Code
<target name="build.csharp" if="isWindowsPlatform">
<exec executable="cmd.exe" failOnError="true">
<arg value="/c"/>
<arg value="build.csharp.bat" />
</exec>
</target>
The ANT manual states:
Errors and return codes
By default the return code of a
<exec>
is ignored; when you setfailonerror="true"
then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can setresultproperty
to the name of a property and have it assigned to the result code (barring immutability, of course).If the attempt to start the program fails with an OS dependent error code, then
<exec>
halts the build unlessfailifexecutionfails
is set tofalse
. You can use that to run a program if it exists, but otherwise do nothing.What do those error codes mean? Well, they are OS dependent. On Windows boxes you have to look at the documentation; error code 2 means 'no such program', which usually means it is not on the path. Any time you see such an error from any Ant task, it is usually not an Ant bug, but some configuration problem on your machine.
To obtain the program's return code you need to use the resultproperty attribute of the exec task.
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