Ant exec 结果属性不起作用 [英] Ant exec resultproperty is not working
问题描述
我正在使用 Ant exec
任务调用批处理文件并在 resultpropery
中设置结果.但是返回值永远不会出现在 Ant 中.下面是我的代码
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%
当脚本运行时,我总是将 abc
作为值而不是批处理文件的返回值.我的批处理文件现在返回 2
,我必须停止构建
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
我想做以下事情:
- 如果返回值为 <> 0,那么我必须使构建失败,而现在不会发生这种情况.
知道如何让他返回值并使蚂蚁构建失败吗?
Any idea how I can get he return value and make the ant build fail?
推荐答案
exec
任务 resultproperty
将捕获 cmd 解释器的退出代码.您在批处理文件中调用 exit 的方式并没有终止 cmd,它只是退出脚本.cmd 的退出代码将不受影响,并保持为零.如果您只是删除 exit 命令的 \b
选项,您也会终止解释器并看到您提供的退出代码传播.
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%
要失败,您可以使用 fail
任务,也许是这样的:
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>
或者您可以将 exec
任务中的 failonerror="true"
设置为立即失败.
Or you could set failonerror="true"
in the exec
task to fail immediately.
这篇关于Ant exec 结果属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!