在 Maven 中,mirror
(镜像)和 repository
(仓库)虽然都与下载依赖有关,但它们的角色和使用场景是 完全不同 的。下面详细解释:
概念 | 定义 | 作用 |
---|---|---|
repository(仓库) | 存放 JAR 包、POM 文件、插件等构件的真实位置 | 是依赖下载的源头 |
mirror(镜像) | 原始仓库的代理地址(通常速度更快) | 替代或覆盖原仓库的访问地址,提高下载速度或解决访问限制 |
Maven 默认使用以下几个仓库:
~/.m2/repository
settings.xml
修改本地仓库地址<localRepository>/path/to/local/repo</localRepository>php54 Bytes© 菜鸟-创作你的创作
pom.xml
中手动添加项目级仓库:<repositories> <repository> <id>my-repo</id> <url>https://my-repo.com/repo</url> </repository></repositories>php122 Bytes© 菜鸟-创作你的创作
mirror
是针对仓库的一种“代理替代”。它通常在 settings.xml
中设置,用于:
<mirrors> <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <name>Aliyun Maven</name> <url>https://maven.aliyun.com/repository/central</url> </mirror></mirrors>php190 Bytes© 菜鸟-创作你的创作
关键字段解释:
字段 | 说明 |
---|---|
id | 镜像 ID,自定义即可 |
mirrorOf | 指定要替代的仓库,一般填 central |
url | 镜像地址(实际下载地址) |
项目 | repository | mirror |
---|---|---|
本质 | 真实的依赖存储仓库 | 替代某个仓库的“替身” |
配置位置 | pom.xml 或 settings.xml | 仅在 settings.xml 中 |
示例 | https://repo1.maven.org/maven2 | https://maven.aliyun.com/ |
用途 | 下载、上传依赖 | 优化下载速度、规避网络问题 |
替代关系 | 原始依赖源 | 可自动替代原始仓库 |
以下是一个完整的 settings.xml
示例,包含:
settings.xml
示例(适用于 Maven 3.x)<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 1. 本地仓库地址 --> <localRepository>/Users/yourname/.m2/repository</localRepository> <!-- 2. 配置镜像(阿里云 Maven 中央仓库) --> <mirrors> <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <name>Aliyun Central Mirror</name> <url>https://maven.aliyun.com/repository/central</url> </mirror> </mirrors> <!-- 3. 公司私服凭证(如 Nexus、Artifactory) --> <servers> <server> <id>company-nexus</id> <username>your-username</username> <password>your-password</password> </server> </servers> <!-- 4. 构建 profile 中设置私服仓库 --> <profiles> <profile> <id>use-company-repo</id> <repositories> <repository> <id>company-nexus</id> <name>Company Nexus Repo</name> <url>https://nexus.company.com/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <!-- 激活 profile --> <activeProfiles> <activeProfile>use-company-repo</activeProfile> </activeProfiles></settings>php1.42 KB© 菜鸟-创作你的创作
~/.m2/settings.xml
(macOS/Linux)
C:\Users\你的用户名\.m2\settings.xml
(Windows)yourname
:你的系统用户名your-username
和 your-password
:私服登录凭据https://nexus.company.com/...
:私服地址https://www.52runoob.com/archives/5351
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。