Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant exec resultproperty is not working

Tags:

build

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%

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?

like image 819
KK99 Avatar asked Jul 20 '11 06:07

KK99


2 Answers

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>
    ...
like image 175
Raghuram Avatar answered Oct 03 '22 13:10

Raghuram


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.

like image 36
martin clayton Avatar answered Oct 03 '22 13:10

martin clayton