Maven:如何进行并行构建?
当您在多核/多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Maven 3(从 beta 1 开始)现在支持并行构建作为实验性功能。
例如,
完整文档可以在 Maven wiki 上找到: Maven 3 中的并行构建 - Apache Maven - Apache 软件基金会。
Maven 3 (as of beta 1) now supports parallel builds as an experimental feature.
For example,
Full documentation can be found on the Maven wiki: Parallel builds in Maven 3 - Apache Maven - Apache Software Foundation.
建议的解决方案很棒,但我想在此处的答案中添加一些关于并行构建期间的测试稳定性的内容。
因此,当使用 Maven 并行构建时:
可能会出现一些测试问题。 请注意测试中串行和并行测试执行之间不同的任何行为。 大多数情况下,这种情况都是由于资源方面的测试隔离不当造成的。
例如,
test1
使用键12345
操作数据库条目,这是硬编码的,test2
使用相同的条目! 这不可能是好事……这是一种应该首先考虑的情况,但有时它会被忘记,并且一旦切换到并行 Maven 构建,可能会导致不同的问题。
如果发生这种情况,并且您至少在某些情况下仍然希望使用并行执行,您可以(当然,除了尝试修复测试并使它们正确隔离)使用
-DskipTests
禁用 Maven 测试运行代码>参数:
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:
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 key12345
, which is hard-coded andtest2
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:一些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
如果您遇到这个问题想要整理您的构建服务器,并且您没有使用本地处理 Maven 的服务器,那么您正在寻找的神奇标志是这样的:
为您的每个构建执行此操作,然后您可以让它们运行所有同时而不是让所有使用 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:
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!