History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: MAVEN-1124
Type: Improvement Improvement
Status: Closed Closed
Resolution: Won't Fix
Priority: Trivial Trivial
Assignee: Unassigned
Reporter: Ronald Blaschke
Votes: 2
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Maven 1

Add exit to maven.bat

Created: 22/Jan/04 05:59 AM   Updated: 08/Mar/06 10:45 PM
Component/s: core
Affects Version/s: None
Fix Version/s: 1.0-rc3

Time Tracking:
Original Estimate: 1 minute
Original Estimate - 1 minute
Remaining Estimate: 1 minute
Remaining Estimate - 1 minute
Time Spent: Not Specified
Remaining Estimate - 1 minute

Environment: Windows (XP)


 Description  « Hide
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



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Ronald Blaschke - 22/Jan/04 06:02 AM
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


Ronald Blaschke - 22/Jan/04 02:11 PM
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.


dion gillard - 04/Feb/04 07:58 PM
Seems harmless to add this in....

Tim Anderson - 04/Feb/04 08:12 PM
It may need to be:
exit /B %ERRORLEVEL%

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


Ronald Blaschke - 05/Feb/04 09:00 AM
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).


dion gillard - 24/Feb/04 06:30 PM
Seems to be unneeded

Bryan Davis - 04/Apr/04 04:11 PM
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).


Brian Conlon - 09/Apr/04 02:06 PM
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.

Ronald Blaschke - 18/Apr/04 11:45 AM
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.


Bryan Davis - 26/Apr/04 11:40 PM
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.


Brett Porter - 27/Apr/04 01:55 AM
I'm a little confused here - does this need to be reopened or not?