Java 使maven构建更快的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32368976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ways to make maven build faster?
提问by Shubham Chaurasia
I have a multi module java project. Maven takes almost around 40 secs to build it. I have tried maven with multi threaded builds too by specifying -T and -C args for no of threads and cores to be used. But I haven't seen any significant improvement in wall time of my builds.
I am using maven 3.2.3 and sometimes I need to build my project very frequently.
I know that clean goal take a lot of time but I can not omit it.
Suggestions please....
我有一个多模块 java 项目。Maven 大约需要 40 秒来构建它。我也通过为要使用的线程和内核指定 -T 和 -C args 来尝试使用多线程构建的 maven。但是我没有看到我的构建的墙壁时间有任何显着改善。
我正在使用 maven 3.2.3,有时我需要非常频繁地构建我的项目。
我知道干净的目标需要很多时间,但我不能省略它。
建议请....
EDIT:
Note: In my case clean is not taking much time. It finishes in 1 sec. install is taking rest of the time.
编辑:
注意:就我而言,干净不需要太多时间。它在 1 秒内完成。安装正在占用其余时间。
采纳答案by Ram Ghadiyaram
Note: First thing is AFAIK, No other in built options available in maven apart from the all answers here.
注意:首先是 AFAIK,除了此处的所有答案外,maven 中没有其他可用的内置选项。
Running maven build with Multiple threads works for me to speed up the builds. For example :
使用多线程运行 maven build 对我来说可以加快构建速度。例如 :
mvn clean install -T100
where -T is for specifying how many threads you want based on your hardware.
其中 -T 用于根据您的硬件指定您想要的线程数。
Below are the variants from wiki
以下是来自维基的变体
Maven 3.x has the capability to perform parallel builds. The command is as follows:
- 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 3.x 能够执行并行构建。命令如下:
- mvn -T 4 全新安装 使用 4 个线程构建
- mvn -T 1C 全新安装每个 CPU 核心 1 个线程
- mvn -T 1.5C 全新安装每个 CPU 核心 1.5 个线程
How Execution is evaluated(See Parallel builds in Maven 3)?
如何评估执行(请参阅 Maven 3 中的并行构建)?
Each node in the graph represents a module in a multi-module build, the "levels" simply indicate the distance to the first module in the internal reactor dependency graph. Maven calculates this graph based on declared inter-module dependencies for a multi-module build. Note that the parent maven project is also a dependency, which explains why there is a single node on top of most project graphs. Dependencies outside the reactor do not influence this graph.
图中的每个节点代表多模块构建中的一个模块,“级别”仅表示到内部反应器依赖图中第一个模块的距离。Maven 根据声明的多模块构建的模块间依赖关系计算此图。请注意,父 maven 项目也是一个依赖项,这解释了为什么在大多数项目图的顶部都有一个节点。反应器外部的依赖关系不会影响此图。
Finally if you want to skip test execution you can also use -DskipTests
as well.
最后,如果您想跳过测试执行,您也可以使用-DskipTests
。
Caution :Some of your plugins may not be compatible for multithreaded builder, it may work. but it will give below warning message. you may need to see plugin documentation for multithreading support.
注意:您的某些插件可能与多线程构建器不兼容,但它可能会工作。但它会给出以下警告信息。您可能需要查看多线程支持的插件文档。
[WARNING] *****************************************************************
[WARNING] * Your build is requesting parallel execution, but project *
[WARNING] * contains the following plugin(s) that have goals not marked *
[WARNING] * as @threadSafe to support parallel building. *
[WARNING] * While this /may/ work fine, please look for plugin updates *
[WARNING] * and/or request plugins be made thread-safe. *
[WARNING] * If reporting an issue, report it against the plugin in *
[WARNING] * question, not against maven-core *
[WARNING] *****************************************************************
[WARNING] The following plugins are not marked @threadSafe in test-project:
[WARNING] de.dentrassi.maven:rpm:0.9.2
[WARNING] Enable debug to see more precisely which goals are not marked @threadSafe.
[WARNING] *****************************************************************
回答by Subodh Joshi
You can optimize the build by using of some small-2 trick like
您可以使用一些小技巧来优化构建,例如
- If you have written Junit Test and dont want to run test case every time you can use
-DskipTests=true
- Locally Install Nexus or Repositories
- You can adjust memory configurations to optimum for eg: add this line to
mvn.bat
setMAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m
- 如果你已经写过 Junit Test 并且不想每次都运行测试用例,可以使用
-DskipTests=true
- 在本地安装 Nexus 或存储库
- 您可以将内存配置调整为最佳,例如:添加此行以
mvn.bat
设置MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m
For more information you can check How to Speed Your Maven build
有关更多信息,您可以查看如何加速您的 Maven 构建
回答by ccreedon1
If using commandline you can check how many cores your machine has and use all of them, if you also want to skip your tests, you can add -DskipTests
For example, I have 8 core processor:
如果使用命令行,您可以检查您的机器有多少个内核并使用所有这些内核,如果您还想跳过测试,您可以添加-DskipTests
例如,我有 8 核处理器:
mvn -T 8C clean install -DskipTests
回答by Vladimir I
You can do a hacky way if you have some extra RAM as i do. And you don't need SSD. Because its sensitive for SSD to do a lot of read-writes. Even if you disable +a bit (MacOS - i don't remember if Win have similar issue). But
如果你像我一样有一些额外的 RAM,你可以做一个 hacky 的方式。而且您不需要SSD。因为它对SSD进行大量读写很敏感。即使您禁用 + 一点(MacOS - 我不记得 Win 是否有类似的问题)。但
1) create RAM drive (number of ways depends on your OS) and mount it to /Users/Username/volatile folder
1) 创建 RAM 驱动器(方式数量取决于您的操作系统)并将其安装到 /Users/Username/volatile 文件夹
2) locate maven-module-builder-X.X.X.jar in MVN_HOME/lib
2) 在 MVN_HOME/lib 中找到 maven-module-builder-XXXjar
3) in JAR modify file org/apache/maven/model/pom-4.0.0.xml by modifying
3) 在 JAR 中修改文件 org/apache/maven/model/pom-4.0.0.xml 通过修改
<build><directory>/Users/Username/volatile/${project.groupId}/${project.artifactId}/${project.version}/target
<build><directory>/Users/Username/volatile/${project.groupId}/${project.artifactId}/${project.version}/target
4) From now on All project compilations will be done in memory. And its a difference even in comparison with SSD.
4) 从现在开始所有项目编译都将在内存中完成。即使与SSD相比,它也有所不同。
Yes, its hack and some maven plugins can stop working BTW.
是的,它的 hack 和一些 Maven 插件可以停止工作 BTW。
回答by mourad m
On my actual project :
在我的实际项目中:
1/
mvn clean install
[INFO] Total time: 01:05 h
1/
mvn clean install
[INFO] 总时间:01:05 h
2/
mvn clean install -DskipTests
[INFO] Total time: 18:35 min
2/
mvn clean install -DskipTests
[INFO] 总时间:18:35 分钟
3/
mvn clean install -Dmaven.test.skip -DskipTests
[INFO] Total time: 10:58 min
3/
mvn clean install -Dmaven.test.skip -DskipTests
[INFO] 总时间:10:58 分钟
4/
mvn -T 1C clean install -Dmaven.test.skip -DskipTests
[INFO] Total time: 04:00 min
4/
mvn -T 1C clean install -Dmaven.test.skip -DskipTests
[INFO] 总时间:04:00 分钟
Also : Dont use * imports
另外:不要使用 * 进口
Remove all unused imports in your project
删除项目中所有未使用的导入
Remove all unused code (classes, variable, field, parameter, etc..)
删除所有未使用的代码(类、变量、字段、参数等)
Upgrade to last JAVA VERSION
升级到最新的 JAVA 版本
回答by user1416932
Apart from parallel run which many of the folks have already mentioned, try these maven options which would speed up build -XX:+TieredCompilation -XX:TieredStopAtLevel=1
除了许多人已经提到的并行运行之外,尝试这些可以加快构建速度的 Maven 选项 -XX:+TieredCompilation -XX:TieredStopAtLevel=1