Ant exec resultproperty not working

I invoke the batch file using the Ant exec task and set the result to 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:

 if %errorlevel% neq 0 exit /b %errorlevel% 

When the script is executed, I always get abc as the value instead of the return value from the batch file. My batch file returns 2 and I need to stop the build

I want to do the following:

  • If the return value is <> 0, then I need to make a build failure that is not happening right now.

Any idea how I can get it to return a value and make Ant build fail?

+6
source share
2 answers

The exec resultproperty will capture the cmd interpreter exit code. The way you call exit in a batch file, although it does not end with cmd, only exits the script. The exit code from cmd will not be changed and will remain equal to zero. If you simply remove the \b option of the exit command, you will also terminate the interpreter and see that the exit code that you supply is being distributed.

 if %errorlevel% neq 0 exit %errorlevel% 

To crash, you can use the 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 can set failonerror="true" to the exec task to crash immediately.

+5
source

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

 Override ignored for property "BuildErrorCode" 

Essentially, when the ant property is set, its value cannot be changed. This SO question contains information.

A possible workaround is to not declare 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> ... 
+8
source

Source: https://habr.com/ru/post/893124/


All Articles