目录
前言
我们在使用Maven的时候都会去更改镜像仓库,我在网上看到的更多是配置在<mirrors>下的,还有一种是配置在<profiles>下。好奇两种配置有什么区别,下面让我们深入探讨探讨。
第一种
我们先看下网上配置最多的方式,接下来我用阿里的库进行演示,我将在<mirrors>标签里添加如下配置:
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
接下来在pom写一个不存在的依赖坐标:
用命令下载看下:
通过上面的图片可以看到它去阿里仓库下载,但是没找到就抛异常了!接下来我们看下第二种。
第二种
我将上面添加在<mirrors>里的配置删掉,然后在<profiles>下添加如下配置:
<!-- 配置阿里的远程库 -->
<profile>
<id>aliyunRepository</id>
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>https://maven.aliyun.com/repository/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
需要在配置文件的末尾添加如下配置,以便激活使用以上的仓库:
<activeProfiles>
<!-- 手动激活 -->
<activeProfile>aliyunRepository</activeProfile>
</activeProfiles>
跟上面一样在cmd窗口用命令下载:
通过以上的图片可以明显的看到,它先是去阿里的仓库寻找,没找到后就去中央仓库寻找,还是没找到便抛出了异常!这下区别就很明显了!
总结
通过以上的对比,如果你想做到既能让自己配置的远程库生效又想让中央仓库生效的话可以考虑第二种配置!大家也知道<mirrors>是可以配置多个的,这个时候大家可能会认为第一个仓库找不到的时候便会去第二个仓库找,但并非如此,只有当第一个仓库连接不上了才会去第二个仓库!
🥇原创不易,还希望各位大佬支持一下!
👍点赞,你的认可是我创作的动力 !
🌟收藏,你的青睐是我努力的方向!
✏️评论,你的意见是我进步的财富!