maven 3.8.1 版本之后,在 settings.xml 中通过配置 mirror 禁用了不安全的 http 链接,默认情况下必须使用 https 的仓库地址。

想要使用 http 方式,需要禁用 settings.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>true</blocked>
</mirror>

为了方便Maven自动读取用户配置,我通常会把默认的 ​​settings.xml​​​ 配置文件复制到用户目录的 ​​.m2​​ 下面,为了使用 http 仓库,把用户目录下的配置修改后,发现 IDEA 中可以用了。

后来使用 maven 的 docker 镜像时,按照官方文档配置 settings.xml 后发现无论如何都没用,后来在本地通过 ​​-s settings.xml​​​ 指定配置文件时,发现也是无效。最后想着把 ​​apache-maven-3.8.4\conf\settings.xml​​ 修改了再试试,结果就可以了。

上面各种配置时,一直没有动 ​​apache-maven-3.8.4\conf\settings.xml​​​ 这个配置文件,改了这里才好,因此猜测 Maven 对 ​​settings.xml​​​ 配置文件的处理策略肯定是合并,搜索资料没找到官方的,找到了以下​​资料​​:

A settings.xml file is usually found in a couple of places:

  • Global settings in Mavens home directory: ${maven.home}/conf/settings.xml
  • User settings in the user’s home: ${user.home}/.m2/settings.xml

If both files exist, their contents are merged. Configurations from the user settings take precedence.

可以看到是两个配置合并,用户配置优先。

当通过 ​​-s​​ 指定配置文件时,指定的这个会作为用户配置,可以通过下面命令查看当前使用的哪个配置文件:

​mvn -s settings.xml -X clean | findstr settings​​​ [DEBUG] Imported: org.apache.maven.settings < plexus.core
[DEBUG] Reading global settings from D:\Program Files\apache-maven-3.8.4\conf\settings.xml
[DEBUG] Reading user settings from D:\GitLab\tmp\settings.xml

在 Docker 镜像中执行命令时,输出如下:

​root@e267c541e6e3:~/tmp# mvn -X clean | grep setting​​​ [DEBUG] Imported: org.apache.maven.settings < plexus.core
[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml

了解这两层配置的位置和用途后,以后在遇到配置不起作用时就有法解决了。

但是,为什么 IDEA 中改了本地配置就能用了呢?

打开 IDEA 的 Maven 配置,如下图:

Maven 3.8.x 版本遇到的一个坑_xml


这里的 User settings file 就是一开始配置改对的,和直接在命令行运行 ​​mvn​​ 不同的时,这里用的 IDEA 内置的 Maven。在 IDEA 中执行相同的命令输出如下:

[DEBUG] Reading global settings from D:\Program Files\ideaIU-2021.2.2.win\plugins\maven\lib\maven3\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\Administrator.m2\settings.xml

查看内置的配置文件后,里面没有禁用 http 的配置,所以两个配置文件合并后是可以使用 http 镜像库的。

使用 Maven 的 Docker 镜像时,必须修改 ​​/usr/share/maven/conf/settings.xml​​ 才可以。