Maven:如何进行并行构建?

发布于 2024-07-14 06:23:18 字数 88 浏览 7 评论 0原文

当您在多核/多 CPU 计算机上使用 Maven 进行构建时,通常可以并行构建不同的子项目。 有没有办法用 Maven 来做到这一点? 有这个/什么的插件吗?

When you build with maven on a multicore / multi-CPU machine it would often be possible to build different subprojects in parallel. Is there a way to do this with maven? Is there a plugin for this / whatever?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

如何视而不见 2024-07-21 06:23:18

Maven 3(从 beta 1 开始)现在支持并行构建作为实验性功能。

例如,

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

完整文档可以在 Maven wiki 上找到: Maven 3 中的并行构建 - Apache Maven - Apache 软件基金会

Maven 3 (as of beta 1) now supports parallel builds as an experimental feature.

For example,

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Full documentation can be found on the Maven wiki: Parallel builds in Maven 3 - Apache Maven - Apache Software Foundation.

失去的东西太少 2024-07-21 06:23:18

建议的解决方案很棒,但我想在此处的答案中添加一些关于并行构建期间的测试稳定性的内容。

因此,当使用 Maven 并行构建时:

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

可能会出现一些测试问题。 请注意测试中串行和并行测试执行之间不同的任何行为。 大多数情况下,这种情况都是由于资源方面的测试隔离不当造成的。

例如,test1 使用键 12345 操作数据库条目,这是硬编码的,test2 使用相同的条目! 这不可能是好事……

这是一种应该首先考虑的情况,但有时它会被忘记,并且一旦切换到并行 Maven 构建,可能会导致不同的问题。

如果发生这种情况,并且您至少在某些情况下仍然希望使用并行执行,您可以(当然,除了尝试修复测试并使它们正确隔离)使用 -DskipTests禁用 Maven 测试运行代码>参数:

mvn clean install -T 4 -DskipTests

The suggested solutions are great, but I wanted to add something to the answers here regarding the test stability during parallel builds.

So, when Maven parallel build is used:

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Some issues with tests can appear. Note any behavior in tests which is different between serial and parallel test execution. Most of the times it happens do to improper test isolation resource-wise.

For example, test1 manipulate db entry with key 12345, which is hard-coded and test2 uses the same entry! It can't be good…

It's a situation that should be considered in the first place, but sometime it's forgotten and could lead to different problems once the switch to parallel maven build is made.

In case that happens, and you still want to use the parallel execution at least in some of the occasions, you can (of course, besides trying to fix the test and make them properly isolated) to disable Maven test runs using -DskipTests argument:

mvn clean install -T 4 -DskipTests
却一份温柔 2024-07-21 06:23:18

一些CI构建应用程序(例如hudson)可以同时构建多个maven项目(甚至在多台机器上)。

在maven“独立”中对此的支持也很好,快速浏览一下maven问题跟踪器给了我: http://jira.codehaus.org/browse/MNG-3004

Some of the CI build applications (e.g. hudson) can build multiple maven projects at the same time (and even on multiple machines).

Support for this in maven 'standalone' would also be nice, a quick look through the maven issue tracker gave me: http://jira.codehaus.org/browse/MNG-3004

无语# 2024-07-21 06:23:18

如果您遇到这个问题想要整理您的构建服务器,并且您没有使用本地处理 Maven 的服务器,那么您正在寻找的神奇标志是这样的:

-Dmaven.repo.local=someNoneGlobalDir

为您的每个构建执行此操作,然后您可以让它们运行所有同时而不是让所有使用 Maven 的东西都在队列中!

If you came to this question looking to sort out your build server and you're not using one that deals with maven natively the magic flag you are looking for is this:

-Dmaven.repo.local=someNoneGlobalDir

do that for each one of your builds and you can let them run all at the same time rather than having everything that uses maven in a queue!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文