You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Evgeny Goldin <ge...@netvision.net.il> on 2003/12/16 16:22:14 UTC

"If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Hello,

It's being said on this list that "If Ant fails, it will return a non-zero exit
code". Unfortunately, it doesn't work for me:

1) I have a build.xml that fails (see below)
2) I call 'ant' from Perl script, using 'system()' call and expect to see
   a non-zero exit code from ant.
3) What I see *is* zero.

Ant v1.5.4, j2se v1.4.2, Windows 2000

Here's the code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ build.xml ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?xml version="1.0" ?>
<project name="Test" default="compile">
    <target name="compile">
        <fail message="AAAAAAAAA"/>
    </target>
</project>
~~~~~~~~~~~~~~~~~~~~~~~~~~ show_exit_code.pl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use strict;
use warnings;

( @ARGV == 1 ) or die "Param: command to run";
my $command   = $ARGV[ 0 ];
my $exit_code = ( system ( $command ) / 256 );
print "Exit code of running [$command] is [$exit_code]\n";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Running it:
>perl -w show_exit_code.pl ant
Buildfile: build.xml

compile:

BUILD FAILED
file:E:/Projects/AntFailure/build.xml:5: AAAAAAAAA

Total time: 2 seconds
Exit code of running [ant] is [0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Running script with something else that fails (to make sure it's not lying)
>perl -w show_exit_code.pl "java Opa"
Exception in thread "main" java.lang.NoClassDefFoundError: Opa
Exit code of running [java Opa] is [1]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you may see, the exit code of failing ant project is zero. Can somebody,
please, explain this to me ?






---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Evgeny Goldin <ge...@netvision.net.il>.
SB> err, well, yes - but you've also removed all hooks that clean up your
SB> environment.

Sure, removing the clean-up code does have it's side effects but let it be if it
returns me the correct exit code now.

SB> The real problem - AFAIU and I know virtually nothing about Windows
SB> batch programming - is that you can't store the exit code of Java and
SB> exit the batch file with that code at a later stage (at least not in a
SB> way that works on Win9x).

That would be the cleanest solution and I wouldn't mind using it. I bet ant.bat
developers wouldn't mind either :) Can anybody suggest how to do that (Windows
2000 if that matters) or it's a 100% dead end ?


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 16 Dec 2003, Evgeny Goldin <ge...@netvision.net.il> wrote:

> After I removed everything after first "%_JAVACMD%" in :runAnt label
> (to make sure "%_JAVACMD%" is the latest operation executed) - the
> exit code I received from running 'ant' has become 1 for failed
> projects.

err, well, yes - but you've also removed all hooks that clean up your
environment.

The real problem - AFAIU and I know virtually nothing about Windows
batch programming - is that you can't store the exit code of Java and
exit the batch file with that code at a later stage (at least not in a
way that works on Win9x).

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Evgeny Goldin <ge...@netvision.net.il>.
That's strange, my previous message doesn't seem to appear on list, so I'll
repeat it here. I think I've found the problem, taking a look on ant.bat:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:runAnt
"%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%"
org.apache.tools.ant.Main %ANT_ARGS% %ANT_CMD_LINE_ARGS%
goto end

:runAntWithJikes
"%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%"
"-Djikes.class.path=%JIKESPATH%" org.apache.tools.ant.Main %ANT_ARGS% %ANT_CMD_LINE_ARGS%
goto end

:end
set LOCALCLASSPATH=
set _JAVACMD=
set ANT_CMD_LINE_ARGS=

if "%OS%"=="Windows_NT" @endlocal

:mainEnd
if exist "%HOME%\antrc_post.bat" call "%HOME%\antrc_post.bat"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So my thought is following: since running Java isn't the latest operation in
the batch file - all following operations simply overwrite the exit code of java
process which is indeed 1 when it fails. But since following operations do succeed -
what is finally returned to the outside world is *their* exit code, not java's
(never was a batch-files pro, so this is only a guess).

After I removed everything after first "%_JAVACMD%" in :runAnt label (to make sure
"%_JAVACMD%" is the latest operation executed) - the exit code I received from running
'ant' has become 1 for failed projects.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re[2]: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Evgeny Goldin <ge...@netvision.net.il>.
SB> As you are using Perl anyway, try runant.pl instead.

Whops, I didn't notice that one. Thank you !

MG> my @xsl = ("java", "org.apache.tools.ant.Main.main");
MG> system(@xsl);

I think it'll also work. But for our needs - we'll just make sure nothing is
launched from batch file after the Ant's java process.





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 16 Dec 2003, Evgeny Goldin <ge...@netvision.net.il> wrote:

> 3) What I see *is* zero.

I think it doesn't work for the Windows batch file.

As you are using Perl anyway, try runant.pl instead.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Martin Gainty <mg...@hotmail.com>.
Evgeny
Try this
(from Perl)
my @xsl = ("java", "org.apache.tools.ant.Main.main");
system(@xsl);

Regards,
Martin

----- Original Message ----- 
From: "Evgeny Goldin" <ge...@netvision.net.il>
To: <us...@ant.apache.org>
Sent: Tuesday, December 16, 2003 10:22 AM
Subject: "If Ant fails, it will return a non-zero exit code" - doesn't hold
any more ?


