Why does only the first line of this Windows batch file execute but all three lines execute in a command shell?

116,564

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 execs 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!

Share:
116,564

Related videos on Youtube

wiki
Author by

wiki

Updated on September 02, 2020

Comments

  • wiki
    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
      fishbone over 7 years
      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
    • Peter Mortensen
      Peter Mortensen over 5 years
    • Praveen Tiwari
      Praveen Tiwari almost 5 years
      @fishbone and coincidently you and OP both have 1.3k reputation..
  • wiki
    wiki over 13 years
    Is there a way to force the script to execute the next mvn command?
  • Owen
    Owen over 13 years
    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/proddoc‌​s/… for more info on call
  • akf
    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, use call.
  • Rich
    Rich over 13 years
    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.
  • Jeff Mercado
    Jeff Mercado over 13 years
    @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.
  • bames53
    bames53 over 10 years
    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.
  • Jeff Mercado
    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
    Ben Gotow over 6 years
    (The first example here is equivalent to cmd1 && cmd2 && cmd3 && cmd4, essentially making all the commands one line.)
  • JinSnow
    JinSnow over 6 years
    add call pause at the end (without quotes) if you want to keep the cmd windows open
  • Manohar Reddy Poreddy
    Manohar Reddy Poreddy over 6 years
    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.
  • Tim
    Tim over 5 years
    This solution did the trick for me. The call solution wasn't stopping at errors, this one does.
  • Prajwal
    Prajwal almost 5 years
    @JinSnow, thanks. Using 'call pause' at the end is very helpful when running the script from GUI.
  • bitrock
    bitrock almost 5 years
    Similarly if running gradlew from a batch file the call prefix is required.
  • Harsha J K
    Harsha J K almost 4 years
    Worked perfectly well when the command encountered an error!
  • Manohar Reddy Poreddy
    Manohar Reddy Poreddy almost 4 years
    Happy to know 👍 @HarshaJK
  • Stephan
    Stephan over 3 years
    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.