百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

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

liuian 2025-07-09 14:15 30 浏览

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

相关推荐

教你把多个视频合并成一个视频的方法

一.情况介绍当你有一个m3u8文件和一个目录,目录中有连续的视频片段,这些片段可以连成一段完整的视频。m3u8文件打开后像这样:m3u8文件,可以理解为播放列表,里面是播放视频片段的顺序。视频片段像这...

零代码编程:用kimichat合并一个文件夹下的多个文件

一个文件夹里面有很多个srt字幕文件,如何借助kimichat来自动批量合并呢?在kimichat对话框中输入提示词:你是一个Python编程专家,完成如下的编程任务:这个文件夹:D:\downloa...

Java APT_java APT 生成代码

JavaAPT(AnnotationProcessingTool)是一种在Java编译阶段处理注解的工具。APT会在编译阶段扫描源代码中的注解,并根据这些注解生成代码、资源文件或其他输出,...

Unit Runtime:一键运行 AI 生成的代码,或许将成为你的复制 + 粘贴神器

在我们构建了UnitMesh架构之后,以及对应的demo之后,便着手于实现UnitMesh架构。于是,我们就继续开始UnitRuntime,以用于直接运行AI生成的代码。PS:...

挣脱臃肿的枷锁:为什么说Vert.x是Java开发者手中的一柄利剑?

如果你是一名Java开发者,那么你的职业生涯几乎无法避开Spring。它如同一位德高望重的老国王,统治着企业级应用开发的大片疆土。SpringBoot的约定大于配置、SpringCloud的微服务...

五年后,谷歌还在全力以赴发展 Kotlin

作者|FredericLardinois译者|Sambodhi策划|Tina自2017年谷歌I/O全球开发者大会上,谷歌首次宣布将Kotlin(JetBrains开发的Ja...

kotlin和java开发哪个好,优缺点对比

Kotlin和Java都是常见的编程语言,它们有各自的优缺点。Kotlin的优点:简洁:Kotlin程序相对于Java程序更简洁,可以减少代码量。安全:Kotlin在类型系统和空值安全...

移动端架构模式全景解析:从MVC到MVVM,如何选择最佳设计方案?

掌握不同架构模式的精髓,是构建可维护、可测试且高效移动应用的关键。在移动应用开发中,选择合适的软件架构模式对项目的可维护性、可测试性和团队协作效率至关重要。随着应用复杂度的增加,一个良好的架构能够帮助...

颜值非常高的XShell替代工具Termora,不一样的使用体验!

Termora是一款面向开发者和运维人员的跨平台SSH终端与文件管理工具,支持Windows、macOS及Linux系统,通过一体化界面简化远程服务器管理流程。其核心定位是解决多平台环境下远程连接、文...

预处理的底层原理和预处理编译运行异常的解决方案

若文章对您有帮助,欢迎关注程序员小迷。助您在编程路上越走越好![Mac-10.7.1LionIntel-based]Q:预处理到底干了什么事情?A:预处理,顾名思义,预先做的处理。源代码中...

为“架构”再建个模:如何用代码描述软件架构?

在架构治理平台ArchGuard中,为了实现对架构的治理,我们需要代码+模型描述所要处理的内容和数据。所以,在ArchGuard中,我们有了代码的模型、依赖的模型、变更的模型等,剩下的两个...

深度解析:Google Gemma 3n —— 移动优先的轻量多模态大模型

2025年6月,Google正式发布了Gemma3n,这是一款能够在2GB内存环境下运行的轻量级多模态大模型。它延续了Gemma家族的开源基因,同时在架构设计上大幅优化,目标是让...

比分网开发技术栈与功能详解_比分网有哪些

一、核心功能模块一个基本的比分网通常包含以下模块:首页/总览实时比分看板:滚动展示所有正在进行的比赛,包含比分、比赛时间、红黄牌等关键信息。热门赛事/焦点战:突出显示重要的、关注度高的比赛。赛事导航...

设计模式之-生成器_一键生成设计

一、【概念定义】——“分步构建复杂对象,隐藏创建细节”生成器模式(BuilderPattern):一种“分步构建型”创建型设计模式,它将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建...

构建第一个 Kotlin Android 应用_kotlin简介

第一步:安装AndroidStudio(推荐IDE)AndroidStudio是官方推荐的Android开发集成开发环境(IDE),内置对Kotlin的完整支持。1.下载And...