>
> Hello,
>
> It's being said on this list that "If Ant fails, it will return a non-zero
exit
> code". Unfortunately, it doesn't work for me:
>
> 1) I have a build.xml that fails (see below)
> 2) I call 'ant' from Perl script, using 'system()' call and expect to see
>    a non-zero exit code from ant.
> 3) What I see *is* zero.
>
> Ant v1.5.4, j2se v1.4.2, Windows 2000
>
> Here's the code:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ build.xml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> <?xml version="1.0" ?>
> <project name="Test" default="compile">
>     <target name="compile">
>         <fail message="AAAAAAAAA"/>
>     </target>
> </project>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~ show_exit_code.pl
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> use strict;
> use warnings;
>
> ( @ARGV == 1 ) or die "Param: command to run";
> my $command   = $ARGV[ 0 ];
> my $exit_code = ( system ( $command ) / 256 );
> print "Exit code of running [$command] is [$exit_code]\n";
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~
>
> Running it:
> >perl -w show_exit_code.pl ant
> Buildfile: build.xml
>
> compile:
>
> BUILD FAILED
> file:E:/Projects/AntFailure/build.xml:5: AAAAAAAAA
>
> Total time: 2 seconds
> Exit code of running [ant] is [0]
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~
>
> Running script with something else that fails (to make sure it's not
lying)
> >perl -w show_exit_code.pl "java Opa"
> Exception in thread "main" java.lang.NoClassDefFoundError: Opa
> Exit code of running [java Opa] is [1]
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~
>
> As you may see, the exit code of failing ant project is zero. Can
somebody,
> please, explain this to me ?
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Evgeny Goldin <ge...@netvision.net.il>.
MG> You can call cmd.exe and send along the command line parms of the batch file
MG> (and get all of the env vars set properly)
MG> OR you can call the perl script I mentioned earlier

I ended up with the following code (in fact, it was surprisingly simple to save
the java process exit code in variable and exit with it later on):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%JAVA_HOME%\jre\bin\java ... org.apache.tools.ant.Main
set exit_code=%ERRORLEVEL%

... // some more commands

exit /b %exit_code%
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If such a code was used in original ant.bat - it's exit code returned to the
outside world was indeed 1 for the failed projects.

P.S.
DOS manual to the rescue !

"DOS Batch Language: A personal view"
http://gearbox.maem.umr.edu/~batch/batchtoc.htm
http://gearbox.maem.umr.edu/~batch/variable.htm
http://gearbox.maem.umr.edu/~batch/intrin1.htm#ifer




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Martin Gainty <mg...@hotmail.com>.
Evegny
You can call cmd.exe and send along the command line parms of the batch file
(and get all of the env vars set properly)
OR you can call the perl script I mentioned earlier
Your choice
Shalom,
Martin

----- Original Message ----- 
From: "Evgeny Goldin" <ge...@netvision.net.il>
To: "Ant Users List" <us...@ant.apache.org>
Sent: Tuesday, December 16, 2003 11:12 AM
Subject: Re: "If Ant fails, it will return a non-zero exit code" - doesn't
hold any more ?


>
> EG> It's being said on this list that "If Ant fails, it will return a
non-zero exit
> EG> code". Unfortunately, it doesn't work for me:
>
> Taking a look on ant.bat I see:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> :runAnt
> "%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%"
"-Dant.home=%ANT_HOME%" org.apache.tools.ant.Main %ANT_ARGS%
%ANT_CMD_LINE_ARGS%
> goto end
>
> :runAntWithJikes
> "%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%"
"-Dant.home=%ANT_HOME%" "-Djikes.class.path=%JIKESPATH%"
org.apache.tools.ant.Main %ANT_ARGS% %ANT_CMD_LINE_ARGS%
> goto end
>
> :end
> set LOCALCLASSPATH=
> set _JAVACMD=
> set ANT_CMD_LINE_ARGS=
>
> if "%OS%"=="Windows_NT" @endlocal
>
> :mainEnd
> if exist "%HOME%\antrc_post.bat" call "%HOME%\antrc_post.bat"
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> So my thought is following: since running Java isn't the latest operation
in
> the batch file - all following operations simply overwrite the exit code
of java
> process which is indeed 1 when it fails. But since following operations do
succeed -
> what is finally returned to the outside world is *their* exit code, not
java's
> (never was a batch-files pro, so this is only a guess).
>
> After I removed everything after first "%_JAVACMD%" in :runAnt label (to
make sure
> "%_JAVACMD%" is the latest operation executed) - the exit code I received
from running
> 'ant' has become 1 for failed projects.
>
> That's weird ... Did nobody notice this behavior before ?
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: "If Ant fails, it will return a non-zero exit code" - doesn't hold any more ?

Posted by Evgeny Goldin <ge...@netvision.net.il>.
EG> It's being said on this list that "If Ant fails, it will return a non-zero exit
EG> code". Unfortunately, it doesn't work for me:

Taking a look on ant.bat I see:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:runAnt
"%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%" org.apache.tools.ant.Main %ANT_ARGS% %ANT_CMD_LINE_ARGS%
goto end

:runAntWithJikes
"%_JAVACMD%" %ANT_OPTS% -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%" "-Djikes.class.path=%JIKESPATH%" org.apache.tools.ant.Main %ANT_ARGS% %ANT_CMD_LINE_ARGS%
goto end

:end
set LOCALCLASSPATH=
set _JAVACMD=
set ANT_CMD_LINE_ARGS=

if "%OS%"=="Windows_NT" @endlocal

:mainEnd
if exist "%HOME%\antrc_post.bat" call "%HOME%\antrc_post.bat"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So my thought is following: since running Java isn't the latest operation in
the batch file - all following operations simply overwrite the exit code of java
process which is indeed 1 when it fails. But since following operations do succeed -
what is finally returned to the outside world is *their* exit code, not java's
(never was a batch-files pro, so this is only a guess).

After I removed everything after first "%_JAVACMD%" in :runAnt label (to make sure
"%_JAVACMD%" is the latest operation executed) - the exit code I received from running
'ant' has become 1 for failed projects.

That's weird ... Did nobody notice this behavior before ?




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org