最直接有效的方式是修改 Maven 的 settings.xml,把中央仓库请求重定向到阿里云镜像,同时保留公司私服等私有仓库的直连能力——不改仓库配置、不破坏权限和认证,只加速公共依赖下载。

最直接有效的方式是修改 Maven 的 settings.xml,把中央仓库请求重定向到阿里云镜像,同时保留公司私服等私有仓库的直连能力——不改仓库配置、不破坏权限和认证,只加速公共依赖下载。
确认 settings.xml 位置并准备编辑
优先使用用户级配置(避免权限问题且不影响他人):
- Windows:
C:\Users\{用户名}\.m2\settings.xml - macOS/Linux:
~/.m2/settings.xml
如果文件不存在,可从 $MAVEN_HOME/conf/settings.xml 复制一份再修改。确保文件格式为 UTF-8,避免中文乱码或解析失败。
只镜像中央仓库(推荐企业/混合环境)
在 <settings> 标签下,找到或新建 <mirrors> 区块,插入以下配置:
<mirror> <id>aliyun-central</id> <name>Aliyun Central Mirror</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror>
关键点:<mirrorOf>central</mirrorOf> 表示仅拦截 ID 为 central 的仓库请求(即默认中央仓库),不会影响 nexus-private、company-snapshots 等自定义私服仓库,认证和路径全部保持原样。
验证是否生效
执行任意依赖下载命令(如 mvn compile 或 mvn dependency:resolve),观察控制台日志:
- 成功时会看到类似
Downloading from aliyun-central: https://maven.aliyun.com/... - 若仍显示
Downloading from central: https://repo.maven.apache.org/...,说明配置未加载或<mirrorOf>写错(比如误写成CentraL大小写敏感)
补充提速技巧(按需启用)
如果仍偶发超时或卡顿,可在 <settings> 根节点下添加网络调优参数:
<properties> <maven.wagon.httpconnectionManager.timeout>300000</maven.wagon.httpconnectionManager.timeout> <maven.wagon.http.retryHandler.count>3</maven.wagon.http.retryHandler.count> </properties>
这会将单次连接超时设为 5 分钟,并允许最多重试 3 次,对弱网或高延迟环境更友好。


















