为什么只执行此 Windows 批处理文件的第一行,而在命令 shell 中执行所有三行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4036754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does only the first line of this Windows batch file execute but all three lines execute in a command shell?
提问by wiki
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?
我有一个执行三个 Maven 命令的批处理文件,一个接一个。每个命令都可以在脚本中成功执行 - 单独执行!。但是当我将所有三个命令添加到同一个文件时,只有第一个在脚本退出之前执行。知道为什么吗?
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.
此外,如果我复制所有三个命令并将它们粘贴到命令外壳 (cmd.exe) 中,它们会一个接一个地执行,没有问题。所以这显然是 dos 批处理文件的一些问题。
回答by Jeff Mercado
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.
Maven 使用批处理文件来开展业务。对于任何批处理脚本,您必须使用该call
命令调用另一个脚本,以便它知道在被调用脚本完成后返回到您的脚本。尝试添加call
到所有命令。
Another thing you could try is using the start
command which should work similarly.
您可以尝试的另一件事是使用start
应该类似工作的命令。
回答by Manohar Reddy Poreddy
Having call
helps. However today it didn't.
有call
帮助。然而今天没有。
This is how I solved it:
我是这样解决的:
Bat file contents(if you want to stopbatch when one of cmds errors)
Bat 文件内容(如果您想在cmds 错误之一时停止批处理)
cmd1 && ^
cmd2 && ^
cmd3 && ^
cmd4
Bat file contents(if you want to continuebatch when one of cmds errors)
Bat 文件内容(如果您想在cmds 错误之一时继续批处理)
cmd1 & ^
cmd2 & ^
cmd3 & ^
cmd4
回答by Seweryn Habdank-Wojewódzki
To execute more Maven builds from one script you shall use the Windows callfunction in the following way:
要从一个脚本执行更多 Maven 构建,您应按以下方式使用 Windows调用函数:
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
回答by Alan Haggai Alavi
It should be that the particular mvn
command exec
s and does not return, thereby not executing the rest of the commands.
应该是特定mvn
命令exec
s 和 不返回,从而不执行其余命令。
回答by Dr. belisarius
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.
只有前两行被执行。但是,如果您在命令提示符下键入“exit”,则会处理接下来的两行。这是一个shell加载另一个。
To be sure that this is not what is happening in your script, just type "exit" when the first command ends.
为确保这不是脚本中发生的情况,只需在第一个命令结束时键入“exit”即可。
HTH!
哼!