223

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.

2
  • 9
    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 Commented Nov 25, 2016 at 12:56
  • 1
    Possible duplicate of How to run multiple .BAT files within a .BAT file Commented Nov 1, 2018 at 16:31

6 Answers 6

368

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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. microsoft.com/resources/documentation/windows/xp/all/proddocs/… for more info on call
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.
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: 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.
add call pause at the end (without quotes) if you want to keep the cmd windows open
|
45

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 

5 Comments

(The first example here is equivalent to cmd1 && cmd2 && cmd3 && cmd4, essentially making all the commands one line.)
True. I wrote it that way because is a bat file, mostly you will have multiple, possibly long commands, 4 commands in a line will not be good in future maintenance. Readability is key in writing good programs and so they are on different lines.
This solution did the trick for me. The call solution wasn't stopping at errors, this one does.
Worked perfectly well when the command encountered an error!
Happy to know 👍 @HarshaJK
22

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 

Comments

1

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

1 Comment

Is there a way to force the script to execute the next mvn command?
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!

1 Comment

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.
-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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.