ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vincent Massol" <vmas...@octo.com>
Subject [PATCH] (WAS: Re: <exec> problem with exit value)
Date Sun, 25 Mar 2001 07:39:46 GMT
Hi,
I'd like to propose a patch to ant.bat so that ant.bat returnes an exit code
if an error ocurred during the build. This is very useful for example when
calling a ant build using the <exec failonerror="true"> task from another
ant build file. Otherwise, the calling build file does not exit. See the
mails below for an explanation.

I have come up with 2 possibilities to do this :

1) Solution 1 :

The idea is to check for a ERRORLEVEL after the java ant command line and
use the shell exit command. However, when used on a top level shell, the
exit will close the DOS windows, so I introduced an environment variable
ANT_EXITONFAILURE to control the exit or not.

A simple example on how to use it :

        <exec dir="..." executable="ant.bat" failonerror="true">
            <env key="ANT_EXITONFAILURE" value="true"/>
[...]
        </exec>

The patch to ant.bat :

80a81
> set _ERRORLEVEL=%ERRORLEVEL%
84a86
> set _ERRORLEVEL=%ERRORLEVEL%
90a93,101
> if "%ANT_EXITONFAILURE%" == "" goto almostEnd
> if "%_ERRORLEVEL%" == "0" goto almostEnd
> if not "%OS%"=="Windows_NT" goto mainEnd1
> @endlocal
> :mainEnd1
> if exist "%HOME%\antrc_post.bat" call "%HOME%\antrc_post.bat"
> exit %_ERRORLEVEL%
>
> :almostEnd
97d107
<

2) Solution 2 (which I personally prefer) :

It is a variant of solution 1. Create another batch file at the same
location as ant.bat. Let's call it antExit.bat. This batch file simply calls
ant.bat and does an "exit %ERRORLEVEL% if the errorlevel is not 0.  Using it is very simple
:

        <exec dir="..." executable="antExit.bat" failonerror="true">
[...]
        </exec>

antExit.bat :
----------
@echo off

if not "%OS%"=="Windows_NT" goto win9xStart
:winNTStart
@setlocal

rem On NT/2K grab all arguments at once
set ANT_CMD_LINE_ARGS=%*
goto doneStart

:win9xStart
rem Slurp the command line arguments.  This loop allows for an unlimited number of 
rem agruments (up to the command line limit, anyway).

set ANT_CMD_LINE_ARGS=

:setupArgs
if %1a==a goto doneStart
set ANT_CMD_LINE_ARGS=%ANT_CMD_LINE_ARGS% %1
shift
goto setupArgs

:doneStart
rem This label provides a place for the argument list loop to break out 
rem and for NT handling to skip to.

call ant.bat %ANT_CMD_LINE_ARGS%
if %ERRORLEVEL% == "0" goto end
exit %ERRORLEVEL%

:end
set ANT_CMD_LINE_ARGS=

if not "%OS%"=="Windows_NT" goto mainEnd
:winNTend
@endlocal

:mainEnd
----------

Tell me what you think, if you have even simpler solution and if this can be
included as part of the Ant distribution.
Thanks
Vincent Massol

----- Original Message -----
From: "Thomas Christen" <chr@active.ch>
To: <ant-dev@jakarta.apache.org>
Sent: Saturday, March 24, 2001 6:38 PM
Subject: AW: <exec> problem with exit value


> I've got a similar problem. Fact is, WindowsNT returns exit codes only
from
> within an executable. Batch and commands are not real executables. My
> workaround was to modify the excute (I wraped it in an additional Task) in
> the way, that the log methode scans the message and looks for a String
EXIT
> nn (nn would be the exitcode). You then must ensure, that at the end of
the
> command an "@ECHO EXIT %ERRORLEVEL%" (%ERRORLEVEL% represents your
internal
> stored errorcode), is inserted. If an EXIT nn has been detected by exec,
it
> sets an internal variable with the value (nn) and if the task has done its
> work, checks again the exitcode scanned before ... Easy to implement but
> somone might have a better solution.
>
> Thomas
>
> > -----Ursprüngliche Nachricht-----
> > Von: Vincent Massol [mailto:vmassol@octo.fr]
> > Gesendet am: Freitag, 23. März 2001 10:21
> > An: ant-dev@jakarta.apache.org
> > Betreff: <exec> problem with exit value
> >
> > Hi,
> >
> > I am trying to use the exec task to call another ant build file in some
> > other directory (I don't want ot use the <ant> task to do that because I
> > don't want to inherit any of the properties from the calling build.xml).
> > However, when the called ant build file fails, the calling build file
does
> > not generate a "build failed" and does not stop.
> >
> > My settings :
> >
> > 1/ I have used the failonerror attribute in the exec task :
> >
> >         <exec dir="${out.sample.build.dir}" executable="ant.bat"
> > failonerror="true">
> >             <arg line="tests_all"/>
> >         </exec>
> >
> > 2/ When I manually run the called ant build file it fails and if I type
> > "echo %ERRORLEVEL%" at the DOS prompt (I am on Windows 2000), it
correctly
> > says "1"
> >
> > 3/ For confirmation, I modified the Ant Main.java class to output the
exit
> > value in the main() method and it effectively does a "System.Exit(1)"
> >
> > 4/ I modified Execute.waitFor() method as follows :
> >
> >     protected void waitFor(Process process) {
> >         try {
> >             process.waitFor();
> >             System.out.println("Process return value = " +
> > process.exitValue());
> >             setExitValue(process.exitValue());
> >         } catch (InterruptedException e) {}
> >     }
> >
> > and it prints "Process return value = 0" !
> >
> > I am stuck ...
> > Can you help me to debug this problem ?
> >
> > Thank you.
> > Vincent.
> >
>
>
>


Mime
View raw message