百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分析 > 正文

理解maven私服和镜像配置这篇就够了

liebian365 2025-01-12 16:18 18 浏览 0 评论

网上一大堆,关于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(默认中央仓库)

相关推荐

4万多吨豪华游轮遇险 竟是因为这个原因……

(观察者网讯)4.7万吨豪华游轮搁浅,竟是因为油量太低?据观察者网此前报道,挪威游轮“维京天空”号上周六(23日)在挪威近海发生引擎故障搁浅。船上载有1300多人,其中28人受伤住院。经过数天的调...

“菜鸟黑客”必用兵器之“渗透测试篇二”

"菜鸟黑客"必用兵器之"渗透测试篇二"上篇文章主要针对伙伴们对"渗透测试"应该如何学习?"渗透测试"的基本流程?本篇文章继续上次的分享,接着介绍一下黑客们常用的渗透测试工具有哪些?以及用实验环境让大家...

科幻春晚丨《震动羽翼说“Hello”》两万年星间飞行,探测器对地球的最终告白

作者|藤井太洋译者|祝力新【编者按】2021年科幻春晚的最后一篇小说,来自大家喜爱的日本科幻作家藤井太洋。小说将视角放在一颗太空探测器上,延续了他一贯的浪漫风格。...

麦子陪你做作业(二):KEGG通路数据库的正确打开姿势

作者:麦子KEGG是通路数据库中最庞大的,涵盖基因组网络信息,主要注释基因的功能和调控关系。当我们选到了合适的候选分子,单变量研究也已做完,接着研究机制的时便可使用到它。你需要了解你的分子目前已有哪些...

知存科技王绍迪:突破存储墙瓶颈,详解存算一体架构优势

智东西(公众号:zhidxcom)编辑|韦世玮智东西6月5日消息,近日,在落幕不久的GTIC2021嵌入式AI创新峰会上,知存科技CEO王绍迪博士以《存算一体AI芯片:AIoT设备的算力新选择》...

每日新闻播报(September 14)_每日新闻播报英文

AnOscarstatuestandscoveredwithplasticduringpreparationsleadinguptothe87thAcademyAward...

香港新巴城巴开放实时到站数据 供科技界研发使用

中新网3月22日电据香港《明报》报道,香港特区政府致力推动智慧城市,鼓励公私营机构开放数据,以便科技界研发使用。香港运输署21日与新巴及城巴(两巴)公司签署谅解备忘录,两巴将于2019年第3季度,开...

5款不容错过的APP: Red Bull Alert,Flipagram,WifiMapper

本周有不少非常出色的app推出,鸵鸟电台做了一个小合集。亮相本周榜单的有WifiMapper's安卓版的app,其中包含了RedBull的一款新型闹钟,还有一款可爱的怪物主题益智游戏。一起来看看我...

Qt动画效果展示_qt显示图片

今天在这篇博文中,主要实践Qt动画,做一个实例来讲解Qt动画使用,其界面如下图所示(由于没有录制为gif动画图片,所以请各位下载查看效果):该程序使用应用程序单窗口,主窗口继承于QMainWindow...

如何从0到1设计实现一门自己的脚本语言

作者:dong...

三年级语文上册 仿写句子 需要的直接下载打印吧

描写秋天的好句好段1.秋天来了,山野变成了美丽的图画。苹果露出红红的脸庞,梨树挂起金黄的灯笼,高粱举起了燃烧的火把。大雁在天空一会儿写“人”字,一会儿写“一”字。2.花园里,菊花争奇斗艳,红的似火,粉...

C++|那些一看就很简洁、优雅、经典的小代码段

目录0等概率随机洗牌:1大小写转换2字符串复制...

二年级上册语文必考句子仿写,家长打印,孩子照着练

二年级上册语文必考句子仿写,家长打印,孩子照着练。具体如下:...

一年级语文上 句子专项练习(可打印)

...

亲自上阵!C++ 大佬深度“剧透”:C++26 将如何在代码生成上对抗 Rust?

...

取消回复欢迎 发表评论: