Loading

Maven远程仓库优先级

零、结论

配置mirrorOf的值为*的mirror > active settings.profiles.profile > active pom.profiles.profile > pom.repositories.repository > 配置mirrorOf为其他值的mirror

settings.profiles和pom.profiles中,后声明的profile的优先级高于先声明的,后声明的profile找不到依赖时就去先声明的profile中去找;pom.repositories中,按repository的声明顺序去找

Maven依赖查找流程:

Maven查找依赖时,先从本地仓库找,找不到去配置的远程仓库找。如果配置了mirrorOf为*的mirror,则去该仓库找,找不到就报错,若没有配置mirrorOf为*的mirror,则依次按上面的顺序去找,找不到则报错

一、Maven设置远程仓库的四种方式

  1. settings.mirrors.mirror
  2. settings.profiles.profile:开启激活才生效,否则不生效
  3. pom.profiles.profile:开启激活才生效,否则不生效
  4. pom.repositories.repository

二、测试

1、准备

settings文件

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    ..........
    
    <!-- 配置一:settings.mirrors.mirror -->
    <mirrors>
        <mirror>
          <id>aliyun-maven-mirror</id>
          <mirrorOf>central</mirrorOf>
          <name>aliyun Maven</name>
          <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
        
        <mirror>
            <id>nexus-maven-mirror</id>
            <mirrorOf>*</mirrorOf>
            <name>private maven</name>
            <url>http://localhost:8081/repository/maven-public/</url>
        </mirror>
    </mirrors>

    <!-- 配置二:settings.profiles.profile -->
    <profiles>
        <!-- 配置阿里云Maven -->
        <profile>
            <id>maven-aliyun</id>
            <repositories>
                <repository>
                    <!-- 唯一标识远程仓库 -->
                    <id>aliyun-profile</id>
                    <name>aliyun maven</name>
                    <url>https://maven.aliyun.com/repository/public</url>
                    <!-- 远程仓库里的发布版本设置 -->
                    <releases>
                        <!-- 是否使用远程仓库的发布版本 -->
                        <enabled>true</enabled>
                        <!-- 更新远程仓库发布版本的频率:always-一直,daily-每日(默认),interval:X-X分钟,never-从不 -->
                        <updatePolicy>daily</updatePolicy>
                        <!-- maven验证构件检验文件失败时的处理方式:ignore-忽略,fail-失败,warn-警告 -->
                        <checksumPolicy>warn</checksumPolicy>
                    </releases>
                    <!-- 远程仓库里的快照版本设置 -->
                    <snapshots>
                        <enabled>false</enabled>
                        <updatePolicy>always</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
        </profile>

        <!-- 配置私服Maven:相关的私服验证信息已在servers中给出,这里为了简洁将其省略 -->
        <profile>
            <id>maven-nexus</id>
            <repositories>
                <repository>
                    <!-- 对于私服,需要配置用户密码,故此id必须与上面servers中声明的id一样 -->
                    <id>maven-releases</id>
                    <name>releases</name>
                    <url>http://localhost:8081/repository/maven-releases/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
                <repository>
                    <id>maven-snapshots</id>
                    <name>snapshots</name>
                    <url>http://localhost:8081/repository/maven-snapshots/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <!-- 手动激活profile -->
    <activeProfiles>
        <!-- activeProfile的属性值就是上面profiles列表种profile的id,若不存在则忽视 -->
        
        <!-- jdk-1.8已经自动激活,故此处无需显示指定激活 -->
        <!--<activeProfile>jdk-1.8</activeProfile>-->
        <activeProfile>maven-aliyun</activeProfile>
        <activeProfile>maven-nexus</activeProfile>
    </activeProfiles>
</settings>

pom文件

第二个和第三个依赖均位于私服中,此处设置的仓库nexus-maven-pom需要私服的验证,这些信息都在settings中给出(上面为粘贴),能保证连接到私服

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ttest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

        <dependency>
            <groupId>org.ob</groupId>
            <artifactId>json</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>tttttt</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>


    <!-- 配置三:pom.profiles.profile -->
    <profiles>
        <profile>
            <id>profile-test</id>
            <activation>
                <!-- 为方便测试此处设置自动激活 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <repositories>
                <repository>
                    <id>nexus-maven-pom-profile</id>
                    <url>http://localhost:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
    
    <!-- 配置三:pom.repositories.repository -->
    <!-- pom文件中设置的远程仓库 -->
    <repositories>
        <repository>
            <id>nexus-maven-pom</id>
            <url>http://localhost:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

2、过程

测试一

条件

配置文件如上所示,四个地方均配置了远程仓库地址

操作

