Bug 42828 - Preserve return code of called batch files from exec task
Summary: Preserve return code of called batch files from exec task
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.7.0
Hardware: PC Windows XP
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Ant Notifications List
Depends on:
Reported: 2007-07-06 11:50 UTC by Paul Galbraith
Modified: 2009-04-06 12:40 UTC (History)
0 users

this is the wrapper I'm using (401 bytes, text/plain)
2007-07-06 11:52 UTC, Paul Galbraith

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Galbraith 2007-07-06 11:50:50 UTC
For the WinNT platform, if a batch script is called from the <exec> task
normally any batch error codes aren't propagated back to ant from the command
shell.  This causes a lot of headaches for those who expect or want an "exit /b
1" command to cause ant to fail the build process.  

I've been getting around this by using a very simple batch wrapper when I exec a
batch file.  It simply takes the return code from the wrapped batch file and
exits the shell with the same code.  

I'm thinking that it's not too big of a job to hack the exec task to
automatically use a wrapper such as this anytime a "*.cmd" or "*.bat" is set as
the executable on a winnt platform.  

In the interests of backwards compatibility this would be an option that
defaults off, but in the interests of saving traffic on the mail lists, and
making ant do what most people expect out of the box, I'd vote to have this on
by default...
Comment 1 Paul Galbraith 2007-07-06 11:52:59 UTC
Created attachment 20469 [details]
this is the wrapper I'm using
Comment 2 Steve Loughran 2007-07-09 03:28:52 UTC
Execution is one of the most troublesome spots of Ant; very brittle against java
version changes, and then there are platform issues. Even on the winnt platform,
you have 2000, xp, 2003 and vista to deal with, each of whom have subtle
differences in their batch language. 

I'd be reluctant to make any changes to how we exec ant stuff, and at the same
time, have always found that batch files seem to work right for me (e.g. if you
exec ant itself, it can pass its error code back)

leaving as open.
Comment 3 Paul Galbraith 2007-07-11 04:51:36 UTC
My experience with this is certainly limited to winxp, I don't even know if 
the "/b" option of the exit command is present on any other platform, but the 
winxp test case is simple.  <exec> a batch file with "exit /b 1" (w/ 
failonerror=true) should, imo, fail the ant build, but in my case it does not.
Comment 4 Peter Reilly 2007-07-16 04:09:13 UTC
I am not too sure that I understand this request.
Should the .cmd or .bat files have this exit /b
command in them if they want to return the status ?
I do not think that ant should automatically add
this (in any case how should one do this?).
It may be possible to write an ant task to
execute dos/windows batch file (by extending the
<exec> task).
Comment 5 William Darby 2009-04-06 12:40:19 UTC
In addition to the exit code from the ant.bat file, it appears that the Java invocation of Ant is not always returning non-zero when it reports BUILD FAILED. I have printed the %ERRORLEVEL% immediately after the call to %_JAVACMD% and the result is always 0. I believe this needs to be addressed as part of the fix to this larger issue.