如何解决Maven中pom.xml里repositories不生效的问题

后端 潘老师 2个月前 (03-05) 113 ℃ (0) 扫码查看

在Maven的使用过程中,大家可能会碰到这样的困扰:明明在pom.xml文件里配置了仓库,可就是不生效。今天这篇文章,就专门来给大家讲讲这个问题,并且分享有效的解决办法。

一、问题描述

在Maven环境下进行项目构建时,我们常常需要配置依赖库的仓库地址。通常,我们会在pom.xml文件里进行相关配置。比如下面这段配置代码:

<repositories>
    <repository>
        <id>libs-snapshot</id>
        <name>libs-release</name>
        <url>仓库地址</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
    <repository>
        <id>libs-release</id>
        <name>libs-release</name>
        <url>仓库地址</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

这段代码的作用是配置了两个仓库,一个是用于快照版本(libs-snapshot)的仓库,另一个是用于发布版本(libs-release)的仓库 。配置完后,我们期望Maven能从这些指定的仓库中获取依赖。但实际情况却可能不尽如人意,配置好像没起作用。

与此同时,我们还需要知道,settings.xml文件在Maven中也起着重要作用。它里面的mirrors配置项,可以指定仓库的镜像地址。比如下面这样的配置:

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://192.168.69.166:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

这段代码表示配置了一个名为nexus的镜像,mirrorOf的值为*,意味着它会替代所有仓库的请求,Maven会从指定的http://192.168.69.166:8081/repository/maven-public/这个地址去获取依赖。这就可能导致我们在pom.xml里配置的仓库被覆盖,从而出现不生效的情况。

二、解决办法

想要让pom.xml里配置的repositories生效,我们可以对settings.xml里的mirrors配置进行调整。修改后的配置如下:

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>nexus</id>
      <mirrorOf>*,!libs-snapshot,!libs-release</mirrorOf>
      <url>http://192.168.69.166:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

在这个配置中,mirrorOf的值变为了*,!libs-snapshot,!libs-release 。这里的!表示排除的意思,也就是说,除了libs-snapshotlibs-release这两个仓库,其他仓库的请求还是会走nexus这个镜像地址,而libs-snapshotlibs-release则会按照pom.xml里配置的仓库地址去获取依赖,这样就保证了pom.xml里配置的这两个仓库能够生效。

三、总结

在Maven开发中,配置仓库既可以在pom.xml文件里进行,也可以在settings.xml文件里完成。具体选择哪种方式,主要取决于个人的习惯以及项目的实际需求。希望通过本文的介绍,大家以后再遇到pom.xmlrepositories不生效的问题,能够轻松解决。


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/back/15325.html
喜欢 (1)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】