从 MS bat 文件执行多个 Maven 项目?
我需要使用 build.bat 文件构建 3 个独立的 Maven 项目(因为 tycho 聚合不是一个选项 - 请参阅 romaintaz 答案的评论)。我已经尝试过(从构建文件夹执行 - 见下文):
cd ../projectA
mvn clean install -U
cd ..
cd ../projectB
mvn clean install -U
cd ..
cd ../projectC
mvn clean install -U
项目的文件夹结构是:
build
|--> build.bat
projectA
|--> pom.xml
projectB
|--> pom.xml
projectC
|--> pom.xml
但只有projectA是构建projectB,而projectC被跳过。关于如何修改上面的bat文件以便在前一个项目构建成功的情况下构建以下项目的任何想法?
I need to build 3 independent maven projects using a build.bat file (because of tycho aggregation is not an option - see comments on romaintaz answer). I have tried (executed from the build folder - see below):
cd ../projectA
mvn clean install -U
cd ..
cd ../projectB
mvn clean install -U
cd ..
cd ../projectC
mvn clean install -U
where the folder structure of the projects are:
build
|--> build.bat
projectA
|--> pom.xml
projectB
|--> pom.xml
projectC
|--> pom.xml
but only projectA is build projectB and projectC are skipped. Any ideas on how to modify the above batfile so the following project is build if the previous was build successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如上所述,您需要使用“call”来运行 mvn 脚本,如下所示:
为了捕获错误,您需要使用
ERROR_LEVEL
变量,如下所示:请参阅 http://jojovedder.blogspot.com/2009/03/executing-multiple-mvn-commands-from.html 获取更多评论。
As noted above, you need to use "call" to run mvn script as in:
In order to catch errors you need to use the
ERROR_LEVEL
variable as in:See http://jojovedder.blogspot.com/2009/03/executing-multiple-mvn-commands-from.html for further comments.
您为什么不尝试创建一个聚合父项目?
您似乎具有以下结构:
只需在根目录(在我的示例中为
someDirectory
)中创建一个pom.xml
,并定义模块列表,它们是 <代码>projectA、projectB
和projectC
。这个 pom 看起来像:注意:
pom
,因为它不是一个“真正的”Java 项目。模块
的名称应与托管子模块的目录名称匹配。现在,通过这样做,当您在根目录上运行 Maven 命令时,Maven 将自动在所有模块上运行此命令。因此,如果您只是在根目录上运行
mvn clean install
,它将在您的三个模块中运行此命令。重要说明:我在这里谈论的是Maven 的聚合功能。 不是继承。这意味着不需要每个模块都将根项目作为父项目。
Why would you not try to create an aggregation parent project?
You seems to have the following structure:
Simply create a
pom.xml
in the root directory (someDirectory
in my example), and define the list of modules, which are theprojectA
,projectB
andprojectC
. This pom will look like:notes:
<packaging>pom</packaging>
, as it is not a "real" Java project.module
should match the name of the directory where the sub-module is hosted.Now, by doing that, when you run a Maven command on the root directory, Maven will automatically run this command on all the modules. So if you just run
mvn clean install
on the root directory, it will run this command in your three modules.Important note: I am talking here about the aggregation feature of Maven. Not inheritance. This means that it is not required that each module has the root project as parent.
当您想调用父文件中的另一个批处理文件时,请使用“call”,以便控制权将返回到父批处理文件并继续执行。
例如调用 mvn clean install
Use 'call' when you want to invoke another batch file in the parent file ,so that control will be returned to the parent batch file and it will continue execution.
e.g call mvn clean install
我认为使用 %ERRORLEVEL% 将完成 +FrVaBe 的答案。最初来自 Kunal
I think that using %ERRORLEVEL% will complete +FrVaBe's answer. Originally from Kunal
由于您的项目位于同一目录级别,因此您不应向上导航两次。您将通过
cd ..
上升,然后再次通过执行cd ../projectX
上升一级以进入下一个项目。删除中间的 cd .. 或将项目文件夹中的 cd 更改为 cd ./projectX (当前目录而不是父目录
)你真的需要为此使用bat 文件吗?你考虑过多模块 pom 吗?
Since your projects are on the same directory level, you shouldn't navigate up twice. You're going up via
cd ..
and then again you're going up one level to enter the next project by doingcd ../projectX
.Either remove the
cd ..
's in between or change the cd into your project folder intocd ./projectX
(current directory instead of parent directory)By the way, do you really need to use a bat-file for this? Have you considered a multimodule pom?
FrVaBe 的回答非常好。如果您想保留代码结构,由于一些其他活动(例如复制工件),解决方案是:
这里最重要的是 call 函数,它用于将 maven build 作为一种子例程执行,它会返回到您的bat文件中并且不会完全退出。
FrVaBe had answered very good. If you would like to keep your code structure, because of some other activities like copying artifacts the the solution would be:
Here the most important is call function which is used to execute maven build as a kind of subroutine, which ends back into your bat file andd do not exits completely.
使用
call
命令执行您的 mvn 流程,例如:参见 call 在线文档 或
了解有关 call 命令的进一步说明。
为了避免使用所有这些 cd 命令,您还可以使用 -f 选项来指定 pom 的路径,例如
Use the
call
command to execute your mvn processes, like:See online doc for call or
for further explanations on the call command.
To avoid having all these
cd
commands you can also use the-f
option to specify the path to your pom, e.g.