![Maven配置多个仓库共同使用的方法](http://img.jiangsulong.com/220503/130AQ936-0.jpg)
文章插图
Maven是JAVA的项目配置管理工具,用来管理依赖,具体的用途就不展开说了 。大部分项目,配置一个镜像仓库地址就可以了(单个mirror) 。但是有的网上下载的项目需要从多个仓库查找对应的包,从网上找了很多帖子,配置Maven的settings.xml文件中的多个仓库,都是从一个地方复制粘贴的,方法说的都不对 。今天自己研究了一下,分享给大家 。
最终的settings.xml文件配置如果你不想看具体的配置,可以直接把我下面这个配置拿走,修改一下localRepository部分对应自己的本地repo(通常为~/.m2/repository),然后将settings.xml替换${MAVEN_HOME}/conf/settings.xml即可 。
<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.Apache.org/SETTINGS/1.2.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd"><localRepository>/Users/chris/.m2/repository</localRepository><pluginGroups></pluginGroups><proxies></proxies><servers></servers><mirrors></mirrors><profiles><profile><id>aliyun</id><repositories><repository><id>aliyun</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></profile><profile><id>pentaho</id><repositories><repository><id>pentaho</id><url>https://nexus.pentaho.org/content/repositories/omni/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></profile><profile><id>repo1</id><repositories><repository><id>repo1</id><url>https://repo1.maven.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></profile><profile><id>repo2</id><repositories><repository><id>repo2</id><url>https://repo2.maven.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></profile></profiles><activeProfiles><activeProfile>aliyun</activeProfile><activeProfile>pentaho</activeProfile><activeProfile>repo1</activeProfile><activeProfile>repo2</activeProfile></activeProfiles></settings>
settings.xml相关具体配置介绍Mirrors标签部分通常用来定义镜像仓库(repository),指定用来下载对应依赖和插件的仓库地址 。使用Mirrors的理由如下:- 配置一个就近加速的网络仓库;
- 配置一个你本地创建的镜像仓库;
<settings>...<mirrors><mirror><id>other-mirror</id><name>Other Mirror Repository</name><url>https://other-mirror.repo.other-company.com/maven2</url><mirrorOf>central</mirrorOf></mirror></mirrors>...</settings>
在Mirrors部分可以配置多个镜像仓库,但是在该部分配置多个仓库,并不能提供自动查询多个仓库的功能,默认还是取第一个仓库进行查询 。Profiles标签部分settings.xml中的profile部分,包括4个二级子标签:activation, repositories, pluginRepositories and properties 。profile部分的内容需要在下面进行激活 。这里介绍多个镜像共同使用仅介绍Repositories部分 。
Repositories配置方式如下,指定repo地址、id:
<profiles><profile><id>repo2</id><repositories><repository><id>repo2</id><url>https://repo2.maven.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></profile></profiles>
Active Profiles标签部分settings.xml配置文件最后的一部分就是激活公用多个仓库的重点activeProfiles 。包含多个activeProfile元素,每一个activeProfile元素都用来指定一个上部分profile的id,也就是说,每指定一个activeProfile映射,就激活一个profile,将会覆盖其他任何环境配置 。
推荐阅读
- 买电脑,应该关心的6个配置!哪种电脑不能买?
- linux-centos网络配置bond
- Centos6下网卡启动、配置ifcfg-eth0教程
- CISA 美国网络安全和基础设施安全局 和CrowdStrike发布多个 Log4j 扫描器但没有一个能检测到所有的格式
- 配置与 Hyper-V 虚拟机的远程桌面连接
- 网易游戏有哪些?
- Win下部署多个MySQL数据库实例
- Nginx可视化配置神器
- 华为,H3C和锐捷:一起学习如何配置VRRP
- 如何在Rescue模式下配置网络和SSH登录