如何让 maven 构建得更快?
2022-08-31 14:19:33
我有一个多模块java项目。Maven需要大约40秒才能构建它。我也尝试过使用多线程构建,方法是为要使用的线程和内核指定-T和-C参数。但是我没有看到我的构建的墙壁时间有任何显着的改善。
我正在使用maven 3.2.3,有时我需要非常频繁地构建我的项目。
我知道干净的目标需要很多时间,但我不能省略它。
建议请....
编辑:
注意:在我的情况下,清洁不需要太多时间。它在1秒内完成,安装需要其余的时间。
我有一个多模块java项目。Maven需要大约40秒才能构建它。我也尝试过使用多线程构建,方法是为要使用的线程和内核指定-T和-C参数。但是我没有看到我的构建的墙壁时间有任何显着的改善。
我正在使用maven 3.2.3,有时我需要非常频繁地构建我的项目。
我知道干净的目标需要很多时间,但我不能省略它。
建议请....
编辑:
注意:在我的情况下,清洁不需要太多时间。它在1秒内完成,安装需要其余的时间。
使用多线程运行 maven 构建对我而言可以加快构建速度。例如:
mvn clean install -T100
其中 -T 用于根据硬件指定所需的线程数。
Maven 3.x 具有执行并行构建的功能。该命令如下所示:
- mvn -T 4 全新安装 具有 4 个线程的构建
- mvn -T 1C 全新安装 每个 cpu 内核 1 个线程
- mvn -T 1.5C 全新安装 每个 cpu 内核 1.5 个线程
图中的每个节点代表多模块构建中的一个模块,“级别”只是表示到内部反应堆依赖关系图中第一个模块的距离。Maven 根据为多模块构建声明的模块间依赖项计算此图。请注意,父 maven 项目也是一个依赖项,这解释了为什么在大多数项目图的顶部有一个节点。反应堆外部的依赖性不会影响此图。
最后,如果你想跳过测试执行,你也可以使用-DskipTests。
谨慎:您的某些插件可能与多线程构建器不兼容,它可能会起作用。但它会给出以下警告消息。您可能需要查看插件文档以获取多线程支持。
[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] *****************************************************************
在我的实际项目中:
mvn clean install[信息]总时间: 01:05 小时
mvn clean install -DskipTests[信息]总时间:18 分 35 秒
mvn clean install -Dmaven.test.skip -DskipTests [信息]总时间:10 分 58 秒
mvn -T 1C clean install -Dmaven.test.skip -DskipTests[信息]总时间:04:00 分钟
mvn -T 1C clean install -Dmaven.test.skip -DskipTests -Dmaven.javadoc.skip=true
mvn -T 1C quickclean install -Dmaven.test.skip -DskipTestsmvn quickclean install...mvn trashclean以下是您需要添加到pom中的内容.xml如果您想使用此概念
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd-HH-mm</maven.build.timestamp.format>
<trashdir>trash/target-${maven.build.timestamp}</trashdir>
</properties>
<profile>
<id>quickclean</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>rename_target</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<move todir="${trashdir}" failonerror="false">
<fileset dir="target/"/>
</move>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>trashclean</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>clean_trash</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete dir="trash/" failonerror="false"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>