Maven 在尝试连接到 Maven Central 仓库超时的解决方案和排查步骤

Maven 在尝试连接到 Maven Central 仓库时超时。以下是一些可能的解决方案和排查步骤:

1. 检查网络连接

确保你的网络连接正常,并且能够访问外网。

  • Ping 测试:

    sh 复制代码
    ping repo.maven.apache.org
  • Telnet 测试:

    sh 复制代码
    telnet repo.maven.apache.org 443

2. 配置代理

如果你在公司网络中使用代理服务器,需要配置 Maven 使用代理。

编辑 settings.xml

找到或创建 Maven 的 settings.xml 文件,通常位于 ~/.m2/settings.xml${MAVEN_HOME}/conf/settings.xml

添加代理配置:

xml 复制代码
<settings>
  <proxies>
    <proxy>
      <id>example-proxy</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.example.com</host>
      <port>8080</port>
      <username>proxyuser</username>
      <password>proxypass</password>
      <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

3. 使用镜像仓库

有时直接连接到 Maven Central 可能会很慢或不稳定,可以配置国内的镜像仓库来加速依赖下载。

编辑 settings.xml

找到或创建 Maven 的 settings.xml 文件,通常位于 ~/.m2/settings.xml${MAVEN_HOME}/conf/settings.xml

添加镜像配置:

xml 复制代码
<settings>
  <mirrors>
    <mirror>
      <id>nexus-aliyun</id>
      <name>Nexus Aliyun Mirror</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
</settings>

4. 清除本地缓存

有时候本地缓存可能导致问题,可以尝试清除本地仓库中的缓存。

删除本地仓库

删除或重命名本地 Maven 仓库目录,通常位于 ~/.m2/repository

sh 复制代码
rm -rf ~/.m2/repository/com/baomidou/mybatis-plus-core

然后重新运行 Maven 构建命令。

5. 增加超时时间

增加 Maven 的连接和传输超时时间。

编辑 settings.xml

找到或创建 Maven 的 settings.xml 文件,通常位于 ~/.m2/settings.xml${MAVEN_HOME}/conf/settings.xml

添加超时配置:

xml 复制代码
<settings>
  <profiles>
    <profile>
      <id>custom-timeouts</id>
      <properties>
        <http.connection.timeout>60000</http.connection.timeout>
        <http.socket.timeout>120000</http.socket.timeout>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>custom-timeouts</activeProfile>
  </activeProfiles>
</settings>

6. 使用 -U 参数强制更新依赖

使用 -U 参数强制 Maven 更新所有快照和发布版本的依赖。

sh 复制代码
mvn clean install -U

7. 检查防火墙和安全组设置

确保防火墙或安全组没有阻止对 repo.maven.apache.org 的访问。

8. 使用 VPN 或其他网络

如果上述方法都不奏效,可以尝试使用 VPN 或切换到其他网络环境进行构建。

示例:配置 Maven 使用阿里云镜像

以下是配置 Maven 使用阿里云镜像的具体步骤:

  1. 编辑 settings.xml :

    打开或创建 ~/.m2/settings.xml 文件。

  2. 添加镜像配置 :

    添加以下内容到 <settings> 标签内:

    xml 复制代码
    <settings>
      <mirrors>
        <mirror>
          <id>nexus-aliyun</id>
          <name>Nexus Aliyun Mirror</name>
          <url>https://maven.aliyun.com/repository/public</url>
          <mirrorOf>central</mirrorOf>
        </mirror>
      </mirrors>
    </settings>
  3. 保存并关闭文件

  4. 重新运行 Maven 构建:

    sh 复制代码
    mvn clean install

示例:配置 Maven 使用代理

以下是配置 Maven 使用代理的具体步骤:

  1. 编辑 settings.xml :

    打开或创建 ~/.m2/settings.xml 文件。

  2. 添加代理配置 :

    添加以下内容到 <settings> 标签内:

    xml 复制代码
    <settings>
      <proxies>
        <proxy>
          <id>example-proxy</id>
          <active>true</active>
          <protocol>http</protocol>
          <host>proxy.example.com</host>
          <port>8080</port>
          <username>proxyuser</username>
          <password>proxypass</password>
          <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
        </proxy>
      </proxies>
    </settings>
  3. 保存并关闭文件

  4. 重新运行 Maven 构建:

    sh 复制代码
    mvn clean install

总结

通过以上步骤,你应该能够解决 Maven 下载依赖时遇到的连接超时问题。如果问题仍然存在,建议检查以下几点:

  • 网络连接: 确保网络稳定。
  • 代理设置: 如果使用代理,确保配置正确。
  • 镜像仓库: 使用国内镜像仓库加快下载速度。
  • 防火墙和安全组: 确保没有阻止对 Maven Central 的访问。
相关推荐
lssjzmn5 分钟前
Spring Web 异步响应实战:从 CompletableFuture 到 ResponseBodyEmitter 的全链路优化
java·前端·后端·springboot·异步·接口优化
new_daimond18 分钟前
二级缓存在实际项目中的应用
java
一只乔哇噻25 分钟前
java后端工程师进修ing(研一版 || day41)
java·开发语言·学习·算法
User_芊芊君子43 分钟前
【Java】设计模式——单例、工厂、代理模式
java·设计模式·代理模式
2301_803554521 小时前
正向代理,反向代理,负载均衡还有nginx
java·nginx·负载均衡
要开心吖ZSH1 小时前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
向上的车轮1 小时前
基于Java Spring Boot的云原生TodoList Demo 项目,验证云原生核心特性
java·spring boot·云原生
程序员清风1 小时前
快手一面:为什么要求用Static来修饰ThreadLocal变量?
java·后端·面试
逍遥德1 小时前
Java8 Comparator接口 和 List Steam 排序使用案例
java·spring boot·list·排序算法
前行的小黑炭1 小时前
Android :如何快速让布局适配手机和平板?
android·java·kotlin