从 MS bat 文件执行多个 Maven 项目?

发布于 2024-11-09 10:54:36 字数 496 浏览 0 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酷到爆炸 2024-11-16 10:54:37

如上所述,您需要使用“call”来运行 mvn 脚本,如下所示:

call mvn package

为了捕获错误,您需要使用 ERROR_LEVEL 变量,如下所示:

call mvn clean
echo Exit Code = %ERRORLEVEL%
if not "%ERRORLEVEL%" == "0" exit /b

请参阅 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:

call mvn package

In order to catch errors you need to use the ERROR_LEVEL variable as in:

call mvn clean
echo Exit Code = %ERRORLEVEL%
if not "%ERRORLEVEL%" == "0" exit /b

See http://jojovedder.blogspot.com/2009/03/executing-multiple-mvn-commands-from.html for further comments.

长伴 2024-11-16 10:54:37

您为什么不尝试创建一个聚合父项目?

您似乎具有以下结构:

someDirectory
  +- projectA
      +- pom.xml
  +- projectB
      +- pom.xml
  +- projectC
      +- pom.xml

只需在根目录(在我的示例中为 someDirectory)中创建一个 pom.xml ,并定义模块列表,它们是 <代码>projectA、projectBprojectC。这个 pom 看起来像:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company</groupId>
    <artifactId>my-aggregation-project</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>projectA</module>
        <module>projectB</module>
        <module>projectC</module>
    </modules>
</project>

注意:

  • 不要忘记设置 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:

someDirectory
  +- projectA
      +- pom.xml
  +- projectB
      +- pom.xml
  +- projectC
      +- pom.xml

Simply create a pom.xml in the root directory (someDirectory in my example), and define the list of modules, which are the projectA, projectB and projectC. This pom will look like:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company</groupId>
    <artifactId>my-aggregation-project</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>projectA</module>
        <module>projectB</module>
        <module>projectC</module>
    </modules>
</project>

notes:

  • Don't forget to set the <packaging>pom</packaging>, as it is not a "real" Java project.
  • The name of a 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.

失而复得 2024-11-16 10:54:37

当您想调用父文件中的另一个批处理文件时,请使用“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

盗心人 2024-11-16 10:54:37

我认为使用 %ERRORLEVEL% 将完成 +FrVaBe 的答案。最初来自 Kunal

 call mvn -f ProjectA\pom.xml clean install  if not "%ERRORLEVEL%" == "0" goto error

 call mvn -DskipTests=true -f ProjectC\pom.xml clean install if not "%ERRORLEVEL%" == "0" goto error

 exit

 :error @echo Build Failed pause

I think that using %ERRORLEVEL% will complete +FrVaBe's answer. Originally from Kunal

 call mvn -f ProjectA\pom.xml clean install  if not "%ERRORLEVEL%" == "0" goto error

 call mvn -DskipTests=true -f ProjectC\pom.xml clean install if not "%ERRORLEVEL%" == "0" goto error

 exit

 :error @echo Build Failed pause
请叫√我孤独 2024-11-16 10:54:37

由于您的项目位于同一目录级别,因此您不应向上导航两次。您将通过 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 doing cd ../projectX.

Either remove the cd ..'s in between or change the cd into your project folder into cd ./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?

沒落の蓅哖 2024-11-16 10:54:37

FrVaBe 的回答非常好。如果您想保留代码结构,由于一些其他活动(例如复制工件),解决方案是:

cd ../projectA
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectB
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectC
call mvn clean install -U
echo Do some stuff with artifacts

这里最重要的是 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:

cd ../projectA
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectB
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectC
call mvn clean install -U
echo Do some stuff with artifacts

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.

放肆 2024-11-16 10:54:36

使用 call 命令执行您的 mvn 流程,例如:

call mvn clean install -U

参见 call 在线文档

help call

了解有关 call 命令的进一步说明。

为了避免使用所有这些 cd 命令,您还可以使用 -f 选项来指定 pom 的路径,例如

call mvn -f <path>/projectA/pom.xml clean install -U
call mvn -f <path>/projectB/pom.xml clean install -U
call mvn -f <path>/projectC/pom.xml clean install -U

Use the call command to execute your mvn processes, like:

call mvn clean install -U

See online doc for call or

help call

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.

call mvn -f <path>/projectA/pom.xml clean install -U
call mvn -f <path>/projectB/pom.xml clean install -U
call mvn -f <path>/projectC/pom.xml clean install -U
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文