
前言:
作者每次拉到一个非常nice的项目,下载依赖时总会遇到依赖下载失败的情况。故此学习,并记录!
com.github.promeg:tinypinyin:jar:2.0.3 was not found in https://repo1.maven.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus has elapsed or updates are forced每次看到下载失败,作者肥肠难受,决定好好研究这个“https://repo1.maven.org/maven2”这个家伙,为什么每次失败,都是从这个地址下载不到。
Maven 在下载依赖前,首先检查本地仓库(默认位于~/.m2/repository或者用户在settings.xml文件中自定义的本地仓库地址)是否已经存在所需依赖。如果存在,则直接使用,不会联网下载。
如果本地没有,Maven 会根据配置的远程仓库地址去下载依赖。这些配置的优先级如下:
pom.xml 和激活的 settings.xml profiles。 <mirrors> 规则,将这些逻辑仓库的请求重定向到镜像地址。关键点:
<mirrors>不是“添加”仓库,而是“替换”已有仓库的地址。
Maven 会合并以下来源的仓库定义:
项目
pom.xml中的<repositories>
settings.xml中 激活的 profile 里定义的<repositories>
这些仓库都有一个 <id>(如 central、my-repo 等),Maven 会把它们当作“逻辑仓库”。
<mirrors> 规则(来自 settings.xml)Maven 会检查 settings.xml 中的 <mirrors> 配置,看是否有 mirror 的 <mirrorOf> 匹配某个逻辑仓库的 ID。
如果匹配,则用 mirror 的 URL 替代原仓库的 URL。
如果
<mirrorOf>*</mirrorOf>,则所有仓库请求都被重定向到该镜像(包括central、my-repo等)。重点:
<mirrors>的优先级最高:它发生在仓库地址实际使用之前。
pom.xml中定义了:<repositories>
<repository>
<id>my-company-repo</id>
<url>https://repo.mycompany.com/maven2</url>
</repository>
</repositories>settings.xml中配置了: <mirror>
<id>nexus-aliyun</id>
<name>nexus aliyun</name>
<url>https://maven.aliyun.com/repository/public/</url>
<mirrorOf>aliyunmaven</mirrorOf>
</mirror>
my-company-repo(ID 不是 central)→ 不会被阿里云镜像覆盖,仍从
https://repo.mycompany.com/maven2下载。如果依赖在
central仓库中 → 被重定向到阿里云。
settings.xml 中配置的是:<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus.internal/repository/maven-public/</url>
</mirror>central 和 my-company-repo)都会被重定向到 http://nexus.internal/...pom.xml 写了别的 URL,也会被忽略!配置来源 | 是否决定下载地址? | 说明 |
|---|---|---|
pom.xml <repositories> | 不直接决定 | 提供逻辑仓库 ID 和原始 URL |
settings.xml <profiles> + <repositories> | 不直接决定 | 同上,合并到逻辑仓库列表 |
settings.xml <mirrors> | 最终决定 | 根据 <mirrorOf> 匹配逻辑仓库 ID,替换实际下载地址 |
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。