A batch file is executed via a cmd.exe command shell, either the
current one or a new one. When Ant's <exec ... failonerror="true">
is used, a new shell is spawned which executes the batch file.
As usual, the process exit code is used to determine if the shell
returned successfully (==0) or failed (!=0).
Now, the problem is the shell's exit code. As far as I can tell,
it's either the result of the last call in the batch file, or
the value explicitly set via the "EXIT" call. For example, the
following batch file, when executed via <exec>, fails as it should.
— build.xml
...
<exec executable="some.bat" failonerror="true"/>
...
— some.bat
@echo off
java SomeMissingClass
—
C:\temp>ant
Buildfile: build.xml
all:
[exec] java.lang.NoClassDefFoundError: SomeMissingClass
[exec] Exception in thread "main"
BUILD FAILED
C:\temp\build.xml:4: exec returned: 1
—
Unfortunately, as soon as you add other commands the
status gets lost (at least some commands, eg
SET does not harm the status).
— some.bat
@echo off
java SomeMissingClass
goto end
:end
—
[exec] java.lang.NoClassDefFoundError: SomeMissingClass
[exec] Exception in thread "main"
BUILD SUCCESSFUL
—
Windows also sets a pseudo-environment variable ERRORLEVEL to
the exit status of the last executed process, in the example
above to the exit status of "java," which would be the one
we want.
— some.bat
@echo off
java SomeMissingClass
goto end
:end
exit %ERRORLEVEL%
—
[exec] java.lang.NoClassDefFoundError: SomeMissingClass
[exec] Exception in thread "main"
BUILD FAILED
—
But if the batch file is called by an existing shell (command line),
it is executed in that shell, and the exit call would end it, not
just the batch file.
One could explicitly spawn the shell, call the batch file, and end
it with EXIT.
— build.xml
<exec executable="cmd.exe" failonerror="true">
<arg value="/c"/>
<arg value="call some.bat; EXIT %ERRORLEVEL%"/>
</exec>
—
Note the "/c" and the "call" before some.bat.
This can be fixed, at least for Windows XP, by adding the following statement in the last line of maven.bat.
exit %ERRORLEVEL%
Thereby resolving the original issue.
...
1 error
2 warnings
BUILD FAILED
File...... file:/C:/Documents and Settings/user/.maven/plugins/maven-test-plugin-1
.4/
Element... javac
Line...... 34
Column.... 46
Compile failed; see the compiler error output for details.
BUILD FAILED
C:\tmp\build.xml:160: Following error occured while exec
uting this line
C:\tmp\build.xml:103: exec returned: 70
Total time: 8 seconds