Why does an Ant Exec task return code not match ERRORLEVEL value?
我有一个从Ant exec任务调用的批处理脚本来编译一些CSharp代码。批处理脚本的结构如下:
1 2 | msbuild.exe %ARGS% echo %ERRORLEVEL% |
现在,当任务在Ant中运行时,我得到以下结果:
1 2 3 4 5 |
%ERRORLEVEL%如何可能为0,但Ant exec的返回码为2?如果命令不返回代码,是否设置了一些默认错误代码?蚂蚁文档显示:
1 | error code 2 means 'no such program', |
但是显然我的批处理文件正在正确执行。
使用蚂蚁代码更新
1 2 3 4 5 6 | <target name="build.csharp" if="isWindowsPlatform"> <exec executable="cmd.exe" failOnError="true"> </exec> </target> |
ANT Wiki上有一篇有关此行为的文章,包括解决方案:
http://wiki.apache.org/ant/AntOnWindows
我进行了一堆排列。我试过了:
- 没有最终的退出语句,只有命令
-
exit %errorlevel% -
exit /b %errorlevel% -
@comspec /c exit %errorlevel%
唯一允许蚂蚁看到正确错误代码的技术是
我想从DOS专家那儿来,他可以解释这些结果并对其进行扩展。谢谢。
ANT手册指出:
Errors and return codes
By default the return code of a
is ignored; when you set failonerror="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
halts the build unless failifexecutionfails 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.
要获取程序的返回代码,您需要使用exec任务的resultproperty属性。