Maven配置文件加载问题

问题描述

在使用IDEA自定义了配置文件 如 settings-abc.xml
或者执行maven 命令时通过 -s/–settings 指定配置文件
编译打包报错
xxxxx.jar in settings.xml 中的仓库或者镜像地址 was cached
in the local repository, resolution will not be reattempted until the update interval of alimaven has elapsed or updates are forced

表现出的现象就是:
本地仓库有这个组件,但是远程仓库没有。

问题复现

这里情况是在 settings.xml 中原来也指定了镜像,配置了一些三方仓库
后来由于切换另一个环境,自定义了一个配置文件,指定了另外的镜像和仓库。
因此在实际开发时,就会使用 -s 或者 --settings 指定对应环境的配置文件,正常理解的话,自定义来了配置文件肯定会读取指定的那个配置文件里面的配置,但是事实并非如此

我们通过 mvn help:effective-settings 命令查看当前生效配置文件

我们在默认的配置文件中增加一项server配置

    <server>
        <id>aaa</id>
        <username>aaa</username>
        <password>aaa</password>
    </server>

在自定义的配置文件中增加另一项配置

    <server>
        <id>aaa</id>
        <username>bbb</username>
        <password>bbb</password>
    </server>

不指定配置文件

mvn help:effective-settings

可以看到生效的是默认的配置文件 settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <localRepository>E:\java\repository</localRepository>
  <servers>
    <server>
      <username>aaa</username>
      <password>***</password>
      <id>aaa</id>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <id>alimaven</id>
    </mirror>
  </mirrors>
  <pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>

指定配置文件

mvn -s D:\toolkit\apache-maven-3.5.4\conf\settings-abc.xml help:effective-settings

此时再看得到的结果

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
  <localRepository>E:\java\repository</localRepository>
  <servers>
    <server>
      <username>bbb</username>
      <password>***</password>
      <id>bbb</id>
    </server>
    <server>
      <username>aaa</username>
      <password>***</password>
      <id>aaa</id>
    </server>
  </servers>
  <mirrors>
    <mirror>
      <mirrorOf>*</mirrorOf>
      <name>xxx 私服镜像</name>
      <url>http://ip:port/repository/maven-public</url>
      <id>agg</id>
    </mirror>
    <mirror>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <id>alimaven</id>
    </mirror>
  </mirrors>
  <pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>

可以看到,虽然通过 -s 指定了配置文件,但是最终依然读到了默认的配置文件里面的配置

解决

根据MAVEN官方文档,这个配置读取大致有3个位置,以下优先级从低到高,加载顺序从上往下

  • ${maven.home}/conf/settings.xml
  • ${user.home}/.m2/settings.xml
  • -s / --settings 指定的 settings.xml

越往下,优先级越高;如果是冲突的配置,则覆盖,否则进行合并形成最终的配置文件

注意,这里的文件名为 settings.xml,如果 -s/–settings 指定的配置文件名称不为 settings.xml,最终结果会与默认的 settings.xml 内进行合并,就会出现本文的问题

所以,最终的解决方案是:直接干掉上面两个默认位置的settings.xml,所有环境都使用自定义名称的配置文件;当然如果有一些公共的配置(就是想在所有环境中都生效的配置)希望实现复用,还是可以抽取出来放到 settings.xml 中的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值