Maven 1

Add exit to maven.bat

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Trivial Trivial
  • Resolution: Won't Fix
  • Affects Version/s: None
  • Fix Version/s: 1.0-rc3
  • Component/s: core
  • Labels:
    None
  • Environment:
    Windows (XP)
  • Number of attachments :
    0

Description

Windows (XP) seems to not propagate the last ERRORLEVEL in batch files. Thus, when calling maven.bat from an ant build file, ant reports success, even when maven fails.

...
1 error
2 warnings
BUILD FAILED

Total time: 6 seconds
Finished at: Thu Jan 22 11:49:10 CET 2004

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 SUCCESSFUL
Total time: 8 seconds

Activity

Hide
Ronald Blaschke added a comment -

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

Show
Ronald Blaschke added a comment - 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
Hide
Ronald Blaschke added a comment -

Please drop this request, this is not a maven issue.

Windows batch files are quite different from processes, and are always executed in the context of a command prompt (cmd.exe). Ant worries about the return code of the cmd process, not the executed batch file's ERRORLEVEL.

Show
Ronald Blaschke added a comment - Please drop this request, this is not a maven issue. Windows batch files are quite different from processes, and are always executed in the context of a command prompt (cmd.exe). Ant worries about the return code of the cmd process, not the executed batch file's ERRORLEVEL.
Hide
dion gillard added a comment -

Seems harmless to add this in....

Show
dion gillard added a comment - Seems harmless to add this in....
Hide
Tim Anderson added a comment -

It may need to be:
exit /B %ERRORLEVEL%

to avoid terminating cmd.exe if maven.bat was invoked from
the command line.

Show
Tim Anderson added a comment - It may need to be: exit /B %ERRORLEVEL% to avoid terminating cmd.exe if maven.bat was invoked from the command line.
Hide
Ronald Blaschke added a comment -

I have played around with this a bit on WinXP, and it seems like "exit /b %exit_code%" just sets the ERRORLEVEL and finishes the current batch script, whereas "exit %exit_code%" terminates cmd.exe with the errorlevel.

"exit /b %ERRORLEVEL%" at the end of the batch file propably does nothing, as it seems to be intended to exit the batch somewhere in the middle.

"exit %ERRORLEVEL%" at the end unfortunately terminates the process with the exit code (which is what you want when treating a batch file as a process, but not when you invoke it on the command line).

Show
Ronald Blaschke added a comment - I have played around with this a bit on WinXP, and it seems like "exit /b %exit_code%" just sets the ERRORLEVEL and finishes the current batch script, whereas "exit %exit_code%" terminates cmd.exe with the errorlevel. "exit /b %ERRORLEVEL%" at the end of the batch file propably does nothing, as it seems to be intended to exit the batch somewhere in the middle. "exit %ERRORLEVEL%" at the end unfortunately terminates the process with the exit code (which is what you want when treating a batch file as a process, but not when you invoke it on the command line).
Hide
dion gillard added a comment -

Seems to be unneeded

Show
dion gillard added a comment - Seems to be unneeded
Hide
Bryan Davis added a comment -

Hm. I don't really understand why this is not a Maven issue. My company's build machine only invokes Ant, so I have a build.xml which does an Ant <exec> to run the Maven command (.sh or .bat depending on platform). Some time ago the builds for my project started always returning success, even when they have failed.

I've been a staunch proponent of Maven (against much resistance I might add) at my company. This issue was highly visible... and not in a good way. As you might imagine, failed builds that no one knows about are pretty serious.

All I know is that this used to work, the build machine has not been OS upgraded to XP or anything, and the EXIT %ERRORLEVEL% seems to fix the problem (although with the aforementioned nasty side-effects if you are running Maven in a CMD window yourself).

Could someone at least take the time to explain why this isn't a Maven bug? Stuff like this may sound trivial but I can assure you that it is not. The fact of the matter is that most people use and are familiar with Ant, so making Maven seamlessly callable from Ant is a pretty big requirement. The Maven Ant task seems to be outdated (I spent quite a bit of time trying to get it to work but never solved forehead-related classloader issues).

Show
Bryan Davis added a comment - Hm. I don't really understand why this is not a Maven issue. My company's build machine only invokes Ant, so I have a build.xml which does an Ant <exec> to run the Maven command (.sh or .bat depending on platform). Some time ago the builds for my project started always returning success, even when they have failed. I've been a staunch proponent of Maven (against much resistance I might add) at my company. This issue was highly visible... and not in a good way. As you might imagine, failed builds that no one knows about are pretty serious. All I know is that this used to work, the build machine has not been OS upgraded to XP or anything, and the EXIT %ERRORLEVEL% seems to fix the problem (although with the aforementioned nasty side-effects if you are running Maven in a CMD window yourself). Could someone at least take the time to explain why this isn't a Maven bug? Stuff like this may sound trivial but I can assure you that it is not. The fact of the matter is that most people use and are familiar with Ant, so making Maven seamlessly callable from Ant is a pretty big requirement. The Maven Ant task seems to be outdated (I spent quite a bit of time trying to get it to work but never solved forehead-related classloader issues).
Hide
Brian Conlon added a comment -

I have to agree with Bryan on this one. I have exactly the same requirement to call Maven from Ant and have likewise been unsuccessful with the MavenTask. Because of IDE integration with Ant and the need for Ant to do some file manipulation before Maven is invoked, it would be really great to have a solution for this.

Show
Brian Conlon added a comment - I have to agree with Bryan on this one. I have exactly the same requirement to call Maven from Ant and have likewise been unsuccessful with the MavenTask. Because of IDE integration with Ant and the need for Ant to do some file manipulation before Maven is invoked, it would be really great to have a solution for this.
Hide
Ronald Blaschke added a comment -

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.

Show
Ronald Blaschke added a comment - 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.
Hide
Bryan Davis added a comment -

THANK YOU for adding additional information. Last week the %ERRORLEVEL% patch to maven.bat did NOT work in a particular failure case (although it definitely works some of the time), so I'm still faced with builds that are bad but report success.

I have not yet had time to investigate the specifics of what happened on this particular build, but the additional details are greatly appreciated.

Show
Bryan Davis added a comment - THANK YOU for adding additional information. Last week the %ERRORLEVEL% patch to maven.bat did NOT work in a particular failure case (although it definitely works some of the time), so I'm still faced with builds that are bad but report success. I have not yet had time to investigate the specifics of what happened on this particular build, but the additional details are greatly appreciated.
Hide
Brett Porter added a comment -

I'm a little confused here - does this need to be reopened or not?

Show
Brett Porter added a comment - I'm a little confused here - does this need to be reopened or not?

People

Vote (2)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
1m
Original Estimate - 1 minute
Remaining:
1m
Remaining Estimate - 1 minute
Logged:
Not Specified
Time Spent - Not Specified