Why does Ant Exec return code not match ERRORLEVEL?

I have a script package from Ant exec to compile CSharp code. The script package is structured as follows.

msbuild.exe %ARGS% echo %ERRORLEVEL% 

Now that the task is running 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 Ant exec gets return code 2? Is this error code set by default if the command does not return a code? Ant docs show:

 error code 2 means 'no such program', 

But obviously my batch file is executing correctly.

Update Using Ant Code

 <target name="build.csharp" if="isWindowsPlatform"> <exec executable="cmd.exe" failOnError="true"> <arg value="/c"/> <arg value="build.csharp.bat" /> </exec> </target> 
+4
source share
3 answers

The ANT manual states:

Errors and Return Codes

By default, the return code <exec> ignored; when you set failonerror="true" , then any failure of the return code signal (OS dependent) will cause the build to fail. Alternatively, you can set the resultproperty name of the property and assign it a result code (of course, a ban on immutability).

If the attempt to start the program failed with an OS- failifexecutionfails error code, then <exec> stops the build if failifexecutionfails not set to false . You can use this to run the program if it exists, but otherwise do nothing.

What do these error codes mean? Well, they are OS dependent. In windows, you should look at the documentation; error code 2 means “no such program”, which usually means that it is not in the way. Whenever you see such an error from any Ant task, it is usually not an Ant error, but some configuration problem on your computer.

To get the program return code, you need to use the resultproperty exec task attribute.

+1
source

I started a bunch of permutations. I tried:

  • no final exit statement, just a command
  • exit %errorlevel%
  • exit /b %errorlevel%
  • @comspec /c exit %errorlevel%

The only way ant could see the correct error code was exit %errorlevel% . While the script is working correctly, this is a little annoying, since running it directly from the command shell will cause the shell to exit later. (Running the script through cmd /c works, but is cumbersome.)

I would love to come here from a DOS expert who could explain these results and expand them. Thanks.

+1
source

This article has an ANT wiki article, including a solution: http://wiki.apache.org/ant/AntOnWindows

+1
source

All Articles