Maven仓库及镜像配置

摘要

  • maven仓库和镜像的配置及其关联方式

Maven依赖查找顺序(按优先级排序,由高到低)

  • 1.本地仓库

    • 路径:settings.xml<localRepository> 指定的位置,默认 ~/.m2/repository
    • 优先级最高:如果本地已经有对应版本,Maven 默认直接使用,不会再去远程仓库(除非 -U 或本地缺失/损坏)。
  • 2.settings.xml 中的 Profile 仓库

    • 路径:settings.profiles.profile.repositories.repository
    • 只有在 settings.xml 里激活了 <activeProfiles>,对应的 Profile 才会生效。
    • 优先级高于 pom.xml 里的所有仓库配置。
  • 3.pom.xml 中的 Profile 仓库

    • 路径:project.profiles.profile.repositories.repository
    • 必须在 settings.xml 中通过 <activeProfiles> 激活对应的 Profile 才会生效。
    • 优先级低于 settings.xmlProfile 仓库,高于 pom.xml 普通仓库。
  • 4.pom.xml 中的普通仓库

    • 路径:project.repositories.repository
    • 通常用来指定项目专用的仓库,比如公司内部的Nexus、Artifactory。
  • 5.Maven 默认中央仓库

  • 6.镜像(Mirror)会改变顺序

    • 如果你在 settings.xml 里配置了 <mirrors>,那么所有远程仓库的请求都会被重定向到镜像地址。
    • 比如配置了 Nexus 作为中央仓库的镜像,所有原本去 https://repo.maven.apache.org/maven2 的请求,都会被重定向到 Nexus。
    • 即便是 Profile 声明的仓库,其在拉取镜像时也会优先匹配是否存在镜像仓库,如果匹配成功,则优先使用镜像仓库。

说明,仓库的查找顺序实际上就是最终的pom.xml中仓库的声明顺序,所以可以通过如下命令导出最终的pom信息进行查看

1
mvn help:effective-pom -Doutput=EffectivePom.xml

仓库配置–repository

  • 可以将仓库信息配置在settings.xmlpom.xml中,如下可以配置多个,按顺序依次查找,注意这里的id必须唯一,repositorypluginRepository的id可以相同:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>

<pluginRepository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

镜像代理–mirror

说明

  • 由于网络等原因导致我们不能很顺利的从仓库下载到对应的依赖,此时我们可以为仓库配置镜像代理,加快下载速度,国内一般会使用阿里云作为镜像代理

  • mirror镜像可以配置多个,每个mirrorOf里可以配置如下内容:

  1. * : 匹配所有仓库,一般我们搭建了自己的私服时推荐这样设置

  2. repo1Id : 匹配仓库id为repo1Id的仓库

  3. repo1Id,repo2Id : 匹配仓库id为repo1Id和repo2Id的仓库

  4. *,!repo1Id : 匹配所有仓库,但不包含repo1Id

  5. external:* : 匹配所有不在本机上的远程仓库,也就是说,如果仓库的url里配置的是localhost或者file://则不进行代理

镜像代理需要配置在setting.xml中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<mirrors>
<mirror>
<id>aliyun-mirror</id>
<name>aliyun-mirror</name>
<!-- mirrorOf 里配置要代理的仓库id,这样当请求该仓库时就会走这个镜像代理 -->
<!-- maven默认配置的仓库id就是central,此时就是使用阿里云作为默认仓库的代理 -->
<mirrorOf>central</mirrorOf>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

<mirror>
<id>nexus-mirror</id>
<name>nexus-mirror</name>
<!-- mirrorOf 里配置要代理的仓库id,这样当请求该仓库时就会走这个镜像代理 -->
<!-- 此时就是使用nexus私服代理所有仓库 -->
<mirrorOf>*</mirrorOf>
<url>http://nexus.my.local:8080/repository/maven-public/</url>
</mirror>
</mirrors>

maven3.8.1以后的配置中会禁止http协议的仓库地址,该如何处理?

  • 我们可以看到setting.xml中的镜像配置里默认增加了如下配置,其会禁止所有http协议的仓库被请求,目的是推荐使用https协议

1
2
3
4
5
6
7
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
  • 此时执行mvn时会报告如下错误:

1
maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories
  • 但是很多公司的私服都没有配置证书,我们可以修改其配置如下,即可解决

1
2
3
4
5
6
7
<mirror>
<id>nexus</id>
<!-- <mirrorOf>external:*</mirrorOf> -->
<mirrorOf>external:http:*</mirrorOf>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<blocked>false</blocked>
</mirror>

仓库配置–profile分环境配置多个仓库

  • 一般我们在公司都会连接公司的私服,但是私服一般不支持外网访问,如果不能vpn到公司的环境,我们就需要切换镜像的地址,此时可以通过配置profile的方式进行解决,我们可以配置多个profile,使用时激活对应的profile即可

  • 配置profile允许maven根据不同的环境采用不同的maven配置,一个profiles标签中可以有很多个profile,只需要根据不同的项目环境,激活不同的profile即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>

<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>

</profiles>

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
  • 默认激活的就是nexus对应的私服,激活其它profile可以修改配置文件的activeProfile,或者使用如下命令

