maven 加载顺序

转载 http://uoften.com/article/216374.html

概述

本文主要是关于Maven仓库加载顺序的实例解析和相关实例

 

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

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

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

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

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

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

pom.xml配置仓库:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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配置仓库:

复制代码
1
2
3
4
5
6
7
8
9
<!-- 这里配置的阿里云的镜像仓库来替代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一下:

复制代码
1
2
3
4
5
6
7
8
9
...
//优先加载本地仓库
[DEBUG] Using local repository at D:programsapache-maven-3.6.1-binnotify_repo
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for D:programsapache-maven-3.6.1-binnotify_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).
 
...
复制代码
1
2
3
4
5
6
7
8
9
...
 
//可以看到这是远程仓库加载的顺序,先是加载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级别日志:

posted @   脾气很好的小毛驴  阅读(289)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示