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 的访问。
相关推荐
白宇横流学长10 分钟前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
fat house cat_37 分钟前
【redis】线程IO模型
java·redis
stein_java2 小时前
springMVC-10验证及国际化
java·spring
weixin_478689762 小时前
C++ 对 C 的兼容性
java·c语言·c++
LUCIAZZZ2 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
sky_ph2 小时前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端
IDRSolutions_CN3 小时前
PDF 转 HTML5 —— HTML5 填充图形不支持 Even-Odd 奇偶规则?(第二部分)
java·经验分享·pdf·软件工程·团队开发
hello早上好3 小时前
Spring不同类型的ApplicationContext的创建方式
java·后端·架构
HelloWord~4 小时前
SpringSecurity+vue通用权限系统2
java·vue.js
让我上个超影吧4 小时前
黑马点评【基于redis实现共享session登录】
java·redis