Skip to content

Maven 3.8.1 及以上版本无法更新项目依赖 #215

Open
@Bpazy

Description

@Bpazy

问题描述

安装最新版本 ( 3.8.4 ) 的 Maven 之后,发现无法更新项目依赖了,出现了形如这种错误:

maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories

查阅资料发现,从 3.8.1 版本开始,Maven 为了修复 CVE-2021-26291 增加了禁止访问 HTTP 地址仓库的逻辑: https://maven.apache.org/docs/3.8.1/release-notes.html

禁用的方法是在 maven 的默认 settings.xml 中增加了新的配置:

<mirrors>
  <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>true</blocked>
  </mirror>
</mirrors>

此时当你访问任何 http 开头的仓库时,都会报错终止。

所以有两个解决办法:

方案1:将项目中所有的仓库地址改为 https

我尝试了,将本地用户配置 ~/.m2/settings.xml 中的地址全部改为 https:

<profile>
  <id>profile-f6-public</id>
  <repositories>
    <repository>
      <id>repo-f6</id>
      <url>https://maven.mynexus.com/repository/maven-public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>repo-f6</id>
      <url>https://maven.mynexus.com/repository/maven-public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
  <properties>
    <sonar.host.url>https://sonar.mynexus.com</sonar.host.url>
  </properties>
</profile>

再将项目中配置 ( pom.xml ) 的仓库地址改为 https:

<repositories>
  <repository>
    <id>repo-f6</id>
    <url>https://maven.mynexus.com/repository/maven-public/</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

再次尝试,一般情况应该就可以了。

但是!如果你依赖的 jar 中也定义了 http 形式的 repositories 咋办?比如某工程依赖的 utility 包, 这个依赖内部声明了 http 地址:

<pluginRepository>
  <id>erp</id>
  <name>erp</name>
  <url>http://maven.mynexus.com/content/groups/public/</url>
  <releases>
    <enabled>true</enabled>
  </releases>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</pluginRepository>

此时运行 maven 命令仍会报错,那该怎么办呢?

方案2:修改本地配置,允许http访问

修改本地配置 ~/.m2/settings.xml,允许 http 访问,上面也提到了 3.8.1 禁止 HTTP 访问的方式是新增了名叫 maven-default-http-blocker 的 mirror,那我们定义一个重名的 mirror 覆盖它即可,在配置文件中新增以下代码:

<!-- 允许 HTTP 仓库访问 -->
<mirrors>
  <mirror>
    <id>maven-default-http-blocker</id>
    <mirrorOf>dummy</mirrorOf>
    <name>Dummy mirror to override default blocking mirror that blocks http</name>
    <url>http://0.0.0.0/</url>
    <!-- 这里注释掉因为旧版本的 Maven 不支持该选项,默认值即 false -->
    <!-- <blocked>false</blocked> -->
  </mirror>
</mirrors>

此时万事大吉。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions