Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an Ant Exec task return code not match ERRORLEVEL value?

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>
like image 408
cmcginty Avatar asked Oct 04 '22 23:10

cmcginty


1 Answers

The ANT manual states:

Errors and return codes

By default the return code of a <exec> is ignored; when you set failonerror="true" then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can set resultproperty 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 unless failifexecutionfails is set to false. 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.

like image 148
Mark O'Connor Avatar answered Oct 10 '22 02:10

Mark O'Connor