如何在批处理文件中执行多个maven命令?

139
我创建了一个.bat文件,内容如下:
mvn clean;
mvn package;
但是它并没有起作用,只有第一条命令被执行了。请问有人能帮我吗?
9个回答

294

使用

call mvn clean
call mvn package
请注意,在批处理文件中您不需要分号。而您需要使用call的原因是mvn本身就是一个批处理文件,批处理文件需要使用call互相调用,否则控制权不会返回给调用者。
如果您希望后续命令在命令行中回显(在批处理输出中显示),您还必须在call mvn完成后进行echo on(在下一行上)。这是因为mvn关闭了回显并没有将其打开。

3
同"Gradle"相同。 - Prabs
此外,Maven 还有一个批处理脚本选项:-B--batch-mode,用于“以非交互(批处理)模式运行(禁用输出颜色)”。 - Liscare

31

Joey的回答很好,但是也许一个更完整的代码示例将有助于像我一样正在解决在Windows批处理文件中构建多个maven项目的类似问题的其他人:

REM maven itself uses a batch file so each mvn must be preceded by "call"
REM the -f flag specifies where the pom.xml is found for the project
REM mvn install will save the target output to %userprofile%\.m2\repository ...

call mvn install -f c:\Users\John\workspace\PropertiesReader\pom.xml

call mvn install -f c:\Users\John\workspace\PropertiesWriter\pom.xml

16

你也可以使用以下一行代码:

call mvn clean package 

4
不过,他们应该使用 call 命令,这样在 mvn 命令后输入的任何内容都会被执行。 - Joey

11

观察到的行为源自 MS-DOS 1.0 的时代,并因兼容性原因而保留。作为解决方案,您应以以下方式使用 Windows call 函数:

call mvn clean
call mvn package

“Call”命令可以从一个批处理程序中调用另一个批处理程序,并将其解释为子程序。


10

我有更多的项目要运行,我创建了这样一个批处理文件:

@echo off
SET DEVELOPMENT_HOME=C:\Projects

cd %DEVELOPMENT_HOME%\Project1\
call mvn clean install

cd %DEVELOPMENT_HOME%\Project2\
call mvn clean install

8

当你想在父文件中调用另一个批处理文件时,请使用“call”命令,这样控制权将返回到父批处理文件并继续执行。

例如:call mvn clean install


0
Use these commands in batch file to run ur script. Keep your batch file where 
you pom.xml file is housed

set ProjectPath=C:\TetonWorkSpace\PeriodicApplicationCheck
cd %ProjectPath%
mvn clean test -Dxmlfile=Smoke.xml
pause

To Create a Task in Task scheduler:
1. Follow steps as prescribed to create task
2. In the action tab, just place the path of ur batch file as shown below
    C:\TetonWorkSpace\PeriodicApplicationCheck\testng.bat
3. You can ignore the rest two options like Add Argument and Start in. Use it 
   only when there are certain conditions to be used without which the script 
    becomes dysfunctional.

Placing path of the batch file in Action Tab.


-1
我们可以使用以下内容构建一个Maven,并将其传递到任何Unix文件夹以进行开发目的。
SET projectName=commonutil
cd %gitpath%\%projectName%
call mvn clean install -DskipTests=true %password%
IF %ERRORLEVEL% EQU 0 (Echo No error found) ELSE goto exitdoor 
SET jarpath="%gitpath%\%projectName%\target\%projectName%-0.0.1-SNAPSHOT.jar"
copy /Y %jarpath% "%libpath%"
scpg3 %jarpath% %ssh_profile_name%@%hostname%:%dev_lib_folder_name%

-1

使用

执行 mvn clean package 命令

sample
------
echo %test%
cd %test%\ManaulActionAddNotes-test
call mvn clean
cd %test%\restAuthentication-test
call mvn clean

2
这里的信息比其他答案更详细吗?对我来说,它看起来像是被接受的(9年前)答案的重复。 - jeb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接