Skip to content

Maven 依赖解析异常:解决 Blocked mirror for repositories 错误

问题描述

当使用 Maven 3.8.1 及以上版本执行 mvn clean package 时,可能会遇到以下错误:

[ERROR] Failed to execute goal on project MassBank2NIST: Could not resolve dependencies for project MassBank2NIST:MassBank2NIST:jar:0.0.2-SNAPSHOT: Failed to collect dependencies at edu.ucdavis.fiehnlab.splash:core:jar:1.8: Failed to read artifact descriptor for edu.ucdavis.fiehnlab.splash:core:jar:1.8: Could not transfer artifact edu.ucdavis.fiehnlab.splash:core:pom:1.8 from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [EBI (http://www.ebi.ac.uk/intact/maven/nexus/content/repositories/ebi-repo/, default, releases+snapshots), releases (http://gose.fiehnlab.ucdavis.edu:55000/content/groups/public, default, releases+snapshots)]

这个错误表明 Maven 阻止了不安全的 HTTP 仓库连接,这是从 Maven 3.8.1 版本开始引入的安全特性。

根本原因

Maven 3.8.1 及以上版本默认阻止所有 HTTP 仓库访问,强制使用 HTTPS 以确保安全性。这一变化在 Maven 3.8.1 发行说明中明确说明。

安全提醒

虽然本文提供绕过此限制的方法,但强烈建议优先使用 HTTPS 仓库以确保依赖下载的安全性。

解决方案

方案一:升级依赖仓库为 HTTPS(推荐)

检查项目 pom.xml 中的仓库配置,将 HTTP URL 改为 HTTPS:

xml
<repositories>
    <repository>
        <id>wso2-nexus</id>
        <!-- 将 http:// 改为 https:// -->
        <url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </repository>
</repositories>

方案二:全局配置允许 HTTP 仓库

编辑全局 Maven 配置文件 ${MAVEN_HOME}/conf/settings.xml,注释或移除默认的 HTTP 拦截器:

xml
<!-- 注释或删除以下镜像配置 -->
<!--
<mirror>
    <id>maven-default-http-blocker</id>
    <mirrorOf>external:http:*</mirrorOf>
    <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
    <url>http://0.0.0.0/</url>
    <blocked>false</blocked>
</mirror>
-->

方案三:用户级配置允许特定 HTTP 仓库

~/.m2/settings.xml 中添加特定仓库的镜像配置:

xml
<settings>
    <mirrors>
        <mirror>
            <id>insecure-ebi-repo</id>
            <mirrorOf>ebi-repo</mirrorOf>
            <url>http://www.ebi.ac.uk/intact/maven/nexus/content/repositories/ebi-repo/</url>
            <blocked>false</blocked>
        </mirror>
    </mirrors>
</settings>

方案四:项目级配置(适合团队协作)

在项目根目录创建 .mvn 文件夹,添加自定义配置:

  1. 创建 .mvn/custom-settings.xml
xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
    <mirrors>
        <mirror>
            <id>insecure-repo-mirror</id>
            <mirrorOf>external:http:*</mirrorOf>
            <url>http://www.ebi.ac.uk/intact/maven/nexus/content/repositories/ebi-repo/</url>
            <blocked>false</blocked>
        </mirror>
    </mirrors>
</settings>
  1. 创建 .mvn/maven.config
--settings .mvn/custom-settings.xml

或者直接运行:

bash
mvn clean install --settings .mvn/custom-settings.xml

方案五:降级 Maven 版本(临时方案)

如果上述方法都不适用,可以暂时降级到 Maven 3.6.3:

  1. 下载 Maven 3.6.3:https://downloads.apache.org/maven/maven-3/3.6.3/binaries/
  2. 安装并配置为默认 Maven 版本

不推荐

降级 Maven 只是临时解决方案,不建议长期使用,因为它会降低项目安全性。

最佳实践

  1. 优先使用 HTTPS 仓库:联系仓库维护者要求提供 HTTPS 支持
  2. 检查传递依赖:即使你的项目使用 HTTPS,传递依赖可能仍引用 HTTP 仓库
  3. 团队统一配置:使用项目级配置确保团队成员环境一致性
  4. 定期更新依赖:新版本依赖通常会修复不安全仓库问题

总结

Maven 3.8.1+ 的 HTTP 仓库拦截是为了提高安全性。虽然可以通过配置绕过此限制,但建议优先选择 HTTPS 仓库解决方案。如果必须使用 HTTP 仓库,推荐使用方案三或方案四进行有针对性的配置,而不是完全禁用安全特性。

通过正确配置 Maven,您可以平衡安全需求与项目依赖管理的要求,确保构建过程既安全又可靠。