
本文详解 Maven 构建中“Failed to collect dependencies”错误的根源,重点解决因仓库配置不当(如混淆 repositories 与 pluginRepositories、缺失 Spring 相关仓库或 HTTP 镜像拦截)导致的依赖无法解析问题,并提供标准化、安全可用的 pom.xml 和 settings.xml 配置方案。
本文详解 maven 构建中“failed to collect dependencies”错误的根源,重点解决因仓库配置不当(如混淆 `repositories` 与 `pluginrepositories`、缺失 spring 相关仓库或 http 镜像拦截)导致的依赖无法解析问题,并提供标准化、安全可用的 `pom.xml` 和 `settings.xml` 配置方案。
在使用 Maven 构建 Spring Boot 项目(尤其是预发布版本如 2.3.0.RC1)时,常遇到类似以下错误:
Failed to collect dependencies at org.springframework.boot:spring-boot-starter-oauth2-client:jar:2.3.0.RC1 → org.springframework.security:spring-security-oauth2-client:jar:5.3.1.RELEASE → com.nimbusds:oauth2-oidc-sdk:jar:7.1.1 → com.nimbusds:nimbus-jose-jwt:jar:8.8 → net.minidev:json-smart:jar:[1.3.1,2.3]: No versions available for net.minidev:json-smart:jar:[1.3.1,2.3] within specified range
该错误表面是 net.minidev:json-smart 版本范围无法满足,实则根本原因是 Maven 无法从任何有效仓库获取该依赖——而根源往往在于仓库配置策略失当。
? 核心问题定位
pluginRepositories ≠ repositories
pluginRepositories 仅用于解析 Maven 插件(如 maven-compiler-plugin),而普通项目依赖(如 spring-boot-starter-*、nimbus-jose-jwt)必须通过声明的仓库下载。您当前 pom.xml 中仅将 spring-milestones 配置在 下,导致 Spring 生态的 release/milestone 依赖(包括其传递依赖 json-smart)完全不可见。 -
settings.xml 中的 HTTP 拦截镜像生效
您的 settings.xml 包含如下强制拦截配置:<mirror> <id>maven-default-http-blocker</id> <mirrorOf>external:http:*</mirrorOf> <url>http://0.0.0.0/</url> <blocked>true</blocked> </mirror>
此配置会主动阻断所有 http:// 协议仓库(即使 pom.xml 中未显式声明)。而部分旧版 Spring 仓库(如 http://repo.spring.io/release)仍使用 HTTP,若未在 pom.xml 中显式启用 HTTPS 替代,或未在 settings.xml 中排除该镜像,将直接导致依赖拉取失败。
-
缺少关键仓库覆盖范围
Spring 官方依赖(尤其是 RC、milestone、snapshot 版本)分散在多个专用仓库:- https://repo.spring.io/snapshot(快照版)
- https://repo.spring.io/milestone(里程碑版)
- https://repo.spring.io/release(正式 GA 版,推荐 HTTPS)
仅配置中央仓库(https://repo.maven.apache.org/maven2)不足以覆盖 Spring 生态全生命周期依赖。
✅ 推荐解决方案:完整仓库配置
请将以下配置同时更新至您的 pom.xml(注意:
<repositories>
<!-- Spring Snapshots -->
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
<!-- Spring Milestones -->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<!-- Spring GA (Release) -->
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled><updatePolicy>never</updatePolicy></releases>
</repository>
<!-- Central Repository (fallback) -->
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 对应插件仓库(确保插件也能解析) -->
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</pluginRepository>
</pluginRepositories>⚠️ 重要提醒:
- 所有 Spring 仓库 URL 必须使用 https://(非 http://),否则会被 maven-default-http-blocker 拦截;
- 若您确需保留 settings.xml 中的 HTTP 拦截镜像,请在
外添加 或直接移除该镜像(Maven 3.8.1+ 默认启用此 blocker,但现代项目应全面转向 HTTPS); - spring-boot-starter-oauth2-client:2.3.0.RC1 属于已废弃的旧版预发布包,强烈建议升级至稳定版(如 3.2.x 或 3.3.x),以避免兼容性与仓库支持问题。
? 验证与调试技巧
执行以下命令清理缓存并强制更新依赖,验证修复效果:
mvn clean dependency:purge-local-repository -DreResolve=false mvn compile -U
若仍报错,可启用详细日志定位具体哪个依赖卡住:
mvn compile -X 2>&1 | grep -A5 -B5 "json-smart"
✅ 总结
Maven 依赖解析失败,90% 源于仓库配置不完整或协议不匹配。正确做法是:
- ✅ 将 Spring 相关依赖仓库(snapshot/milestone/release)统一声明在
; - ✅ 对应插件仓库声明在
; - ✅ 全面采用 https:// 协议,规避 maven-default-http-blocker 干扰;
- ✅ 优先选用 Spring Boot 官方推荐的稳定版本,减少对预发布仓库的依赖。
遵循上述配置后,net.minidev:json-smart 等传递依赖将自动从 https://repo.spring.io/release 或中央仓库成功解析,构建流程恢复正常。










