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

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

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

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

相关推荐

使用Assembly打包和部署Spring Boot工程

SpringBoot项目的2种部署方式目前来说,SpringBoot项目有如下2种常见的部署方式一种是使用docker容器去部署。将SpringBoot的应用构建成一个docke...

java高级用法之:调用本地方法的利器JNA

简介JAVA是可以调用本地方法的,官方提供的调用方式叫做JNI,全称叫做javanativeinterface。要想使用JNI,我们需要在JAVA代码中定义native方法,然后通过javah命令...

Linux中如何通过Shell脚本来控制Spring Boot的Jar包启停服务?

SpringBoot项目在为开发者带来方便的同时,也带来了一个新的问题就是Jar包如何启动?在一般情况下我们都是采用了最为经典的java-jar命令来进行启动。然后通过ps命令找到对应的应用线程通...

牛逼!自己手写一个热加载(人民币手写符号一个横还是两个横)

热加载:在不停止程序运行的情况下,对类(对象)的动态替换JavaClassLoader简述Java中的类从被加载到内存中到卸载出内存为止,一共经历了七个阶段:加载、验证、准备、解析、初始化、使用、...

java 错误: 找不到或无法加载主类?看看怎么解决吧!

问题扫述:项目名称调整,由原来的com.mp.qms.report.biz调整为com.mp.busicen.mec.qms.report.biz后。项目在IDEA直接运行,但打包部署到服务器...

如何将 Spring Boot 工程打包成独立的可执行 JAR 包

导语:通过将SpringBoot项目打包成独立的可执行JAR包,可以方便地在任何支持Java环境的机器上运行项目。本文将详细介绍如何通过Maven构建插件将SpringBoot...

class 增量发包改造为 jar 包方式发布

大纲class增量发包介绍项目目录结构介绍jar包方式发布落地方案class增量发包介绍当前项目的迭代修复都是通过class增量包来发版本的将改动的代码class增量打包,如下图cla...

Jar启动和IDE里启动Sprintboot的区别

想聊明白这个问题,需要补充一些前提条件,比如Fatjar、类加载机制等1、Fatjar我们在开发业务程序的时候,经常需要引用第三方的jar包,最终程序开发完成之后,通过打包程序,会把自己的代码和三...

Java 20年,以后将往哪儿走?(java还能流行多久)

在今年的Java20周年的庆祝大会中,JavaOne2015的中心议题是“Java的20年”。甲骨文公司Java平台软件开发部的副总裁GeorgesSaab的主题演讲就将关注点放在了java...

Spring Boot Jar 包秒变 Docker 镜像实现多环境部署

你是否在互联网大厂后端开发工作中,遇到过这样的困扰?当完成一个SpringBoot项目开发,准备将Jar包部署到不同环境时,却发现各个环境依赖不同、配置复杂,部署过程繁琐又容易出错,不仅耗费...

从0开始,让你的Spring Boot项目跑在Linux服务器

1搭建Linux服务器1.1购买阿里云服务器或安装虚拟机这里建议是CentOS7.X或CentOS8.X,当然其他的Linux如deepin、Ubuntu也可以,只是软件环境的安装包和安装方式...

【技术】Maven 上传第三方jar包到私服

通过nexus后台上传私服以NexusRepositoryManagerOSS2.14.5-02为例。登录nexus后台。定义Maven坐标Maven坐标有两种方式:1.自定义参数;2....

JVM参数、main方法的args参数使用

一、前言我们知道JVM参数分为自定义参数、JVM系统参数,Javamain方法的参数。今天就谈谈怎么使用吧。二、查看jvm参数定义自定义参数我们打开cmd窗口,输入java,就能看到自定义参数的格式...

Maven项目如何发布jar包到Nexus私服

Maven项目发布jar包到Nexus私服在编码过程中,有些通用的代码模块,有时候我们不想通过复制粘贴来粗暴地复用。因为这样不仅体现不了变化,也不利于统一管理。这里我们使用mavendeploy的方...

干货丨Hadoop安装步骤!详解各目录内容及作用

Hadoop是Apache基金会面向全球开源的产品之一,任何用户都可以从ApacheHadoop官网下载使用。今天,播妞将以编写时较为稳定的Hadoop2.7.4版本为例,详细讲解Hadoop的安...