Maven:在pom.xml中配置并行构建 [英] Maven: configure parallel build in pom.xml
问题描述
Maven可以执行并行构建: https://cwiki.apache.org/confluence/display /MAVEN/Parallel + builds + in + Maven + 3
Maven has a capability to perform parallel builds: https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3
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
是否可以在pom.xml或settings.xml中指定此参数?重复此选项可能很烦人.
Is it possible to specify this arguments in pom.xml or settings.xml? Repeating this options could be annoying.
推荐答案
此解决方案有些破绽,但对我有用.它涉及指定一个新的环境变量,为其分配值-T3并将此变量添加到Maven启动脚本中.
This solution is a bit of a hack but worked for me. It involves specifying a new environment variable, assigning the value -T3 to it and adding this variable to the Maven launch script.
对于Windows(在Linux中为parens):
For Windows (Linux in parens):
- 打开环境变量"窗口:计算机->属性->高级系统设置->环境变量
- 添加具有所需值的属性 MAVEN_CMD_LINE_OPTS .就我而言,
-T 3
是我希望Maven使用3个线程并行构建. -
编辑 mvn.cmd 文件(在Linux中: mvn 文件).找到实际执行Java命令的部分,以%MAVEN_JAVA_EXE%开头的行(在Linux中:通常在定义主类的行之后: org.codehaus.plexus.classworlds.launcher .Launcher )
- Open the Environment Variables window: Computer -> Properties -> Advanced System settings -> Environment Variables
- Add the property MAVEN_CMD_LINE_OPTS with your desired value. In my case
-T 3
as I want Maven to use 3 threads to build in parallel. Edit the mvn.cmd file (In Linux: the mvn file). Find the part where the Java command is actually executed, the line starting with %MAVEN_JAVA_EXE% (In Linux: generally after the line defining the main class: org.codehaus.plexus.classworlds.launcher.Launcher)
在行尾添加%MAVEN_CMD_LINE_OPTS%(在Linux中:$ MAVEN_CMD_LINE_OPTS)
Add %MAVEN_CMD_LINE_OPTS% to the end of the line (In Linux: $MAVEN_CMD_LINE_OPTS)
在Maven项目上运行 mvn编译时,您现在将看到以下行:
When you run mvn compile on a Maven project you will now see the following line:
使用线程数为3的MultiThreadedBuilder实现
这具有用户能够覆盖"该值的优点.因此,如果用户执行 mvn -T4编译,则将使用4个线程,而不是默认的3个线程.
This has the advantage of the user being able to 'override' this value. So if the user executes mvn -T4 compile, then 4 threads are used instead of the default 3.
注意:
- 我在Maven 3.3.9上尝试过此方法,但该概念应在任何Maven上均可 版本.
- 多线程构建可能会遇到插件问题 特别是自定义插件不是线程安全的.因此,请谨慎使用 考虑将其禁用以解决问题.
- I tried this on Maven 3.3.9 but the concept should work on any Maven version.
- Multi-threaded builds can suffer from issues where plugins especially custom plugins are not thread safe. So use with care and consider disabling this as a fix in case of issues.
这篇关于Maven:在pom.xml中配置并行构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!