Maven仓库加载顺序

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Maven仓库加载顺序

情愿   2022-05-23 我要评论

Maven仓库一般分为本地仓库和远程仓库。远程仓库又分为私服、中央仓库、中央仓库的镜像仓库。

本地仓库就是本地维护的maven仓库,仅为本机项目提供服务。

私服一般是公司或组织在局域网级别搭建的maven仓库,服务范围是公司或组织局域网内的成员。

镜像仓库,这里指的是maven中央仓库的镜像仓库。分布在全球各个地方,是maven中央仓库的镜像备份。镜像仓库的作用一是分摊中央仓库的访问压力,第二就是可以提升我们下载依赖的速度。常用的镜像仓库有阿里云镜像仓库等等。

那么在实际开发中,在配置了多个仓库的情况下,他们之间的加载访问顺序是怎么样的呢?
本地仓库 》 私服 》 镜像仓库

可以通过配置来验证一下:

pom.xml配置仓库:

 <repositories>
  <repository>
   <id>local-nexus</id>
   <url>http://xxx.xxx.xxx.135:8081/nexus/repository/maven-group-snapshots/</url>
   <releases>
    <enabled>true</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
  <repository>
   <id>ali-maven</id>
   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
   <releases>
    <enabled>true</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
 </repositories>

setting.xml配置仓库:

   <!-- 这里配置的阿里云的镜像仓库来替代maven中央仓库 -->
  <mirrors>
    <mirror>  
      <id>alimaven</id>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>          
    </mirror>
  </mirrors>

compile一下:

...
//优先加载本地仓库
[DEBUG] Using local repository at D:\programs\apache-maven-3.6.1-bin\notify_repo
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for D:\programs\apache-maven-3.6.1-bin\notify_repo
[INFO] Scanning for projects...
//这里使用setting.xml配置的阿里云中央仓库代替maven中央仓库
[DEBUG] Using mirror alimaven (http://maven.aliyun.com/nexus/content/groups/public/) for central (https://repo.maven.apache.org/maven2).

...
...

//可以看到这是远程仓库加载的顺序,先是加载pom.xml配置的135私服,然后是ali-maven,最后才是setting.xml中的alimavn,在pom.xml中,可以通过调整<repository>的顺序来调整私服的加载顺序
[DEBUG] Repositories (dependencies): 
[local-nexus (http://xxx.xxx.xxx.135:8081/nexus/repository/maven-group-snapshots/, default, releases+snapshots), 
ali-maven (http://maven.aliyun.com/nexus/content/groups/public/, default, releases+snapshots), 
alimaven (http://maven.aliyun.com/nexus/content/groups/public/, default, releases)]

...

这里说的、以及setting.xml中的并不能代表实际中出现的所有配置,具体加载顺序还是得看具体的项目和maven配置,这些都不是重点,重点是可以通过日志来知道实际是什么情况的。

最后,如何在idea中开启maven的debug级别日志:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们