返回

懒人必备!解决Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom错误,一招搞定

后端

SpringBoot 噩梦:解决 Could not transfer artifact 错误

简介

初始化 SpringBoot 时,你是否曾经遇到过恼人的 Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom 错误?这可能令人抓狂,但别担心,本文将指导你完成解决此问题的步骤,一步一步地消除你的烦恼。

步骤一:网络连接检查

确保你的网络连接通畅。使用以下命令测试:

ping www.baidu.com

如果结果正常,则网络连接没问题。

步骤二:更换仓库地址

如果网络连接良好,尝试更换仓库地址。将默认的中央仓库替换为阿里云镜像:

<mirrors>
  <mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public/</url>
    <mirrorOf>central</mirrorOf>
  </mirror>
</mirrors>

添加到 Maven 的 settings.xml 文件中。

步骤三:更换中心仓库地址

如果更改仓库地址不起作用,请尝试更改中心仓库地址。将默认的中央仓库替换为阿里云镜像的中心仓库:

<repositories>
  <repository>
    <id>central</id>
    <name>Central Repository</name>
    <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

添加到 settings.xml 文件中。

步骤四:手动下载

如果以上步骤都失败,请尝试手动下载缺失的依赖项。从 Maven Central 下载 org.springframework.boot:spring-boot-starter-parent:pom,然后将其复制到本地 Maven 仓库中。

代码示例

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.7.6</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
</dependencies>

结语

恭喜你解决 Could not transfer artifact 错误!遵循这些步骤,你将不再被此错误困扰。记住,当你遇到此类问题时,保持冷静和条理清晰至关重要。希望本文对你的开发之旅有所帮助。

常见问题解答

  1. 为什么会出现 Could not transfer artifact 错误?

    这可能是网络连接问题、仓库地址不正确或中心仓库地址错误造成的。

  2. 更换仓库地址后为什么仍然遇到错误?

    确保你已更新 Maven 的 settings.xml 文件并重新运行 Maven 命令。

  3. 手动下载依赖项后如何添加它到项目中?

    将下载的依赖项 JAR 文件复制到本地 Maven 仓库中,然后在 pom.xml 文件中添加依赖项声明。

  4. 为什么我的代码中需要 spring-boot-starter-parent?

    它提供了 SpringBoot 的 BOM(管理对象模型),其中包含 SpringBoot 所有依赖项的版本信息。

  5. 还有什么其他解决 Could not transfer artifact 错误的方法?

    检查防火墙或代理设置,确保它们没有阻止 Maven 访问仓库。