执行编译(每次编译之前均要清空本地仓库

结果

从配置central的中央仓库(aliyun-maven-mirror )中下载maven相关依赖后,从配置*的仓库(nexus-maven-mirror)中下载依赖

image-20220319152430501

结论

当在settings.mirrors.mirros中配置mirrorOf为*后,直接使用此镜像,其他配置不生效,也就是说:配置mirrorOf为*的mirror的优先级最高

测试二

条件

将settings文件中配置的第二个mirror注释掉,如下:

<mirrors>
    <mirror>
        <!-- 唯一标识一个mirror -->
        <id>aliyun-maven-mirror</id>
        <!-- 指定该镜像代替的时那个仓库,例如central就表示代替官方的中央库,*表示所有仓库都是用该镜像,!表示该仓库除外
               <mirrorOf>*, ! central</mirrorOf> 表示所有的远程仓库 central除外,都使用该阿里云镜像
           -->
        <mirrorOf>central</mirrorOf>
        <name>aliyun Maven</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

<!--<mirror>-->
<!--    <id>nexus-maven-mirror</id>-->
<!--    <mirrorOf>*</mirrorOf>-->
<!--    <name>private maven</name>-->
<!--    <url>http://localhost:8081/repository/maven-public/</url>-->
<!--</mirror>-->
</mirrors>

操作

执行编译(编译之前均要清空本地仓库

结果

从配置central的中央仓库(aliyun-maven-mirror )中下载maven相关依赖后,从配置2:settings.profiles.profile配置的第二个仓库(maven-nexus)中下载依赖,而不是从settings.mirrors.mirro中下载依赖

image-20220319145812396

结论

(激活的)settings.profiles.profile的配置优先级第二

测试三

条件

将settings.profiles.profile的状态改为未激活,使其配置失效,如下:

<!-- 手动激活profile -->
<activeProfiles>
    <!-- activeProfile的属性值就是上面profiles列表种profile的id,若不存在则忽视 -->

    <!-- jdk-1.8已经自动激活,故此处无需显示指定激活 -->
    <!--<activeProfile>jdk-1.8</activeProfile>-->
    <!--<activeProfile>maven-aliyun</activeProfile>-->
    <!--<activeProfile>maven-nexus</activeProfile>-->
</activeProfiles>

操作

执行编译(编译之前要清空本地仓库

结果

先从配置central的中央仓库(aliyun-maven-mirror )中下载maven相关依赖后,再从配置3:pom.profiles.profile配置的仓库(nexus-maven-pom-profile)中下载依赖,而不是从settings.profiles.profile中下载依赖

image-20220319151354017

结论

(激活的)pom.profiles.profile的配置优先级第三

测试四

条件

将pom.profiles.profile的状态改为未激活(把activeByDefault的值设为false),使其配置失效,如下:

<profiles>
    <profile>
        <id>profile-test</id>
        <activation>
            <!-- 为方便测试此处设置自动激活 -->
            <activeByDefault>false</activeByDefault>
        </activation>
        <repositories>
            <repository>
                <id>nexus-maven-pom-profile</id>
                <url>http://localhost:8081/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
</profiles>

操作

清空本地仓库,执行编译

结果

先从配置central的中央仓库(aliyun-maven-mirror )中下载maven相关依赖后,再从配置4:pom.repositories.repository配置的仓库(nexus-maven-pom-profile)中下载依赖,而不是从pom.profiles.profile中下载依赖

image-20220319151623240

结论:

pom.repositories.repository的配置优先级第四

测试五

条件

删除配置四:pom.repositories.repository。另外,settings.profiles.profile和pom.profiles.profile的配置都不生效,就只有下面这一处中央仓库(central)的配置了

<mirrors>
    <mirror>
        <!-- 唯一标识一个mirror -->
        <id>aliyun-maven-mirror</id>
        <!-- 指定该镜像代替的时那个仓库,例如central就表示代替官方的中央库,*表示所有仓库都是用该镜像,!表示该仓库除外
               <mirrorOf>*, ! central</mirrorOf> 表示所有的远程仓库 central除外,都使用该阿里云镜像
           -->
        <mirrorOf>central</mirrorOf>
        <name>aliyun Maven</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

<!--<mirror>-->
<!--    <id>nexus-maven-mirror</id>-->
<!--    <mirrorOf>*</mirrorOf>-->
<!--    <name>private maven</name>-->
<!--    <url>http://localhost:8081/repository/maven-public/</url>-->
<!--</mirror>-->
</mirrors>

操作

清空本地仓库,执行编译

结果

只配置了这一个远程仓库,故只能从该仓库获取依赖。

结论

配置mirrorOf不为*的mirror的优先级最低。

posted @ 2022-03-19 18:14  OMaster  阅读(988)  评论(0编辑  收藏  举报