哪怕我不知道Maven
的具体依赖加载顺序,我都敢肯定会先从本地仓库开始查找。
那么问题来了,我现在就碰到了这个有点颠覆我认知的问题,我本地仓库有一模一样的依赖,但打包就是打不进去!
下面是解决问题的定位过程,让我们开始这次冒险~
检查所有配置
1、pom
已知项目中手动把central
给覆盖了
sunyelw@windows:user-cur-api$
sunyelw@windows:user-cur-api$ mvn help:effective-pom | grep -5 central
<id>settings-profile-repo-01</id>
<name>Repository for test settings-profile-repo</name>
<url>http://www.myhost.com/maven/jdk18</url>
</repository>
<repository>
<id>central</id>
<name>ASIAINFO MIGU REPO</name>
<url>http://10.1.228.195:18081/nexus/content/groups/public</url>
</repository>
<repository>
<id>thirdparty</id>
--
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
sunyelw@windows:user-cur-api$
2、setting.xml
sunyelw@windows:user-cur-api$
sunyelw@windows:user-cur-api$ mvn help:effective-settings
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building user-cur-api PACKT-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:effective-settings (default-cli) @ pinkstone-cur-api ---
[INFO]
Effective user-specific configuration settings:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!-- -->
<!-- Generated by Maven Help Plugin on 2019-12-17T09:37:51 -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -->
<!-- -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!-- -->
<!-- Effective Settings for 'yello' on 'DESKTOP-S0L3088' -->
<!-- -->
<!-- ====================================================================== -->
<settings xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<localRepository xmlns="http://maven.apache.org/SETTINGS/1.1.0">F:\repository</localRepository>
<mirrors xmlns="http://maven.apache.org/SETTINGS/1.1.0">
<mirror>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<id>alimaven</id>
</mirror>
</mirrors>
</settings>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.958 s
[INFO] Finished at: 2019-12-17T21:37:52+08:00
[INFO] Final Memory: 16M/143M
[INFO] ------------------------------------------------------------------------
sunyelw@windows:user-cur-api$
- 这里配了个
aliyun maven
来镜像central
仓库
思路1 - 本地缓存
查看本地缓存是否有问题,index.properties
中会打印报错信息 (每个index
文件夹都是一个仓库)
${USER_HOME}\.IntelliJIdea2017.3\system\Maven\Indices
-
\index4\index.properties
--local
-
\index10\index.properties
--central
没发现明显问题.
思路2 - 搜索顺序
首先分析jar
包搜索顺序, 肯定是优先本地仓库的,找不到才会去各个文件中配置的仓库寻找
- 报这个错说明本地仓库没找到
- 而本地仓库存在, 也即没识别
又进行了以下尝试
- 直接注掉
mirror
-
mirrorsOf
属性含义 -
maven
依赖加载优先级 -
mvn
命令定位执行过程<这一步很重要,下一篇Maven
就是你了~> - 删掉
_remote.repositories
- 不直接删除文件, 删除
_remote.repositories
文件中的central
问题源头 - _remote.repositories
文件
先把问题 jar
包的 _remote.repositories
文件拎出来看看
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri Nov 01 15:06:44 CST 2019
migu-metrics-1.0.2.pom>central=
migu-metrics-1.0.2.jar>central=
这个文件是做什么的?
-
migu-metrics-1.0.2.pom>central=
pom 文件 从central
中加载 -
migu-metrics-1.0.2.jar>central=
jar 包 从central
中加载 -
>central=
用于表明传递依赖的加载方式, 这里显示是从central
中加载
为啥会加载不到?
- 加载依赖
com.migu.tsg:migu-metrcs:1.0.2
, 读取到这个依赖中的_remote.repositories
文件, 然后尝试访问central
加载其传递依赖 - 由于本地加了中央仓库代理, 会去访问
alimaven
, 然后alimaven
是没有这个依赖的, 就报错alimaven
找不到com.migu.tsg:migu-metrcs:1.0.2
解决方法
- 删除
_remote.repositories
文件
问题解决后的思考
既然这样, 那么走私服的依赖应该都有问题, 为啥只有这个 com.migu.tsg:migu-metrcs:1.0.2
依赖报错 ?
查看了下其他正常加载的 jar
包的 _remote.repositories
文件
- 本地打包的依赖
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Tue Oct 22 10:18:07 CST 2019
pinkstone-api-PACKT-SNAPSHOT.jar>=
pinkstone-api-PACKT-SNAPSHOT.pom>=
-
alimaven
拉下来的
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri Dec 21 17:16:30 CST 2018
gax-bom-1.35.0.pom>alimaven=
#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice.
#Tue Jul 10 12:27:28 CST 2018
commons-lang-2.1.jar>alimaven=
commons-lang-2.1.pom>alimaven=
commons-lang-2.1.jar>central=
commons-lang-2.1.pom>central=
- 这个包就有两个记录了, 曾经从
alimaven
拉取过, 后又从central
拉取了一次
思考后解决方案
- 让其默认走本地(删掉
central
)
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri Nov 01 15:06:44 CST 2019
migu-metrics-1.0.2.pom>=
migu-metrics-1.0.2.jar>=
当然,也可以删除全部此类文件
find ~/.m2/ -name _remote.repositories | xargs rm
_remote.repositories
文件的作用到底是什么?
网友评论