Batch Maven build breaking cmd.exe?

0

I had a Windows .bat file with a script for compilation of a project I'm working on. The last command is a maven build. Since it takes a while to finish, I wanted to be notified when it does.

I figured that the easiest way would be the equivalent to echo -e '\a' . So I looked around a bit and added echo <BELL>(<BELL> being a byte with the code 0x07) at the end of my script. I added echo in front of all commands for a quick test and it worked like a charm.

Removed the echos and ran the script. A few minutes later I notice it's finished. I didn't hear the bell, but assumed I just wasn't paying attention. Retested with echoes and it bell-ed just fine. However this has happened several times and I now know for a fact I'm not just missing it, it isn't there.

I've tried making a simple C program just running printf("\a") to the same results. I've then just tried an echo please work, this text didn't show up either. I've figured out that the difference is in the maven command being or not being echoed out. To sum up:

  • Windows 10, Maven for Windows as last effective command in .bat batch script
  • If mvn command is prepended with echo everythng works as it should
  • If mvn command is ran subsequent commands in the .bat do not run
  • Once the script finishes there appears to be nothing wrong with the cmd.exe instance

I have Cygwin (needed for the build) so if it's usefull for debugging that's not a problem.

EDIT: So it appears Maven is a batch file itself. Interrestingly, where mvn lists first bhahlabh\mnv (a bash script) and then blahblah\mvn.com

Mr Redstoner

Posted 2019-08-17T19:48:57.877

Reputation: 103

3Is your maven command another batch file? – DavidPostill – 2019-08-17T19:52:28.907

@DavidPostill No, just a regular mvn clean package -define blah -define blahblah kind – Mr Redstoner – 2019-08-17T19:54:23.470

Answers

1

In Maven for Windows, the command 'mvn' refers to a batch script file called either mvn.bat or mvn.cmd (depending on the Maven version). Look in Maven's bin folder. In simple terms, a .bat file is a .cmd file, and vice versa. They are cmd.exe batch language scripts.

From the Maven web site:

You run Maven by invoking a command-line tool: mvn.cmd from the bin directory of the Maven. To do this conveniently, ${maven.home}\bin must be in your PATH, just like the Java SDK commands. You can add directories to your PATH in the control panel; the details vary by Windows version.

Maven on Windows

Under cmd.exe, if you run a second batch (.bat or .cmd) file from a first one, by using just its name, command transfers to the second one, and never returns.

Control comes back if you invoke the second script using the CALL command.

Example to show what I mean:

Here is a.bat (or a.cmd if you like)

@echo off
echo I am a.bat
b.bat
echo back again

Here is b.bat (or b.cmd if you like)

@echo off
echo I am b.bat

if you run a.bat, and b.bat is either in the same folder, or on your PATH, you will see this

I am a.bat
I am b.bat

You will not see back again, unless, in a.bat, you changed b.bat to call b.bat

Try putting CALL before 'mvn' in your batch.

Michael Harvey

Posted 2019-08-17T19:48:57.877

Reputation: 832

Will try, but I downloaded Maven separately as a standalone and only later added Cygwin to my system (needed rpmbuild). I haven't to my knowledge added the cygwin maven package. – Mr Redstoner – 2019-08-18T15:39:41.847

1See my update to make my answer more clear. – Michael Harvey – 2019-08-18T15:44:45.433

That makes sense. Will report on my findings once I try it(can't atm). – Mr Redstoner – 2019-08-18T19:55:05.783

So let me get this straight: when running a .bat, by default, something.exe is launched separately, but using something.bat, it instead nukes the running one and runs the new one? – Mr Redstoner – 2019-08-19T11:09:16.257

1

Yes, you have got it straight. When you run batch2 from batch1, using just the [path]/name of batch2, batch1 is aborted, control passes to batch2 and never comes back to batch1. This can be overcome by using CALL batch2. This is not what happens with an .exe, and is not the default behaviour in Unix/Linux. It catches many people out. See here

– Michael Harvey – 2019-08-19T11:54:17.400

This batch behaviour has been standard in Windows NT family command line batch scripting, and before that, MS-DOS since around 1981. – Michael Harvey – 2019-08-19T17:13:01.390