Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail the ant script, if exec task fails

I am executing ant script in windows. In that consider that, i am executing dir command in exec task as below

<target name="dummy">
  <exec executable="cmd" failonerror="true">
    <arg line="/C DIRR"/>
  </exec>
  <exec executable="cmd" failonerror="true">
    <arg line="/C cd /d c:\temp"/>
  </exec>
</target>

Here I have given DIRR instead of DIR, this execution will fail. but the ant build is not failing. Its showing the error message as dirr is not recognised as internal or external command and the next command cd /d c:\temp also got executed. I want the ant script execution has to be stopped once error message comes.

I want to this script has to stop executing if error occurs in any one of the exec command. failonerror is also not helping. How to fail the ant build, if exec fails.

Note : I am using ant 1.8.2

like image 617
rashok Avatar asked Oct 31 '11 06:10

rashok


1 Answers

Please note, that there are two levels of execution here:

  • Ant calls cmd.exe.
  • cmd.exe executes DIRSS.

You see, if the the second step fails, this does not necessarily mean, that cmd.exe does propagate the error back to Ant. This might be more obvious if the mentally replace the well-known cmd.exe with something "innocent" like foo.exe.

So the next step is to explore, why the second step behaves differently on your machine than on the machines of the commentators of your question. After that riddle is solved, you can get back to the Ant question.

A first step might be this: Open a new shell window and try

> cmd /c dir
> echo %ERRORLEVEL%

> cmd /c dir nonexisting-directory
> echo %ERRORLEVEL%

> cmd /c dirr
> echo %ERRORLEVEL%

Also tell us the version of your OS.

like image 119
A.H. Avatar answered Nov 09 '22 08:11

A.H.