Hi Folks, I've been working with Ant 1.4.1 and Ant 1.5.1 so far. In one project we've used both wrappers the Windows wrapper "ant.bat" and the Unix wrapper "ant" from a third party tool that uses the returncodes of these scripts to determine if the build has succeeded. While this works out with the Unix wrapper we have had the problem that the Windows wrapper always returned 0 (which normally indicates success) also in case of Ant errors. To get around this I've added 6 lines of code to the Windows wrapper ant.bat. Key to this solution is the fact that command.exe generates a return code different to 0 if the last command executed has failed. So we first need to store the return code of the JVM call and execute a command that fails like dir "!!!not_existent!!!" which normally generates return code 1. I've used the same pattern to overwind the setlocal - endlocal boundary. I would appreciate if you take a look to my solution and tell me your opinion about it. Regards, Martin @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ START: changes to ant.bat 1.5.1: ('+' marks a line to be inserted) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ##################################################### ### CHANGE 1: introduce variable ANT_ERROR and initialize it to 0 ##################################################### *** ant.bat Wed Oct 2 10:55:10 2002 --- ant.cmd Tue Oct 15 17:23:24 2002 *************** *** 3,8 **** --- 3,10 ---- REM Copyright (c) 2001-2002 The Apache Software Foundation. All rights REM reserved. + set ANT_ERROR=0 + if exist "%HOME%\antrc_pre.bat" call "%HOME%\antrc_pre.bat" if "%OS%"=="Windows_NT" @setlocal *************** ##################################################### ### CHANGE 2: set ANT_ERROR to 1 if Ant has failed ##################################################### *** 77,86 **** --- 79,90 ---- :runAnt "%_JAVACMD%" -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%" %ANT_OPTS% org.apache.tools.ant.Main %ANT_CMD_LINE_ARGS% + if not "%ERRORLEVEL%" == "0" set ANT_ERROR=1 goto end :runAntWithJikes "%_JAVACMD%" -classpath "%LOCALCLASSPATH%" "-Dant.home=%ANT_HOME%" "- Djikes.class.path=%JIKESPATH%" %ANT_OPTS% org.apache.tools.ant.Main % ANT_CMD_LINE_ARGS% + if not "%ERRORLEVEL%" == "0" set ANT_ERROR=1 goto end :end *************** ##################################################### ### CHANGE 3: lift ANT_ERROR over the setlocal - endlocal boundary ##################################################### ##################################################### ### CHANGE 4: return an errorcode <> 0 in case of Ant errors ##################################################### *** 88,95 **** --- 92,104 ---- set _JAVACMD= set ANT_CMD_LINE_ARGS= + if "%OS%"=="Windows_NT" set ERRORLEVEL=0 + if "%OS%"=="Windows_NT" ( if not "%ANT_ERROR%" == "0" dir "!!! not_existent!!!" >nul 2>&1 ) if "%OS%"=="Windows_NT" @endlocal + if "%OS%"=="Windows_NT" ( if not "%ERRORLEVEL%" == "0" set ANT_ERROR=1 ) :mainEnd if exist "%HOME%\antrc_post.bat" call "%HOME%\antrc_post.bat" + if not "%ANT_ERROR%" == "0" dir "!!!not_existent!!!" >nul 2>&1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ END: changes to ant.bat @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ the above has been testet under Windows NT 4 and Windows XP
Trouble is we have to support the win9x platform too, and they cant handle return codes properly, which is why the .bat file doesnt do it. I always recommend using the perl invoker from things like makefiles as it is consistent with return codes across all boxes (that need perl installed, of course). So, your patch looks like it works for you, but I dont think we could pull it in to the main codebase. Indeed, the batch and .sh files create such an inordinate amount of support calls we like to avoid touching them whenever they seem stable.
Created attachment 5022 [details] Portable error handling for ant.bat
I have run into the exact same problem. I also have a patch to ant/src/script/ant.bat that is more useful. It applies to CVS HEAD (currently 1.18.2.8) and has been tested to work on Windows 95, Windows NT 4 SP 6, Windows 2000 and Windows XP. Changelog: * src/script/ant.bat: Detects build failures and throws errors. It throws ERRORLEVEL 1, and stores Ant's return code in %ANT_ERROR%. * src/script/ant.bat: Removed extraneous label.
Created attachment 7911 [details] Updated portable error handling patch against ant/src/script/ant.bat 1.32
This bug makes ant on windows useless in automatic build systems, and this buggy behaviour at least deserves a proper hint/warning somewhere in the documentation.
Created attachment 13849 [details] updated ant.bat with proper errorlevel exit The other solutions seem overly complicated and set a random variable "ANT_ERROR". The proper way to exit a batch file with an error code is to use exit /b #. This works in win2k but not sure about 98.
This still seems to be a bug in Ant 1.6.5. The fix is easy. Anthill doesn't work without it. Not sure what other impact it may have. Vote for this bug.
Vince, the exit command doesn't take any options in Windows 9x nor NT4. My solution, unfortunately, is the most useful one. Trust me, I had no desire to write something as ugly as my patch. But portable error handling across Windows batch scripts is non-trivial. I no longer have any use for Ant on Windows, so I can't update my patch. But I'm sure that someone can figure out what's going on and port it to the most recent version in CVS.
*** Bug 36970 has been marked as a duplicate of this bug. ***