Maven-Maven多仓库配置-《Java笔记》

admin 2025-10-19 01:09:50 编程 来源:ZONE.CI 全球网 0 阅读模式

Maven在项目中使用Maven管理jar包依赖,往往会出现以下状况:1、国内访问maven默认远程中央镜像特别慢;2、使用阿里的镜像替代远程中央镜像;3、阿里云镜像中缺少部分jar包;4、同时使用私有仓库和公有仓库;针对以上情况,就需要让Maven支持多仓库配置。

单独仓库配置

当只配置一个仓库时,操作比较简单,直接在Maven的settings.xml文件中的<mirrors></mirrors>标签中进行全局配置即可,以阿里云的镜像为例:

  1. <mirrors>
  2. <mirror>
  3. <id>alimaven</id>
  4. <name>aliyun maven</name>
  5. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  6. <mirrorOf>central</mirrorOf>
  7. </mirror>
  8. </mirrors>

只用新增一个mirror配置即可。要做到单一仓库,设置mirrorOf到。mirrorOf中配置的星号,表示匹配所有的artifacts,也就是everything使用这里的代理地址。上面的mirrorOf配置了具体的名字,指的是repository的名字。镜像配置说明:1、id: 镜像的唯一标识;2、name: 名称描述;3、url: 地址;4、mirrorOf: 指定镜像规则,什么情况下从镜像仓库拉取。其中, : 匹配所有,所有内容都从镜像拉取;external:: 除了本地缓存的所有从镜像仓库拉取;repo,repo1: repo或者repo1,这里的repo指的仓库ID;,!repo1: 除了repo1的所有仓库;

环境仓库配置激活

:::danger 针对多仓库的配置在<mirrors></mirrors>标签中多配置几个mirror并不会生效。 ::: 正确的操作是在profiles节点下配置多个profile,而且配置之后要激活。

  1. <profiles>
  2. <profile>
  3. <id>boundlessgeo</id>
  4. <repositories>
  5. <repository>
  6. <id>boundlessgeo</id>
  7. <url>https://repo.boundlessgeo.com/main/</url>
  8. <releases>
  9. <enabled>true</enabled>
  10. </releases>
  11. <snapshots>
  12. <enabled>true</enabled>
  13. <updatePolicy>always</updatePolicy>
  14. </snapshots>
  15. </repository>
  16. </repositories>
  17. </profile>
  18. <profile>
  19. <id>aliyun</id>
  20. <repositories>
  21. <repository>
  22. <id>aliyun</id>
  23. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  24. <releases>
  25. <enabled>true</enabled>
  26. </releases>
  27. <snapshots>
  28. <enabled>true</enabled>
  29. <updatePolicy>always</updatePolicy>
  30. </snapshots>
  31. </repository>
  32. </repositories>
  33. </profile>
  34. <profile>
  35. <id>maven-central</id>
  36. <repositories>
  37. <repository>
  38. <id>maven-central</id>
  39. <url>http://central.maven.org/maven2/</url>
  40. <releases>
  41. <enabled>true</enabled>
  42. </releases>
  43. <snapshots>
  44. <enabled>true</enabled>
  45. <updatePolicy>always</updatePolicy>
  46. </snapshots>
  47. </repository>
  48. </repositories>
  49. </profile>
  50. <profiles>

通过配置activeProfiles子节点激活:

  1. <activeProfiles>
  2. <activeProfile>boundlessgeo</activeProfile>
  3. <activeProfile>aliyun</activeProfile>
  4. <activeProfile>maven-central</activeProfile>
  5. </activeProfiles>

此时如果是在Idea中使用了本地的Maven配置,那么在项目的Maven管理中会看到类似如下图中的profile选项。image.png打包时,勾选所使用的profile即可。如果使用Maven命令打包执行命令格式如下:

  1. mvn -P aliyun ...

1.如果aliyun仓库的id设置为central,则会覆盖maven里默认的远程仓库。2.aliyun的仓库也可以不用配置,直接在mirrors标签内配置一个镜像仓库,mirrors镜像仓库mirrorOf的值设置为central,则也可以实现覆盖默认的仓库。

项目仓库依赖配置

在项目中添加多个仓库,是通过修改项目中的pom文件实现的。思路:在项目中pom文件的repositories节点(如果没有手动添加)下添加多个repository节点,每个repository节点是一个仓库。配置效果如下:

  1. <!-- 特殊maven仓库 -->
  2. <repositories>
  3. <repository>
  4. <id>central-repo1</id>
  5. <name>Maven Repository Switchboard</name>
  6. <url>http://repo1.maven.org/maven2/</url>
  7. <layout>default</layout>
  8. <releases>
  9. <enabled>true</enabled>
  10. </releases>
  11. </repository>
  12. </repositories>

这里的id就是mirrorOf要使用的ID。在实践的过程中发现单单配置该仓库配置并不会生效,需要同时在setting.xml中定义一个mirrorOf为central-repo1的仓库配置,与该配置的id对照。setting.xml中的对照配置如下:

  1. <mirror>
  2. <id>central</id>
  3. <name>Maven Repository Switchboard</name>
  4. <url>https://repo1.maven.org/maven2/</url>
  5. <mirrorOf>central-repo1</mirrorOf>
  6. </mirror>
以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  17