java相关的技术文章收录

【maven】仓库的优先级顺序

2020-03-06  本文已影响0人  BenjaminY

在maven中,仓库可以分为:本地仓库、远程仓库。
远程仓库可以分为:中央仓库、私服仓库。
中央仓库是maven官方指定的仓库,可以理解为“寻找的最后一站”。
私服仓库可以是自己建的,也可以是其它主体建的(比如aliyun的maven仓库,jboss的maven仓库等)。
私服可以分为:全局应用的私服仓库、应用到项目自身的私服仓库。

maven寻找得顺序大致可以理解为:
1,在本地仓库中寻找,如果没有则进入下一步。
2,在全局应用的私服仓库中寻找,如果没有则进入下一步。
3,在项目自身的私服仓库中寻找,如果没有则进入下一步。
4,在中央仓库中寻找,如果没有则终止寻找。

补充:
1,如果在找寻的过程中,如果发现该仓库有镜像设置,则用镜像的地址代替。
2,如果仓库的id设置成“central”,则该配置会覆盖maven默认的中央仓库配置。

以上,通过实践得来的,可能不全面,仅当参考。

科普一下几种仓库的设置:

pom中的repository:

<repositories>
    <repository>
        <id>dsdf</id>
        <releases>
            <enabled>true</enabled>
        </releases>
        <url>http://222.197.XXXXXX/nexus/content/groups/public/</url>
    </repository>
</repositories>

profile中的仓库是在maven的设置文件(maven安装目录/conf/settings.xml)

<profile>
    <id>nexus</id>
    <repositories>
        <repository>
            <id>sonatype-forge</id>
            <url>http://repository.sonatype.org/content/groups/forge/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>sonatype-forge</id>
            <url>http://repository.sonatype.org/content/groups/forge/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>

使用下面代码来激活profile

<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>

maven profile也是有优先级别

  1. 在settings.xml中的profile优先级高于pom中的
  2. 同在settings.xml的properties,如果都激活了,根据profile定义的先后顺序来进行覆盖取值,后面定义的会覆盖前面,其properties为同名properties中最终有效。并不是根据activeProfile定义的顺序 。
  3. 如果有user setting和globel settings,则两者合并,其中重复的配置,以user settings为准。
上一篇 下一篇

猜你喜欢

热点阅读