1
mvn clean package -P aliyun
  • 注意,如果此时setting.xml文件中还配置了镜像,如下所示,那么此时所有的http:开头的的远程请求都会被映射到这个镜像,也就是说,此时profile中配置的url是什么已经不重要了,只要其是http:开头的,就会被映射到这个镜像中。

1
2
3
4
5
6
7
<mirror>
<id>nexus</id>
<!-- <mirrorOf>external:*</mirrorOf> -->
<mirrorOf>external:http:*</mirrorOf>
<url>http://nexus.my.local:8081/repository/maven-public/</url>
<blocked>false</blocked>
</mirror>

Maven 命令详解

  • mvn 的一些重要参数

参数 全写形式 作用说明 常见使用场景
-o --offline 离线模式,Maven 不会访问远程仓库,只使用本地缓存依赖。 在无网络环境或依赖已完整缓存时加快构建速度。
-U --update-snapshots 强制更新 SNAPSHOT 依赖;本地有 release 版本时,也会去远程检查更新。 SNAPSHOT 版本需立即更新,或本地依赖缓存损坏需要重新下载。
-P --activate-profiles 激活指定的 Profile(可多个,逗号分隔)。 按需构建不同环境(如 dev、test、prod)。
-Dkey=value 设置 Maven 属性,替代 pom.xmlsettings.xml 中的属性。 在命令行传入参数,如 -Dmaven.test.skip=true 跳过测试。
-X 打印 Maven 调试日志,显示完整的依赖下载和执行细节。 构建失败时用于排查问题。
-q --quiet 安静模式,只输出必要信息。 构建日志太多时,想减少输出内容。
-e --errors 构建失败时打印堆栈信息,帮助调试错误。 构建失败但日志信息不够详细时使用。
-B --batch-mode 非交互模式,避免构建过程中弹出交互式提示。 自动化构建(如 CI/CD)中常用。
-T n 并行构建,n 可以是数字或 1C 表示按 CPU 核数自动分配线程。 多模块项目加快构建速度,如 -T 1C
-pl --projects 指定要构建的模块(可多个,逗号分隔)。 多模块项目只构建部分模块。
-am --also-make -pl 配合使用,自动构建所依赖的模块。 构建单个模块时,确保依赖模块也被构建。
-amd --also-make-dependents -pl 配合使用,自动构建依赖该模块的下游模块。 构建单个模块时,确保所有依赖它的模块也被构建。
-rf --resume-from 从指定模块重新开始构建,跳过之前已成功的模块。 多模块项目构建中途失败时,节省时间。
-nsu --no-snapshot-updates 禁用 SNAPSHOT 更新,始终使用本地缓存的 SNAPSHOT 版本。 不想在构建时浪费时间去检查 SNAPSHOT 更新。
-Dmaven.test.skip=true 跳过测试代码的编译和执行。 构建时暂时跳过测试,节省时间。
-DskipTests 跳过测试代码的执行,但仍会编译测试代码。 需要测试类编译但不想执行测试用例时使用。
  • 构建与清理

命令 作用 说明
mvn clean 清理项目 删除 target 目录,清除上次构建生成的文件
mvn compile 编译项目 编译主代码,不执行测试
mvn test-compile 编译测试代码 仅编译测试类,不运行测试
mvn test 运行测试 执行单元测试,不打包
mvn package 打包 编译 + 测试 + 打包生成 jar/war/zip 等
mvn install 安装到本地仓库 构建完成后把产物安装到本地仓库(~/.m2/repository)
mvn deploy 部署到远程仓库 用于 CI/CD,将构建好的产物上传到远程仓库
  • 依赖管理

命令 作用 说明
mvn dependency:tree 打印依赖树 帮助查看依赖层级和冲突
mvn dependency:analyze 分析依赖 检查未使用的依赖和缺失依赖
mvn dependency:purge-local-repository 清理本地依赖 可以指定依赖重新下载 -DmanualInclude=groupId:artifactId
mvn dependency:get -Dartifact=groupId:artifactId:version 拉取指定依赖 不用构建项目也能下载依赖
mvn dependency:copy-dependencies 复制依赖 将所有依赖复制到指定目录
  • 生命周期与插件

命令 作用 说明
mvn help:effective-pom 查看生效的 POM 合并 parent、profiles 后的最终配置
mvn help:all-profiles 列出所有 Profile 包含激活状态,方便调试
mvn help:active-profiles 列出当前激活的 Profile 快速查看哪些 Profile 被启用
mvn clean verify 编译+测试+校验 包括集成测试
mvn validate 验证 POM 检查项目配置正确性
mvn site 生成站点报告 项目文档、依赖分析、测试覆盖率等
  • 构建优化与调试

命令 作用 说明
mvn -o install 离线构建 不访问远程仓库,只使用本地缓存
mvn clean install -U 强制更新依赖 SNAPSHOT 或 release 版本强制从远程仓库拉取
mvn -X 调试模式 打印详细执行日志,帮助排查问题
mvn -T 1C clean install 并行构建 多模块项目加快构建,1C 表示按 CPU 核数
mvn -pl module1,module2 -am install 构建指定模块 并自动构建依赖模块,适合多模块项目
  • 快速跳过测试

命令 作用 说明
mvn install -DskipTests 跳过测试执行 测试代码会编译,但不运行
mvn install -Dmaven.test.skip=true 跳过测试编译和执行 完全跳过测试,构建更快