Why does only the first line of this Windows batch file execute but all three lines execute in a command shell?
Solution 1
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.
Solution 2
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
Solution 3
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
Solution 4
It should be that the particular mvn
command exec
s and does not return, thereby not executing the rest of the commands.
Solution 5
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!
Related videos on Youtube
wiki
Updated on September 02, 2020Comments
-
wiki over 3 years
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.
-
fishbone over 7 yearsWhat 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
-
Peter Mortensen over 5 yearsPossible duplicate of How to run multiple .BAT files within a .BAT file
-
Praveen Tiwari almost 5 years@fishbone and coincidently you and OP both have 1.3k reputation..
-
-
wiki over 13 yearsIs there a way to force the script to execute the next
mvn
command? -
Owen over 13 yearsI've put
ant
inside of windows batch files before and thecall
was required to getant
to execute. Withoutcall
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 oncall
-
akf over 13 years
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, usecall
. -
Rich over 13 yearsUnlikely; Maven indeed uses a batch file which is why they need to use
call
. It's not a nested shell that isn't terminated. -
Jeff Mercado over 13 years@akf: The
WAIT
option for thestart
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. -
bames53 over 10 yearsHow 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.
-
Jeff Mercado over 10 years@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.
-
Ben Gotow over 6 years(The first example here is equivalent to
cmd1 && cmd2 && cmd3 && cmd4
, essentially making all the commands one line.) -
JinSnow over 6 yearsadd
call pause
at the end (without quotes) if you want to keep the cmd windows open -
Manohar Reddy Poreddy over 6 yearsTrue. 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.
-
Tim over 5 yearsThis solution did the trick for me. The
call
solution wasn't stopping at errors, this one does. -
Prajwal almost 5 years@JinSnow, thanks. Using 'call pause' at the end is very helpful when running the script from GUI.
-
bitrock almost 5 yearsSimilarly if running gradlew from a batch file the call prefix is required.
-
Harsha J K almost 4 yearsWorked perfectly well when the command encountered an error!
-
Manohar Reddy Poreddy almost 4 yearsHappy to know 👍 @HarshaJK
-
Stephan over 3 yearswell, if you explicitly open another instance (with the
cmd
command), you naturally have toexit
it to get back to the original instance. But this has nothing to do with the question, so delete this "answer" to avoid downvoting.