美文网首页Maven 系列
Maven - 人生若只如初见(一)

Maven - 人生若只如初见(一)

作者: sunyelw | 来源:发表于2019-12-17 21:25 被阅读0次

前段时间碰到一个Maven依赖加载问题
前段时间熬了个夜解决了
解决过程中算是终于把Maven入门了
这个神奇的问题准备下次再讲~

这篇先讲讲Maven中的依赖加载顺序


前言

加载顺序, 就是当一个依赖你本地找不到时, 会去哪些仓库找, 这些仓库的搜索顺序就是加载顺序.

本地找不到 和 本地不存在 是两个意思哦~~~

那么问题来了,可以在哪些地方配置仓库?
我总结了几个,如若有漏请留言告知,感激不尽!

  • pom_repopom.xml文件的 repositories 中配置
  • pom-profile-repopom.xml文件的 profiles 中配置
  • settings_profile_reposetting.xml文件的profiles中配置
  • mirror_reposetting.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_repocentral

这时候我们添加一个不存在的依赖, 再看看

为了方便查看结果,下面我们都使用一个不存在的依赖来测试

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.xmlpom.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就行
  • 一般不建议这么做,除非一些特殊场景,比如可以在项目中使用私服地址替换中央仓库

相关文章

  • 人生若只如,初见

    看图写诗 人生若只如,初见 文/爱乐 人生若只如,初见 你的花瓣,...

  • 喜欢的文章45

    人生若只如初见,但人生若只如初见 李会莹 2014-11-29 00:34:56 人生若只如初见,如星般灿烂。 人...

  • 人生若只如初见

    702、人生若只如初见,我跟你什么愁什么怨。——是俊 703、人生若只如初见,说了再见别再见。 704、人生若只如...

  • 《人生若只初相见》

    “人生若只如初见,何事西风悲画扇” ——题记 人生若只如初见。初见时,翩若惊鸿,婉如游龙,如蝶舞般曼妙轻盈,似夏花...

  • 人生若只如“初见”

    在现实社会上,每个人都力图给别人留下良好的“第一印象"…… 那你是选择听信“相由心生”,还是觉得“日久见人心”?看...

  • 人生若只如,初见

    关于爱情,有太多美丽的谎言。 我们都在自欺和欺人中坚信天荒地老,海枯石烂,我们以为心诚则灵,真情不变。却独独忘记了...

  • 人生若只如——初见

    人生若只如——初见 叶子 岁月摧折 是一道看不见的篱笆 留给我们青春飞扬 还有无奈 还有感伤 当白浪翻涌 海面上鸥...

  • Maven - 人生若只如初见(一)

    前段时间碰到一个Maven依赖加载问题前段时间熬了个夜解决了解决过程中算是终于把Maven入门了这个神奇的问题准备...

  • 《末爱》

    人生若只如初见, 怀念似明月当空! 人生若只如情深, 离别当肝肠寸断! 人生若只如真爱, 舍身该义无反顾…… 人生...

  • 2019年11月22号

    人生若只如初见,风轻水柔! 人生若只如初见,情真意切! 人生若只如初见,执子之手,偕老一世! 人生若只如初见,得一...

网友评论

    本文标题:Maven - 人生若只如初见(一)

    本文链接:https://www.haomeiwen.com/subject/gcpenctx.html