216

I have a batch file that executes three Maven commands, one after the other. Each command can be successfully executed in the script - by itself!. But when I add all three commands to the same file, only the first one executes before the script exits. Any idea why?

mvn install:install-file -DgroupId=gdata -DartifactId=base -Dversion=1.0 -Dfile=gdata-base-1.0.jar  -Dpackaging=jar -DgeneratePom=true
mvn install:install-file -DgroupId=gdata -DartifactId=blogger -Dversion=2.0 -Dfile=gdata-blogger-2.0.jar  -Dpackaging=jar -DgeneratePom=true
mvn install:install-file -DgroupId=gdata -DartifactId=blogger-meta -Dversion=2.0 -Dfile=gdata-blogger-meta-2.0.jar  -Dpackaging=jar -DgeneratePom=true

Also, if I copy all three commands and paste them into a command shell (cmd.exe), they execute one after the other with no problem. So this is apparently some issue with the dos batch file.

wiki
  • 3,379
  • 3
  • 20
  • 13
  • 8
    What a coincedent: I came here with the same problem and also the same commands in my batch file - multiple lines of "mvn install:install-file" :-D – fishbone Nov 25 '16 at 12:56
  • 1
    Possible duplicate of *[How to run multiple .BAT files within a .BAT file](https://stackoverflow.com/questions/1103994/how-to-run-multiple-bat-files-within-a-bat-file)* – Peter Mortensen Nov 01 '18 at 16:31

6 Answers6

359

Maven uses batch files to do its business. With any batch script, you must call another script using the call command so it knows to return back to your script after the called script completes. Try prepending call to all commands.

Another thing you could try is using the start command which should work similarly.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 1
    I've put `ant` inside of windows batch files before and the `call` was required to get `ant` to execute. Without `call` the batch will stop after the first command, hence the second two not executing. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/call.mspx for more info on `call` – Owen Oct 27 '10 at 19:32
  • 27
    `start` will cause a new window to spawn, and each of the three commands will run in parallel. If you need them to run synchronously, use `call`. – akf Oct 27 '10 at 19:33
  • @akf: The `WAIT` option for the `start` command will cause it to wait for the program to close so it doesn't necessarily have to run in parallel. I should have mentioned that in the beginning. – Jeff Mercado Oct 27 '10 at 20:04
  • 15
    How on earth does it make sense for a scripting environment to behave this way? If I've written a script with two commands I expect the commands to be executed; I don't expect the environment to just decide to stop executing my script simply because one of those commands happens to be implemented as a script itself. – bames53 Sep 20 '13 at 19:53
  • 4
    @bames53: this is a limitation that originated from DOS that carried over into modern implementations of the command prompt in Windows. Since Microsoft's goals were to maintain backward compatibility, this is the result. – Jeff Mercado Sep 20 '13 at 20:47
  • 3
    add `call pause` at the end (without quotes) if you want to keep the cmd windows open – JinSnow Sep 29 '17 at 21:40
  • @JinSnow, thanks. Using 'call pause' at the end is very helpful when running the script from GUI. – Prajwal Jul 12 '19 at 06:57
  • Similarly if running gradlew from a batch file the call prefix is required. – bitrock Jul 19 '19 at 20:24
44

Having call helps. However today it didn't.

This is how I solved it:

Bat file contents (if you want to stop batch when one of cmds errors)

cmd1 && ^
cmd2 && ^
cmd3 && ^
cmd4

Bat file contents (if you want to continue batch when one of cmds errors)

cmd1 & ^
cmd2 & ^
cmd3 & ^
cmd4
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
21

To execute more Maven builds from one script you shall use the Windows call function in the following way:

call mvn install:install-file -DgroupId=gdata -DartifactId=base -Dversion=1.0 -Dfile=gdata-base-1.0.jar  -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -DgroupId=gdata -DartifactId=blogger -Dversion=2.0 -Dfile=gdata-blogger-2.0.jar  -Dpackaging=jar -DgeneratePom=true
call mvn install:install-file -DgroupId=gdata -DartifactId=blogger-meta -Dversion=2.0 -Dfile=gdata-blogger-meta-2.0.jar  -Dpackaging=jar -DgeneratePom=true
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

It should be that the particular mvn command execs and does not return, thereby not executing the rest of the commands.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
1

Try writing the following batch file and executing it:

Echo one
cmd
Echo two
cmd
Echo three
cmd

Only the first two lines get executed. But if you type "exit" at the command prompt, the next two lines are processed. It's a shell loading another.

To be sure that this is not what is happening in your script, just type "exit" when the first command ends.

HTH!

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • Unlikely; Maven indeed uses a batch file which is why they need to use `call`. It's not a nested shell that isn't terminated. – Joey Oct 27 '10 at 19:40
-1

Dos commands in my batch file were running only when I type EXIT in command/DOS window. This problem solved when I removed CMD from batch file. No need of it.

  • well, if you explicitly open another instance (with the `cmd` command), you naturally have to `exit` it to get back to the original instance. But this has nothing to do with the question, so delete this "answer" to avoid downvoting. – Stephan Sep 02 '20 at 17:43