Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 611
wiki Avatar asked Oct 27 '10 19:10

wiki


People also ask

Why does my batch file only executes the first line?

The reason for this is that if you just start one bat file from another, only one of them will exit, while if using CALL, when the called bat file exits, the calling bat file will continue executing.

Can a batch script file run multiple commands at the same time?

You can use batch scripts to run multiple commands and instructions on your machine simultaneously. Using a batch script, you will be able to execute all your commands one by one automatically.

How do I run multiple commands in one batch file?

Try using the conditional execution & or the && between each command either with a copy and paste into the cmd.exe window or in a batch file. Additionally, you can use the double pipe || symbols instead to only run the next command if the previous command failed.


2 Answers

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.

like image 59
Jeff Mercado Avatar answered Sep 22 '22 12:09

Jeff Mercado


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 
like image 43
Manohar Reddy Poreddy Avatar answered Sep 23 '22 12:09

Manohar Reddy Poreddy