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

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 配置,如下图:
在这里插入图片描述
这里的 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 才可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isea533

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值