spring-cloud-starter-gateway依赖拉取失败问题求助
构建Spring Boot项目时,spring-cloud-starter-gateway依赖拉取失败,错误信息如下:
org.springframework.cloud:spring-cloud-starter-gateway:pom:4.0.0 failed to transfer from https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.cloud:spring-cloud-starter-gateway:pom:4.0.0 from/to central (https://repo.maven.apache.org/maven2): transfer failed for https://repo.maven.apache.org/maven2/org/springframework/cloud/spring-cloud-starter-gateway/4.0.0/spring-cloud-starter-gateway-4.0.0.pom
项目的pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>net.javaguides</groupId> <artifactId>api-gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>api-gateway</name> <description>Demo project for Spring Boot api-gateway</description> <properties> <java.version>17</java.version> <spring-cloud.version>2022.0.0</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
从错误信息来看,核心问题是Maven缓存了之前的拉取失败记录,同时可能存在网络或镜像访问问题,按以下步骤排查:
强制刷新Maven依赖
执行命令强制更新依赖并忽略缓存的失败记录:mvn clean install -U-U参数会强制Maven更新所有快照依赖,并重新尝试拉取之前失败的构件。清理本地Maven缓存
如果强制刷新无效,手动删除本地仓库中对应依赖的缓存目录:- 找到本地Maven仓库路径(默认:Windows为
C:\Users\<你的用户名>\.m2\repository,Linux/Mac为~/.m2/repository) - 删除路径
org/springframework/cloud/spring-cloud-starter-gateway/4.0.0下的所有文件 - 重新执行
mvn clean install
- 找到本地Maven仓库路径(默认:Windows为
切换国内Maven镜像
中央仓库访问不稳定时,配置阿里云镜像加速:
在Maven的settings.xml(路径:~/.m2/settings.xml或Maven安装目录/conf/settings.xml)中添加镜像配置:<mirrors> <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>确认版本兼容性
你的版本配置是兼容的:Spring Boot 3.0.x对应Spring Cloud 2022.0.x(Kilburn版本),spring-cloud-starter-gateway:4.0.0是该Cloud版本下的正确版本,无需调整版本号。排查网络与代理
- 确认网络可以正常访问Maven仓库地址
- 关闭本地代理或VPN,避免网络拦截
- 检查防火墙是否限制了Maven的网络请求
内容的提问来源于stack exchange,提问作者jagan mohan Mylapuru

