Maven默认从中央仓库下载依赖会卡住,因每次解析都强制访问外网且中央仓库对国内限速、不支持断点续传;即使本地有缓存或配置镜像,仍需外网连通,无法解决私有包分发问题。

为什么 Maven 默认走中央仓库会卡在 Downloading 状态
不是网络差,而是 Maven 默认配置下,每次 resolve 依赖都会先查 https://repo.maven.apache.org/maven2/,哪怕你本地 .m2/repository 已有对应 JAR。更关键的是,中央仓库对国内多数出口带宽做了限速,且不支持断点续传——Connection reset 或 Read timed out 是常态。
常见错误现象:mvn clean compile 卡在某个 Downloading xxx.jar from https://repo.maven.apache.org 超过 2 分钟;或反复重试后报 Could not transfer artifact。
- 别指望改
settings.xml里<mirrors>加个阿里云镜像就一劳永逸——镜像只是缓存代理,仍需外网连通,且无法解决公司内部私有包分发问题 - 局域网私服(如 Nexus / Artifactory)本质是把远程仓库“搬进内网”,所有依赖请求都落在本地千兆/万兆链路上,延迟通常
- 注意:私服 ≠ 本地仓库。本地
.m2是单机缓存;私服是多项目共享的中心源,必须显式配置才能生效
怎么让 Maven 项目强制走局域网 Nexus 私服
核心动作只有两步:改全局 settings.xml(影响所有本机 Maven 执行),再确保项目 pom.xml 不覆盖该配置。
实操建议:
立即学习“Java免费学习笔记(深入)”;
- 编辑
${M2_HOME}/conf/settings.xml或${USER_HOME}/.m2/settings.xml,在<profiles>下添加 profile:
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.your-company.local/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.your-company.local/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
- 紧接着在
<activeProfiles>中激活它:<activeProfile>nexus</activeProfile> - 检查项目
pom.xml是否硬编码了<repository>—— 如果有,删掉或注释掉,否则会覆盖settings.xml配置 - 验证是否生效:运行
mvn help:effective-settings,确认输出中activeProfiles包含nexus;再执行mvn dependency:tree -Dverbose,看坐标解析来源是否变成你的私服地址
私服 URL 里的 /repository/maven-public/ 是什么
这是 Nexus 3 的默认代理组(group)路径,不是固定写死的。不同公司部署时可能叫 maven-central、company-all,甚至直接用根路径 /。
容易踩的坑:
- 直接复制网上教程的 URL,但实际 Nexus 管理员建的是
http://nexus.company.com/repository/maven-releases/—— 这个路径只允许下载 release 版本,SNAPSHOT会 404 - 误用
http://nexus.company.com/repository/maven-snapshots/当作全量源——它不包含中央仓库的依赖,只存你们自己 deploy 的快照包 - 正确做法:找运维要“统一聚合仓库”的 URL,通常是
repository/xxx类型的 group,背后已 proxy 了 central、jcenter、spring-milestones 等多个远程源 - 如果 URL 末尾漏了斜杠(如
/maven-public),Maven 会拼出错路径,报404 Not Found,而不是连接超时
为什么改完 settings.xml 还是去下中央仓库
最常被忽略的两个点:IDE 没同步配置、Maven 版本太老。
排查步骤:
- IntelliJ 用户:进入
Settings → Build → Build Tools → Maven,确认User settings file指向你刚改的那个settings.xml,且Local repository路径没被手动改成其他位置 - Eclipse 用户:右键项目 →
Maven → Update Project…勾选Force Update of Snapshots/Releases,否则 IDE 缓存了旧 resolution 结果 - 检查 Maven 版本:
mvn -v。Maven 3.0.x 对<mirrorOf>*的匹配逻辑有 bug,建议升到 3.6.3+ 或 3.8.6+ - 临时加参数验证:
mvn clean compile -X 2>&1 | grep "Using transporter",看日志里实际请求的 URL 是不是你的私服地址
复杂点在于:私服本身也可能配置了低效的远程代理策略,比如没开缓存、没设 HTTP 连接池复用,或者上游源(如中央仓库)响应慢拖累了整体速度——这时候光改客户端没用,得协同运维调优 Nexus。










