Ant exec resultproperty is not working

痞子三分冷 提交于 2020-01-01 09:03:13

问题


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%

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

I want to do the following:

  1. If the return value is <> 0 then i have to make the build fail which is not happening now.

Any idea how I can get he return value and make the ant build fail?


回答1:


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%

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>

Or you could set failonerror="true" in the exec task to fail immediately.




回答2:


If you run the build script in verbose mode (ant -v), you will notice the line

Override ignored for property "BuildErrorCode"

Essentially once an ant property has been set its value cannot be changed. This SO question has details.

A possible workaround is to not declare the property.

    ...
    <!--property name="BuildErrorCode" value="abc"/-->
    <exec executable = "cmd" resultproperty="BuildErrorCode" failonerror="false" dir="D:\work">
        <arg value="/c"/>
        <arg value="cmake_cross_compile.bat"/>
    </exec>
    ...


来源:https://stackoverflow.com/questions/6758019/ant-exec-resultproperty-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!