前段时间碰到一个Maven
依赖加载问题
前段时间熬了个夜解决了
解决过程中算是终于把Maven
入门了
这个神奇的问题准备下次再讲~
这篇先讲讲Maven
中的依赖加载顺序
前言
加载顺序, 就是当一个依赖你本地找不到时, 会去哪些仓库找, 这些仓库的搜索顺序就是加载顺序.
本地找不到 和 本地不存在 是两个意思哦~~~
那么问题来了,可以在哪些地方配置仓库?
我总结了几个,如若有漏请留言告知,感激不尽!
-
pom_repo
在pom.xml
文件的repositories
中配置 -
pom-profile-repo
在pom.xml
文件的profiles
中配置 -
settings_profile_repo
在setting.xml
文件的profiles
中配置 -
mirror_repo
在setting.xml
文件的mirrors
中配置
下面建一个简单的项目maven
项目,并重置setting.xml
配置确保无其他仓库配置。
注意
setting.xml
配置文件有两个, 一个安装目录, 一个用户目录
1. 啥配置都不加
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 727 B/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ depe-repo ---
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 154 kB/s)
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\idea\depe-repo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ depe-repo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.225 s
[INFO] Finished at: 2019-11-05T11:33:10+08:00
[INFO] Final Memory: 12M/155M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:depe-repo$
- 可见默认仓库是中央仓库
central
, 地址是https://repo.maven.apache.org/maven2
, 这个地址是配置在Super Pom
中的
Super Pom
路径: 安装目录下${M2_HOME}/maven-model-builder-3.5.2.jar/org/apache/maven/model/pom-4.0.0.xml
2. 添加pom_repo
<repository>
<id>pom_repo</id>
<name>pom_repo_name</name>
<url>http://10.1.228.195:18081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
再编译一下
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/junit/junit/3.8.1/junit-3.8.1.pom
Downloaded from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 1.8 kB/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ depe-repo ---
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 73 kB/s)
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\idea\depe-repo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ depe-repo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.180 s
[INFO] Finished at: 2019-11-05T11:34:14+08:00
[INFO] Final Memory: 11M/142M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:depe-repo$
- 顺序为先
pom_repo
再central
这时候我们添加一个不存在的依赖, 再看看
为了方便查看结果,下面我们都使用一个不存在的依赖来测试
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.1/micrometer-registry-prometheus-3.8.1.pom
Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-registry-prometheus/3.8.1/micrometer-registry-prometheus-3.8.1.pom
[WARNING] The POM for io.micrometer:micrometer-registry-prometheus:jar:3.8.1 is missing, no dependency information available
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.1/micrometer-registry-prometheus-3.8.1.jar
Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-registry-prometheus/3.8.1/micrometer-registry-prometheus-3.8.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.984 s
[INFO] Finished at: 2019-11-05T14:06:42+08:00
[INFO] Final Memory: 9M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project depe-repo: Could not resolve dependencies for project com.maven.demo:depe-repo:jar:1.0-SNAPSHOT: Could not find artifact io.micrometer:micrometer-registry-prometheus:jar:3.8.1 in pom_repo (http://10.1.228.195:18081/nexus/content/groups/public) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
sunyelw@windows:depe-repo$
- 会先去
pom_repo
下载, 没找到再去central
下载, 都没找到就报错:pom_repo
不存在此依赖. -
Maven
找一个不存在的依赖, 会先下载其pom
文件, 再下载jar
包. (pom
不存在就是会报一个warning
, 然后尝试继续下载jar
文件)
优先级
pom_repo
>central
3. 添加 pom_profile_repo
<!-- pom-profile-repo -->
<profiles>
<profile>
<id>pom_profile</id>
<repositories>
<repository>
<id>pom_profile_repo</id>
<name>pom_profile_repo_name</name>
<url>http://mirrors.163.com/maven/repository/maven-public</url>
</repository>
</repositories>
<!-- 默认生效 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
再执行编译
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from pom_profile_repo: http://mirrors.163.com/maven/repository/maven-public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
[WARNING] The POM for io.micrometer:micrometer-registry-prometheus:jar:3.8.4 is missing, no dependency information available
Downloading from pom_profile_repo: http://mirrors.163.com/maven/repository/maven-public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.jar
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.jar
Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.769 s
[INFO] Finished at: 2019-11-05T15:15:51+08:00
[INFO] Final Memory: 9M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project depe-repo: Could not resolve dependencies for project com.maven.demo:depe-repo:jar:1.0-SNAPSHOT: Could not find artifact io.micrometer:micrometer-registry-prometheus:jar:3.8.4 in pom_profile_repo (http://mirrors.163.com/maven/repository/maven-public) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
sunyelw@windows:depe-repo$
- 如果没有默认或传入参数指定生效
profile
, 那么pom_profile
形同虚设 - 如果有多个默认生效的
profile
, 那么按在pom
中的顺序最后一个profile
生效 - 如果下过一次没下成功, 那么再次执行
mvn compile
读取到*.lastUpdated
文件(本地仓库jar
包路径中) 中的错误信息便不会再去下载直接抛出ERROR
. (这个时候需要手动删除*.lastUpdated
文件)
优先级
pom_profile_repo
>pom_repo
>central
4. 添加 settings_profile_repo
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<repositories>
<repository>
<id>settings_profile_repo</id>
<name>Repository for JDK 1.8 builds</name>
<url>http://www.myhost.com/maven/jdk18</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
继续编译
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'snapshotPolicy' (position: START_TAG seen ...</layout>\n <snapshotPolicy>... @272:27) @ C:\Users\yello\.m2\settings.xml, line 272, column 27
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from settings_profile_repo: http://www.myhost.com/maven/jdk18/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from pom_profile_repo: http://mirrors.163.com/maven/repository/maven-public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:05 min
[INFO] Finished at: 2019-11-05T15:22:37+08:00
[INFO] Final Memory: 9M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project depe-repo: Could not resolve dependencies for project com.maven.demo:depe-repo:jar:1.0-SNAPSHOT: Failed to collect dependencies at io.micrometer:micrometer-registry-prometheus:jar:3.8.4: Failed to read artifact descriptor for io.micrometer:micrometer-registry-prometheus:jar:3.8.4: Could not transfer artifact io.micrometer:micrometer-registry-prometheus:pom:3.8.4 from/to jdk18 (http://www.myhost.com/maven/jdk18): Failed to transfer file: http://www.myhost.com/maven/jdk18/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom. Return code is: 500 , ReasonPhrase:Internal Server Error. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
sunyelw@windows:depe-repo$
-
settings
中的profile
默认直接生效, 有多少生效多少 - 按从后往前的顺序遍历下载, 直到下载成功或遍历结束
优先级
settings_profile_repo
>pom_profile_pom
>pom_repo
>central
5. 添加settings-mirror
settings
配置文件中还有一个非常好玩的属性 就是 mirror
属性
其用于镜像仓库的访问, 根据需要配置, 一般会配置中央仓库的镜像.
比如加一个 alimaven 的镜像, 用来镜像中央仓库central
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
继续编译
sunyelw@windows:depe-repo$
sunyelw@windows:depe-repo$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building depe-repo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading from settings-profile-repo-01: http://www.myhost.com/maven/jdk18/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from pom_profile_repo: http://mirrors.163.com/maven/repository/maven-public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from pom_repo: http://10.1.228.195:18081/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.833 s
[INFO] Finished at: 2019-11-05T15:43:11+08:00
[INFO] Final Memory: 9M/123M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "settings-profile-repo-02" could not be activated because it does not exist.
[ERROR] Failed to execute goal on project depe-repo: Could not resolve dependencies for project com.maven.demo:depe-repo:jar:1.0-SNAPSHOT: Failed to collect dependencies at io.micrometer:micrometer-registry-prometheus:jar:3.8.4: Failed to read artifact descriptor for io.micrometer:micrometer-registry-prometheus:jar:3.8.4: Could not transfer artifact io.micrometer:micrometer-registry-prometheus:pom:3.8.4 from/to settings-profile-repo-01 (http://www.myhost.com/maven/jdk18): Failed to transfer file: http://www.myhost.com/maven/jdk18/io/micrometer/micrometer-registry-prometheus/3.8.4/micrometer-registry-prometheus-3.8.4.pom. Return code is: 500 , ReasonPhrase:Internal Server Error. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
sunyelw@windows:depe-repo$
- 最后中央仓库的访问被阿里云镜像了, 没找到也不会再去找中央仓库了.
mirror
中的mirrorOf
属性说明
- 没配置就过
- 没匹配也过
- 命中了则直接替换访问
mirror
中的mirrorOf
属性规则
-
*
匹配所有仓库 -
repo1,repo2
匹配repo1
,repo2
仓库,多个仓库用逗号隔开 -
external:*
匹配不在本机的远程仓库 -
*,!repo1
匹配所有仓库,除了repo1
官网
http://maven.apache.org/guides/mini/guide-mirror-settings.html
总结
Maven
的优先级为从高到低为
settings_profile_repo
pom_profile_pom
pom_repo
-
central
然后, 实际访问此仓库前需要先去匹配一下mirrorOf
属性
拓展
1、profiles
属性在settings.xml
和pom.xml
中都有, 区别
-
pom
中的需要显式激活
<activation>
<activeByDefault>true</activeByDefault>
</activation>
-
settings.xml
中的profiles
不需要激活, 默认都是激活状态, 查询时的顺序是倒序
2、 mirror 仓库的使用
- 被
mirrorOf
表达式命中的仓库会直接被换掉,哪怕镜像仓库没找到也不会再去找原仓库
3、中央仓库是可以被覆盖的
<repository>
<id>central</id>
<name>pom_repo_name</name>
<url>http://10.1.228.195:18081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
- 只需要把你自己加的仓库
id
写成central
就行 - 一般不建议这么做,除非一些特殊场景,比如可以在项目中使用私服地址替换中央仓库
网友评论