理解maven私服和镜像配置这篇就够了
lipiwang 2024-10-15 18:37 12 浏览 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(默认中央仓库)
相关推荐
- Go语言图书管理RESTful API开发实战
-
Go(Golang)是最近流行起来,且相对较新的编程语言。它小而稳定,使用和学习简单,速度快,经过编译(原生代码),并大量用于云工具和服务(Docker、Kubernetes...)。考虑到它所带来的...
- 轻松搞定Golang 中的内存管理(golang设置内存大小)
-
除非您正在对服务进行原型设计,否则您可能会关心应用程序的内存使用情况。内存占用更小,基础设施成本降低,扩展变得更容易/延迟。尽管Go以不消耗大量内存而闻名,但仍有一些方法可以进一步减少消耗。其中一...
- golang实现deepseek 聊天功能(golang deepcopy)
-
在搭建完deepseek环境后在docker内部署deepseekrag环境,我们可以用golang实现聊天功能。在实现这个功能之前,我们先了解下提示词工程(prompt)。大模型虽然知道的东西多...
- golang slice的扩容机制(golang设置内存大小)
-
在Go语言中,切片(slice)是一种动态数组,其长度可以在运行时改变。当向切片中添加元素时,如果切片的容量不足以容纳新元素,就会触发扩容机制。下面详细介绍Go语言切片的扩容机制。扩容触发条件...
- Etcd服务注册与发现封装实现--golang
-
服务注册register.gopackageregisterimport("fmt""time"etcd3"github.com/cor...
- 嘿,轻松获取区间内所有日期的Golang小技巧!
-
在Go语言中,获取两个日期之间的所有日期可以手动实现一个函数来完成。以下是一个示例函数,它会返回一个日期切片,包含从开始日期到结束日期(包括这两个日期)的所有日期:packagemainimpo...
- 仓颉、Java、Golang性能测试——数组扩容
-
版本信息仓颉版本0.53.18Golang版本1.22.8Java版本corretto-1.8.0_452源码仓颉packagecangjie_testimportstd.collect...
- Golang 58个坑 – 中级篇:36-51(golang cef)
-
36.关闭HTTP的响应体37.关闭HTTP连接38.将JSON中的数字解码为interface类型39.struct、array、slice和map的值比较40.从panic...
- 一篇文章学会golang语法,golang简明教程快速入门
-
Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。——Go-wikipedia.org1Go安装最新版本下载地址官方下载https...
- 运维大神如何使用 Golang 日志监控应用程序
-
你是如何使用Golang日志监控你的应用程序的呢?Golang没有异常,只有错误。因此你的第一印象可能就是开发Golang日志策略并不是一件简单的事情。不支持异常事实上并不是什么问题,异常在...
- Golang操作elasticsearch(golang操作word)
-
简介开源的Elasticsearch是目前全文搜索引擎的首选,很多日志都是放到elasticsearch里面,然后再根据具体的需求进行分析。目前我们的运维系统是使用golang开发的,需要定时到e...
- 一文带你看懂Golang最新特性(golang x)
-
作者:腾讯PCG代码委员会经过十余年的迭代,Go语言逐渐成为云计算时代主流的编程语言。下到云计算基础设施,上到微服务,越来越多的流行产品使用Go语言编写。可见其影响力已经非常强大。一、Go语言发展历史...
- Golang 最常用函数(备用查询)(golang函数和方法)
-
hello.gopackagemainimport"fmt"funcmain(){fmt.Println("Hello,world!")}直...
- Golang:将日志以Json格式输出到Kafka
-
在上一篇文章中我实现了一个支持Debug、Info、Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手。有兴趣的可以通过这个链接前往:https://github.com/...
- 如何从 PHP 过渡到 Golang?(php转go需要多久)
-
我是PHP开发者,转Go两个月了吧,记录一下使用Golang怎么一步步开发新项目。本着有坑填坑,有错改错的宗旨,从零开始,开始学习。因为我司没有专门的Golang大牛,所以我也只能一步步自己去...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- maven镜像 (69)
- undefined reference to (60)
- zip格式 (63)
- oracle over (62)
- date_format函数用法 (67)
- 在线代理服务器 (60)
- shell 字符串比较 (74)
- x509证书 (61)
- localhost (65)
- java.awt.headless (66)
- syn_sent (64)
- settings.xml (59)
- 弹出窗口 (56)
- applicationcontextaware (72)
- my.cnf (73)
- httpsession (62)
- pkcs7 (62)
- session cookie (63)
- java 生成uuid (58)
- could not initialize class (58)
- beanpropertyrowmapper (58)
- word空格下划线不显示 (73)
- jar文件 (60)
- jsp内置对象 (58)
- makefile编写规则 (58)