maven配置让pom.xml中的repositories生效

138 阅读1分钟

前言

在使用maven环境中,有时候在pom.xml配置,发现配置的<repositories>不生效

pom.xml配置仓库

有时候在配置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>

settings.xml中配置了

  <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>

这个时候,可以这么配置配置

      <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>

配置!libs-snapshot,!libs-release,这样配置libs-releaselibs-snapshot不会从settings.xml配置仓库中查找

总结

开发中使用`pom.xml`配置仓库,或者直接从`settings.xml`配置,看个人习惯和需求