Maven配置多仓库无效的解决
作者:程序新视界 时间:2023-11-29 04:37:10
在项目中使用Maven管理jar包依赖,往往会出现以下状况:
1、国内访问maven默认远程中央镜像特别慢;
2、使用阿里的镜像替代远程中央镜像;
3、阿里云镜像中缺少部分jar包;
4、同时使用私有仓库和公有仓库;
针对以上情况,我们就需要让Maven支持多仓库配置。
单独仓库配置
当只配置一个仓库时,操作比较简单,直接在Maven的settings.xml文件中进行全局配置即可,以阿里云的镜像为例:
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</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的所有仓库;
多仓库配置
那么针对多仓库的配置是否再多配置几个mirror就可以了?如此配置并不会生效。
正确的操作是在profiles节点下配置多个profile,而且配置之后要激活。
<profiles>
<profile>
<id>boundlessgeo</id>
<repositories>
<repository>
<id>boundlessgeo</id>
<url>https://repo.boundlessgeo.com/main/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>maven-central</id>
<repositories>
<repository>
<id>maven-central</id>
<url>http://central.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
<profiles>
通过配置activeProfiles子节点激活:
<activeProfiles>
<activeProfile>boundlessgeo</activeProfile>
<activeProfile>aliyun</activeProfile>
<activeProfile>maven-central</activeProfile>
</activeProfiles>
此时如果是在Idea中使用了本地的Maven配置,那么在项目的Maven管理中会看到类似如下图中的profile选项。
打包时,勾选所使用的profile即可。如果使用Maven命令打包执行命令格式如下:
mvn -Paliyun ...
1.如果aliyun仓库的id设置为central,则会覆盖maven里默认的远程仓库。
2.aliyun的仓库也可以不用配置,直接在mirrors标签内配置一个镜像仓库,mirrors镜像仓库mirrorOf的值设置为central,则也可以实现覆盖默认的仓库。
项目中配置镜像
在项目中添加多个仓库,是通过修改项目中的pom文件实现的。
思路:在项目中pom文件的repositories节点(如果没有手动添加)下添加多个repository节点,每个repository节点是一个仓库。
配置效果如下:
<!-- 特殊maven仓库 -->
<repositories>
<repository>
<id>central-repo1</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
这里的id就是mirrorOf要使用的ID。
在实践的过程中发现单单配置该仓库配置并不会生效,需要同时在setting.xml中定义一个mirrorOf为central-repo1的仓库配置,与该配置的id对照。
setting.xml中的对照配置如下:
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>https://repo1.maven.org/maven2/</url>
<mirrorOf>central-repo1</mirrorOf>
</mirror>
值得收藏的国内镜像
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/mvn/view</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://maven.aliyun.com/mvn/view</url>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
来源:https://juejin.cn/post/6963471281962352671
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10系统下配置java环境变量的全过程
![](https://img.aspxhome.com/file/2023/4/90034_0s.png)
winform导出dataviewgrid数据为excel的方法
C#实现递归调用的Lambda表达式
Java语言求解完美数代码分析
![](https://img.aspxhome.com/file/2023/1/64791_0s.png)
Java方法的覆盖与隐藏的区别分析
详解Android中Notification的使用方法
![](https://img.aspxhome.com/file/2023/7/138537_0s.png)
Java SSM配置文件案例详解
在Java中int和byte[]的相互转换
java io文件操作删除文件或文件夹的7种方法
![](https://img.aspxhome.com/file/2023/8/99848_0s.jpg)
Android 仿今日头条评论时键盘自动弹出的效果(推荐)
![](https://img.aspxhome.com/file/2023/7/139547_0s.png)
Android自定义View实现波浪动画
![](https://img.aspxhome.com/file/2023/8/137248_0s.gif)
Android网格视图GridView的使用
![](https://img.aspxhome.com/file/2023/5/138935_0s.png)
Android动画之小球拟合动画实例
![](https://img.aspxhome.com/file/2023/0/138090_0s.gif)
Springboot集成Elasticsearch的步骤与相关功能
![](https://img.aspxhome.com/file/2023/8/70488_0s.png)
详解Spring Boot的GenericApplicationContext使用教程
SpringBoot核心@SpringBootApplication使用介绍
![](https://img.aspxhome.com/file/2023/2/59202_0s.png)
一文搞懂Java顶层类之Object类的使用
![](https://img.aspxhome.com/file/2023/0/115970_0s.png)
Java Apache Shiro安全框架快速开发详解流程
![](https://img.aspxhome.com/file/2023/8/69648_0s.png)
详解springboot集成mybatis xml方式
![](https://img.aspxhome.com/file/2023/8/64478_0s.png)