0
点赞
收藏
分享

微信扫一扫

Maven mirrorOf标签的理解


Maven必须要知道至少一个可用的远程仓库,中央仓库就是这样一个默认的远程仓库,Maven 默认有一个 super pom 文件。

maven super pom 文件位置,,{MAVEN_HOME}\lib 下的 maven-model-builder-3.0.4.jar 中的 org/apache/maven/model/pom-4.0.0.xml

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

这个时候我们就明白了,我们在 settings 文件配置一个 mirror 的 mirrorOf 为 central 的镜像就会替代中央仓库

常用的阿里云镜像仓库:​​https://developer.aliyun.com/mvn/guide​​

当远程仓库被镜像匹配到,在获取 jar 包时将从镜像仓库获取,而不是我们配置的 repository 仓库, repository 将失去作用

mirrorOf 标签:

mirrorOf 标签里面放置的是 repository 配置的 id,为了满足一些复杂的需求,Maven还支持更高级的镜像配置:

// 不在本地仓库的文件才从该镜像获取
external:*
// 远程仓库 repo 和 repo1 从该镜像获取
repo,repo1
// 所有远程仓库都从该镜像获取,除 repo1 远程仓库以外
*,!repo1
// 所用远程仓库都从该镜像获取
*

私服(私有仓库):

是一种特殊的远程Maven仓库,它是架设在局域网内的仓库服务,私服一般被配置为互联网远程仓库的镜像,供局域网内的Maven用户使用。当Maven需要下载构件的时候,先向私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,同时缓存在私服之上,然后为Maven下载请求提供下载服务,另外,对于自定义或第三方的jar可以从本地上传到私服,供局域网内其他maven用户使用。

优先级:
排除镜像代理*的情况,优先级如下

  • 本地仓库 > 私服 (settingxml.profile) > 远程仓库(pom.xml-repository)和 镜像(mirror)

镜像是一个特殊的配置,其实镜像等同与远程仓库,没有匹配远程仓库的镜像就毫无作用。

maven中setting.xml的mirrorOf不能配置为*,否则其他所有配置都没用了,代理了所有,都默认走这个url下载。即优先级最高,一般不这么干

所有可以配置的位置如下

  • pom.xml的repository
  • pom.xml的profile的repository
  • settings.xml的profile的repository
  • settings.xml的mirror

pom.xml中配置多个repository,会按照配置的先后顺序从repository1、repository2(repository1中找不到的情况下才会从repository2下载)下载依赖jar。

如果想使用其它仓库,则在settings.xml中配置如下。

<settings>
<mirrors>
<mirror>
<id>spring-plugin-mirror</id>
<mirrorOf>spring-plugin</mirrorOf>
<name>aliyunmaven</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
</mirror>
<mirror>
<id>central-mirror</id>
<mirrorOf>central</mirrorOf>
<name>aliyunmaven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>other-repositories</id>
<repositories>
<repository>
<id>spring-plugin</id>
<name>spring-plugin</name>
<url>http://repo.spring.io/plugins-release/</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>other-repositories</activeProfile>
</activeProfiles>
</settings>

举报

相关推荐

0 条评论