setting.xml中mirror和repository的关系

这两天在连公司的两个不同的私服,但是总是出现JAR包不能全部下载下来的问题。经过查资料,大概有了点理解,记录下

1、Jar包分布

公司的快照JAR全放在http://*:8081/*/libs-snapshot上

其他第三方包全放在http://*:8081/*/libs-public-local上

2、问题描述

不能同时从两个地方下载到所有的JAR包

3、问题解决

经过不断的摸索和查询信息,最终能同时从两个地方拉取JAR包,配置如下:

<mirrors>
      <mirror>
          <id>snapshots</id>
          <mirrorOf>snapshot</mirrorOf>
          <name>snapshots</name>
          <url>http://*:8081/*/libs-snapshot</url>
      </mirror>
      <mirror>
          <id>public</id>
          <mirrorOf>*,!snapshot</mirrorOf>
          <name>public</name>
          <url>http://*:8081/*/libs-public-local</url>
      </mirror>
  </mirrors>
<profiles>
    <profile>
      <id>xyz</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
            <id>libs-public</id>
            <name>libs-public</name>
            <url>http://*:8081/*/libs-public-local</url>
        </repository>
        <repository>
          <id>snapshot</id>
          <url>http://*:8081/*/libs-snapshot</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>ali</id>
          <url>http://maven.aliyun.com/nexus/content/groups/public</url>
          <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>

      </pluginRepositories>
    </profile>
<activeProfiles>
    <activeProfile>xyz</activeProfile>
  </activeProfiles>

4、个人理解

在配置多个远程仓库的时候,需要借助repository和mirror来配置,仓库指定了什么样的包走这个仓库ID进行下载,而mirror则对仓库进行了地址代理,对应ID的包都走这个镜像。

其中 mirrorof配置非常关键,指定了仓库ID,其配置有多种,可以看官网,简单摘抄点

    * = everything 所有的仓库都走这个镜像
    external:* = everything not on the localhost and not file based. 不在本地,且不一file: 开头的东东,反正是外部的
    repo,repo1 = repo or repo1  或
    *,!repo1 = everything except repo1 出repo1仓库之外的其他仓库

另外,网上还有种说法是:配置了mirror,repository就不起作用了,我觉得可能说法有问题,当且仅当mirrorof配置成  *  时,才是那样的结果,其实也不是不起作用了,只不过mirror把所有repository都给“代理”了。另一种说法是多个mirror时,只有第一次才生效,其实也不完全正确,只有当所有的mirrorof相同的时候,说法才正确。官网的说法是

Note that there can be at most one mirror for a given repository. In other words, you cannot map a single repository to a group of mirrors that all define the same <mirrorOf> value. Maven will not aggregate the mirrors but simply picks the first match.

还有,当mirrorof不能精确匹配时,maven会选取第一个镜像

When Maven looks for a mirror of some repository, it first checks for a mirror whose <mirrorOf> exactly matches the repository identifier. If no direct match is found, Maven picks the first mirror declaration that matches according to the rules above (if any). Hence, you may influence match order by changing the order of the definitions in the settings.xml

5、遗留问题

在setting中配置多个仓库后,maven怎么分配哪个包从哪个仓库下呢,我大概知道点是配置repository的 Activation。但是不知道的是如果不配置Activation(如下例),那怎么区分呢,希望知道的小伙伴说下,多谢了

什么时候走 public? 什么时候走ali
<repository>
    <id>libs-public</id>
    <name>libs-public</name>
    <url>http://*:8081/*/libs-public-local</url>
</repository>
<repository>
  <id>ali</id>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>

原文地址:https://www.cnblogs.com/lngo/p/11051148.html

时间: 2024-08-23 20:40:03

setting.xml中mirror和repository的关系的相关文章

maven的setting配置文件中mirror和repository的区别

repository就是个仓库 maven里有两种仓库,本地仓库和远程仓库.远程仓库相当于公共的仓库,大家都能看到. 本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用. 当你向仓库请求插件或依赖的时候,会先检查本地仓库里是否有.如果有则直接返回,否则会向远程仓库请求,并做缓存. 远程仓库可以在工程的pom.xml文件里指定. 如果没指定,默认会到http://repo1.maven.org/maven2这个地方去请求插件和依赖包. <repository> <snapshots

4、Maven的配置中mirror和repository的区别与联系

1.Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录 1.2 Remote Repository主要有3种: 中央仓库:http://repo1.maven.org/maven2/ 私服:内网自建的maven repository,

Maven使用私服setting.xml中的配置

Maven使用私服setting.xml中的配置 setting.xml中的配置 使用maven,每次都是从中央仓库下载依赖包,网络很渣,只有使用Nexus搭建个私服. 下面是setting.xml中的配置文件 <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi=&quo

Maven3在setting.xml中配置代理

在特殊的网络策略环境下,可能要需要开发人员通过代理服务器来访问互联网.此时就需要给Maven配置代理服务来访问仓库.更新相关资源. 打开setting.xml文件 <proxies> <proxy> <id>myProxy</id> <active>true</active> <protocol>http</protocol> <host>123.123.123.123</host> &

在SqlMapConfig.xml 中&lt;typeAliases&gt;与&lt;properties&gt;的关系

先来个正确的写法 <properties resource="jdbc.properties"/> <!-- 设置别名 --> <typeAliases> <!-- <typeAlias type="com.mybatis.review.entity.User" alias="User"/> --> <package name="com.mybatis.review&qu

Maven:mirror和repository 区别

1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录 1.2 Remote Repository主要有3种: 中央仓库:http://repo1.maven.org/maven2/ 私服:内网自建的maven repository,

Maven的Mirror和Repository

今天新公司入职,项目经理让迁出项目,心想maven的阿里镜像源挺快的,干脆在配置了公司私服之后自己配置了阿里的镜像源,没成想项目屡屡报错,找不到项目依赖的公司jar包,后来才发现,同事配置mirror和repository后repository就不生效了. 1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository:存放在本地磁盘的一个文件夹,例如,win

Maven的Mirror和Repository 的详细讲解

1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录 1.2 Remote Repository主要有3种: 中央仓库:http://repo1.maven.org/maven2/ 私服:内网自建的maven repository,

Maven:mirror和repository

1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录 1.2 Remote Repository主要有3种: 中央仓库:http://repo1.maven.org/maven2/ 私服:内网自建的maven repository,