理解maven私服和镜像配置这篇就够了
liuian 2025-07-09 14:15 50 浏览
网上一大堆,关于maven配置本地私服和镜像的流程,神乎其乎,初学者总是很难理解其中的真正原理,
我通过这篇文章来终结他们;
maven安装文件setting.xml的结构
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>interactiveMode: 是否与用户开启交互模式,默认为 true
offline: 离线模式,默认为 false
localRepository: 配置本地存储库的位置,默认为${user.home}/.m2/repository
proxies: 代理配置
profiles: 配置环境
activeProfiles: 配置默认激活的环境
pluginGroups: 比如<pluginGroup>org.eclipse.jetty</pluginGroup>, 默认有org.apache.maven.plugins and org.codehaus.mojo。
servers: 配置私服的用户名和密码
mirrors: mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
配置镜像
因为中央仓库在国外,下载比较慢,所以可以配置为定向到阿里云镜像,阿里云镜像里面一般都很全
<mirror>
<id>Nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>这样访问中央仓库时就会定位到阿里云,不再访问中央仓库。如果阿里云仓库没有呢?怎么办?这种情况一般不会出现,因为阿里云仓库的jar很全
如果你希望如果在阿里云镜像找不到资源时也可以访问问中央仓库,那么阿里云镜像就不能使用<mirrorOf>central</mirrorOf>,可以把阿里云镜像配置成一个私服,
如<mirrorOf>aliyun</mirrorOf>
关于mirrorOf,如果有同名的mirrorOf,那么只会匹配第一个,
如果有<mirrorOf>*</mirrorOf>,优先匹配名字完全相同的,其次匹配通配符
配置profile
<profile>
<id>nexus-mr</id>
<repositories>
<!-- 配置的顺序决定了下载 jar 包的顺序 -->
<!-- maven下载时,按顺序查找,优先找nexus,找不到再找central -->
<!-- 这里nexus为我的私服地址 -->
<repository>
<id>nexus</id>
<url>http://172.16.8.6:8082/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- 中央仓库此处可省略 -->
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://172.16.8.6:8082/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>配置activeProfiles
用于激活前面的环境配置
<activeProfiles>
<activeProfile>nexus-mr</activeProfile>
</activeProfiles>完整的setting.xml配置如下:
<?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">
<!-- 本地存放jar的位置 -->
<localRepository>D:\m2\repository</localRepository>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!-- 此处为我自己的私服账号配置 -->
<server>
<id>maven_3rd_party</id>
<username>maven</username>
<password>maven</password>
</server>
<server>
<id>maven-releases</id>
<username>maven</username>
<password>maven</password>
</server>
<server>
<id>maven-snapshots</id>
<username>maven</username>
<password>maven</password>
</server>
</servers>
<mirrors>
<!-- 此处的mirrorOf不能随便写,要和下面profile中的repository name标签的一致 ;如果配置的是central,后面profile中可以省略配置对应的central -->
<!--
maven获取真正起作用的repository集合流程:首先会获取pom.xml里的repository集合,
然后在settings.xml里找mirrors元素,
如果repository的id和mirror的mirrorOf的值相同,则该mirror替代该repository,
如果该repository找不到对应的mirror,
则使用其本身,依此可以得到最终起作用的repository集合
-->
<!-- 将 central 的请求重定向到阿里云的公共 Maven 仓库 -->
<!-- 其它的不重定向到阿里云 -->
<mirror>
<id>Nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus-mr</id>
<!--
这里的 repositories 如果不配置的话,默认会有一个 Maven 中央仓库的配置,
同样 pluginRepositories 中如果没有配置的话,默认也是有一个 Maven 中央仓库的配置。
还有!如果 repositories 中没有配置 repository.id 是 central 的 repository,
会自动增加一个 Maven 中央仓库的配置,并且是以追加的方式,
也就是配置在 repositories 的最后一个。
所以如果只配置了私服的 repository 情况下,就会先去私服中下载,
私服中下载不到时再去追加上来的 Maven 中央仓库中下载
-->
<repositories>
<!-- 配置的顺序决定了下载 jar 包的顺序 -->
<repository>
<id>nexus</id>
<url>http://172.16.8.6:8082/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!--
如果前面mirrorOf配置了central定位到阿里云镜像,
这里的central配置可以省略,nexus中找不到会自动根据central去阿里云镜像查找;
若上面mirrorOf没有配置central ,这里不配central的话,nexus中找不到会自动去maven默认中央仓库找
-->
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://172.16.8.6:8082/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<!-- 激活环境配置 ,使上面的配置生效 -->
<activeProfile>nexus-mr</activeProfile>
</activeProfiles>
</settings>注意:此文中虽然mirrorOf和profile中central都同时配置了,也可以无需都配置,二者有一个配置了就可以了
如果你想把私服nexus也配置成mirrorOf也可以,但mirrorOf不可配置为*,否则全部请求都走nexus了,不会走central
测试,我们测试一下是否走了阿里云镜像
项目中增加一个新的pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>更新maven,到本地下载的
m2e-lastUpdated.properties文件中查看下载信息,
内容为
nexus|http\://172.16.8.6\:8082/repository/maven-public/|sources=1628230775917
Nexus-aliyun|https\://maven.aliyun.com/repository/central|sources=1628230775917
证明maven访问仓库顺序为:172.16.8.6(nexus)、 maven.aliyun.com(阿里云镜像)
我们将上面的mirrorOf和profile中的central对应的配置都注释,删除本地jar,再次更新项目maven,内容变成:
nexus|http\://172.16.8.6\:8082/repository/maven-public/|javadoc=1628217742198
nexus|http\://172.16.8.6\:8082/repository/maven-public/|sources=1628217716685
central|https\://repo.maven.apache.org/maven2|javadoc=1628217742198
central|https\://repo.maven.apache.org/maven2|sources=1628217716685
证明maven访问仓库顺序为:172.16.8.6(nexus)、repo.maven.apache.org(默认中央仓库)
相关推荐
- 苹果笔记本怎样重装系统(苹果笔记本怎样重装系统还原)
-
苹果笔记本电脑系统可以通过以下步骤进行重装:1.备份数据:在开始重装前,需要备份你的重要数据。你可以将数据存储到外部硬盘、云存储或其他可靠的设备中。2.下载安装器:从AppStore中下载macOS...
- 手机wifi打不开怎么办
-
手机wifi打不开的原因,可能集中在该手机出现了手机文件丢失、手机版本不稳定、手机文件出错以及手机wifi模块摔坏等故障造成的。手机wifi打不开修复教程1.wcnss_qcom_cfg文件丢失导...
- bios恢复出厂设置后无法开机
-
可通过进入BIOS界面设置bios恢复出厂设置的方法解决,步骤如下:1、通过按Delete或数字键盘中的Del键进入BIOS。2、按箭头键输入并将光标移动到“加载设置默认值”项,然后按enter确认。...
- 电脑硬盘打不开怎么办(电脑硬盘打不开怎么办)
-
电脑硬盘坏了是不能开机的。硬盘坏道的修复方法:1、逻辑坏道的修复对于逻辑坏道,Windows自带的“磁盘扫描程序(Scandisk)”就是最简便常用的解决手段。如果硬盘出现了坏道,我们可在Window...
- linux系统备份与还原工具(linux系统备份与还原工具在哪)
-
用GHOST对LINUX系统做备份1:要求将安装了LINUX系统的硬盘(原盘)整盘刻至另一硬盘(目标盘)。2:所需工具:DOS系统引导盘,GHOST2003(版本低的对文件格式不能很好的支持),原盘(...
- pdf怎么转换成xml格式(如何将pdf格式转换成xml格式)
-
将PDF转换为XML需要使用专业的PDF转换工具。以下是一些常用的PDF转XML工具:1.AdobeAcrobatDC:AdobeAcrobatDC是一款功能强大的PDF编辑软件,其中包括P...
- windows7iso文件(iso文件 win7)
-
利用winrar可以直接打开iso文件,如果双击不能直接打开需要设置winrar,步骤如下:1、启动winrar,点击选项菜单设置命令;2、点击综合选项卡,点击全部选择,点击确定即可。具体操作方法步骤...
- 路由器ip地址是什么意思(路由器的ip地址是)
-
路由器IP地址是指连接到互联网的路由器在局域网内的唯一标识符,一般为192.168.1.1或192.168.0.1等地址。通过路由器IP地址,用户可以通过浏览器等工具登录到路由器管理界面,进行网络设置...
-
- mediaplayer播放记录在哪里(mediaplayer历史记录)
-
《WindowsMediaPlayer》无法播放该文件,表示《WindowsMediaPlayer》目前的版本不支持该视频的格式编码。解决方法: 1.如果安装的是正版操作系统,点帮助→检查更新,稍待片刻,WindowsMed...
-
2026-01-14 02:37 liuian
- 电脑xp怎么换系统win7(电脑xp系统换win7教程)
-
第一种方法:自助安装win7系统 我们在进行自助安装win7系统之前我们要保证我们的电脑是联网的。为了能更加顺利的完成对xp系统的升级,我们的电脑最好是能高速上网的,只有能联网我们才可以下载最新的系...
- appstore官方网站(appstore.apple.com)
-
Appstore即applicationstore,通常理解为应用商店。Appstore是苹果公司基于iPhone的软件应用商店,向iPhone的用户提供第三方的应用软件服务,这是苹果开创的一...
- 电脑开不了机怎么办显示英文字母
-
win7操作系统电脑在开机的时候屏幕界面出现CLIENTMACADDR,然后就一直停在了这个界面,要等很长时间才能进入系统登入界面。出现这样问题的原因是什么?这是因为网卡启用了BOOTROM芯片...
- win7此windows副本不是正版(win7 此windows副本不是正版)
-
win7系统提示副本不是正版解决方法:1.打开设备,调出运行窗口,输入命令“cmd”,并按下回车键;2.这时命令提示符窗口便会自动弹出;3.输入命令“SLMGR-REARM”,再按下回车键;4.命令...
- win7安装选版本(win7选哪个版本)
-
Win7旗舰版更好用。Windows7旗舰版属于微软公司开发的Windows7系统系列中的终结版本,是为了取代WindowsXP系统的新系统,Windows7的版本还有简易版、家庭普通版、家庭高...
-
- 电脑psd文件用什么打开(电脑上psd文件打不开)
-
具体操作步骤如下:1、首先鼠标右键单击PSD格式的图片,然后点击“打开方式”选项。2、然后在该页面中点击“选择默认程序”选项。3、之后在该页面中点击“浏览”选项。4、然后在该页面中点击选择要打开的软件后点击“确定”选项即可打开了。PSD文...
-
2026-01-14 01:05 liuian
- 一周热门
-
-
飞牛OS入门安装遇到问题,如何解决?
-
如何在 iPhone 和 Android 上恢复已删除的抖音消息
-
Boost高性能并发无锁队列指南:boost::lockfree::queue
-
大模型手册: 保姆级用CherryStudio知识库
-
用什么工具在Win中查看8G大的log文件?
-
如何在 Windows 10 或 11 上通过命令行安装 Node.js 和 NPM
-
威联通NAS安装阿里云盘WebDAV服务并添加到Infuse
-
Trae IDE 如何与 GitHub 无缝对接?
-
idea插件之maven search(工欲善其事,必先利其器)
-
如何修改图片拍摄日期?快速修改图片拍摄日期的6种方法
-
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)
