使用Spring Tools Suite启动首个Spring应用遇POM依赖解析错误
首先来看你遇到的报错:
Project build error: Non-resolvable parent POM: Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.0.2.RELEASE from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.0.2.RELEASE from/to central (http://repo.maven.apache.org/maven2): connection timed out to http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.0.2.RELEASE/spring-boot-starter-parent-2.0.2.RELEASE.pom and 'parent.relativePath' points at no local POM
这个问题的核心是两个点:一是Maven连接中央仓库时超时导致依赖下载失败,二是Maven会缓存这个失败状态,默认不会自动重试,同时你的本地仓库也没有对应的父POM文件。下面是具体的解决步骤:
强制刷新Maven依赖缓存
这是最直接的方法,强制Maven忽略之前的失败缓存,重新尝试下载依赖:
在Spring Tools Suite中,右键你的项目 →Run As→Maven build...,在弹出的窗口里,Goals输入框中填写:clean install -U点击Run执行,
-U参数会强制Maven更新所有快照和依赖,跳过缓存的失败状态。检查网络与仓库配置
确认你的网络能够正常访问Maven中央仓库,如果是在公司内网或者网络受限环境,可能需要配置代理:
打开STS的Window→Preferences→Maven→User Settings,编辑对应的settings.xml文件,添加代理配置;或者考虑使用国内的Maven镜像仓库,这样下载速度更快也更稳定。手动放置父POM到本地仓库
如果强制更新还是无法下载,可以手动获取对应的父POM文件,然后放到本地Maven仓库的指定路径:
本地仓库路径一般是:- Windows:
C:\Users\你的用户名\.m2\repository\org\springframework\boot\spring-boot-starter-parent\2.0.2.RELEASE\ - Mac/Linux:
~/.m2/repository/org/springframework/boot/spring-boot-starter-parent/2.0.2.RELEASE/
将下载好的spring-boot-starter-parent-2.0.2.RELEASE.pom放入这个目录,然后刷新STS的Maven项目(右键项目 →Maven→Update Project...)。
- Windows:
补充:relativePath配置(如果本地有父POM)
如果你是在一个多模块项目中,本地存在父POM文件,可以在你的项目pom.xml的parent标签中添加relativePath属性,指向本地的父POM,比如:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath>../path/to/parent/pom.xml</relativePath> </parent>不过你的情况是本地没有这个POM,所以这个方法只适用于多模块场景。
内容的提问来源于stack exchange,提问作者Boris

