文章目录
-
- [1. 基础优化](#1. 基础优化)
- [2. 镜像源优化(国内推荐)](#2. 镜像源优化(国内推荐))
- [3. 插件仓库优化](#3. 插件仓库优化)
- [4. 并行构建提升 30%-80%](#4. 并行构建提升 30%-80%)
- [5. 下载可靠性优化](#5. 下载可靠性优化)
- [6. CI/CD 环境优化](#6. CI/CD 环境优化)
- [7. 进阶:依赖锁定与预下载](#7. 进阶:依赖锁定与预下载)
- [8. 实现效果](#8. 实现效果)
Maven settings.xml 终极优化指南,重点是:构建速度提升、依赖下载快、镜像源高效、插件管理智能、并行构建合理。
1. 基础优化
本地仓库配置
xml
<settings>
<!-- 本地仓库位置,建议放在 SSD 或 NVMe 磁盘 -->
<localRepository>/data/maven/repository</localRepository>
-
确保本地仓库在高速磁盘(如 SSD/NVMe),避免频繁 IO 瓶颈。
-
如果用 CI/CD,推荐在缓存目录挂载(如 GitLab Runner 的缓存,Jenkins 的共享 volume)。
2. 镜像源优化(国内推荐)
xml
<mirrors>
<!-- 阿里云 Maven 中央库 -->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 华为云 -->
<mirror>
<id>huaweicloud</id>
<mirrorOf>*</mirrorOf>
<url>https://repo.huaweicloud.com/repository/maven/</url>
</mirror>
<!-- 中央库备用 -->
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<url>https://repo.maven.apache.org/maven2/</url>
</mirror>
</mirrors>
-
推荐多配几个源,Maven 会按顺序尝试,减少单点故障。
-
对内网环境,可以搭建 Nexus3/Artifactory/Harbor-proxy 作为统一代理仓库。
3. 插件仓库优化
xml
<pluginGroups>
<pluginGroup>org.apache.maven.plugins</pluginGroup>
<pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>default-profile</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
-
避免插件解析失败导致的构建停顿。
-
插件仓库也需要镜像优化,不然会卡在 Downloading plugin ...。
4. 并行构建提升 30%-80%
在 settings.xml 中加:
xml
<profiles>
<profile>
<id>parallel-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 并行构建:每核 1-2 线程 -->
<maven.build.threadCount>4</maven.build.threadCount>
<maven.build.parallel>true</maven.build.parallel>
</properties>
</profile>
</profiles>
或者命令行:
bash
mvn -T 1C clean install
-
1C = 每核一个线程
-
1.5C = 每核 1.5 个线程,推荐在 CI/CD 构建机
5. 下载可靠性优化
xml
<settings>
<profiles>
<profile>
<id>download-optimizations</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 重试下载 -->
<maven.wagon.http.retryHandler.count>5</maven.wagon.http.retryHandler.count>
<!-- 连接超时 -->
<maven.wagon.http.connectionTimeout>30000</maven.wagon.http.connectionTimeout>
<!-- 读超时 -->
<maven.wagon.http.readTimeout>60000</maven.wagon.http.readTimeout>
</properties>
</profile>
</profiles>
</settings>
- 避免网络抖动导致的构建失败。
6. CI/CD 环境优化
依赖缓存:在 Jenkins/GitLab CI 用 cache/volume 挂载本地仓库目录。
分层 Docker 镜像:
dockerfile
bash
FROM maven:3.9.9-eclipse-temurin-17 AS build
COPY settings.xml /root/.m2/settings.xml
COPY pom.xml .
RUN mvn dependency:go-offline -B
COPY src ./src
RUN mvn package -DskipTests
这样能最大化缓存依赖,构建时间能减少 50%+。
7. 进阶:依赖锁定与预下载
-
依赖锁定:使用 maven-dependency-plugin 生成依赖清单,减少浮动下载。
-
预下载依赖:在开发机/CI 构建机定时执行:
bash
mvn dependency:go-offline -B
保证构建时本地已有依赖。
8. 实现效果
-
构建速度提升 2~5 倍(SSD、本地代理仓库、并行构建)。
-
网络失败率显著降低(多镜像源 + 重试机制)。
-
团队环境一致性提升(代理仓库 + 依赖锁定)。
附录;最优setting.xml模板
xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地仓库目录 (SSD路径最佳,可减少IO瓶颈) -->
<localRepository>/opt/maven/repo</localRepository>
<!-- 并行下载优化 (在 MAVEN_OPTS 中设置更优效果) -->
<!-- export MAVEN_OPTS="-Dmaven.artifact.threads=10 -Dmaven.wagon.httpconnectionManager.ttl=25 -Dmaven.wagon.http.retryHandler.count=3" -->
<mirrors>
<!-- 阿里云镜像 -->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>Aliyun Central Mirror</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 华为云镜像 -->
<mirror>
<id>huaweicloud</id>
<mirrorOf>central</mirrorOf>
<name>Huawei Cloud Maven</name>
<url>https://repo.huaweicloud.com/repository/maven/</url>
</mirror>
<!-- Apache Maven 官方中央仓库备份 -->
<mirror>
<id>maven-central</id>
<mirrorOf>central</mirrorOf>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
<!-- 企业 Nexus 私服 (如有) -->
<mirror>
<id>nexus-private</id>
<mirrorOf>*</mirrorOf>
<name>Enterprise Nexus</name>
<url>http://nexus.mycompany.com/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>aliyun-public</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 身份认证 (企业 Nexus/Artifactory 私服) -->
<servers>
<server>
<id>nexus-private</id>
<username>devops</username>
<password>${env.NEXUS_PASS}</password>
</server>
</servers>
</settings>
-
配合优化要点
- 本地仓库放到 SSD
xml
<localRepository>/opt/maven/repo</localRepository>
读写速度快,提升 20~30% 构建速度。
- 并行下载
- 在环境变量 MAVEN_OPTS 里设置:
bash
export MAVEN_OPTS="-Dmaven.artifact.threads=10 -Dmaven.wagon.http.retryHandler.count=3 -Dmaven.wagon.httpconnectionManager.ttl=25"
多线程下载依赖,速度提升显著(10-15倍)。
-
私服 Nexus/Artifactory
内网代理外部依赖,团队共享构建缓存,避免重复下载。
-
快照依赖优化
-nsu 参数跳过 SNAPSHOT 检查,减少网络请求:
bash
mvn clean install -T 4C -nsu
-T 4C 表示 按 CPU 核心数 4 倍开线程。

"人的一生会经历很多痛苦,但回头想想,都是传奇"。