Maven 仓库

Maven 仓库(Repository)完全解析(2025 最新版)

一句话总结
Maven 仓库 = 本地缓存 + 远程下载源,通过 pom.xmlsettings.xml 配置,实现依赖自动拉取与加速!

Maven 中央仓库:全球最大开源仓库,URL 为 https://repo.maven.apache.org/maven2/https://repo1.maven.org/maven2/


一、仓库类型分类

类型说明位置/URL配置方式
本地仓库Maven 运行机器上的缓存目录,存储下载的 JAR、本地构建 artifact默认:~/.m2/repository/settings.xml<localRepository>
远程仓库外部服务器,提供依赖下载任意 http://https://pom.xml<repositories>
中央仓库默认远程仓库,Apache 官方https://repo.maven.apache.org/maven2/无需配置(默认)
镜像仓库中央仓库的代理,加速下载阿里云:https://maven.aliyun.com/repository/publicsettings.xml<mirrors>
私有仓库公司内部 Nexus/Artifactoryhttp://nexus.company.compom.xmlsettings.xml

仓库优先级:本地 > 镜像 > 远程(pom.xml 定义) > 中央


二、本地仓库(Local Repository)

  • 作用:缓存所有下载的依赖、插件,避免重复下载;存储 mvn install 的本地 artifact。
  • 默认路径
  • Windows:C:\Users\{用户名}\.m2\repository
  • macOS/Linux:/Users/{用户名}/.m2/repository~/.m2/repository
  • 自定义路径settings.xml):
  <settings>
    <localRepository>D:\maven\repository</localRepository>
  </settings>

清理本地仓库

# 删除特定 artifact
rm -rf ~/.m2/repository/com/example/my-app

# 或全删(慎用)
rm -rf ~/.m2/repository

三、远程仓库配置(pom.xml)

<project> 中添加 <repositories><pluginRepositories>

<repositories>
    <repository>
        <id>aliyun-public</id>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases><enabled>true</enabled></releases>  <!-- 正式版 -->
        <snapshots><enabled>false</enabled></snapshots> <!-- 快照版 -->
    </repository>
</repositories>

<pluginRepositories>  <!-- 插件专用仓库 -->
    <pluginRepository>
        <id>aliyun-plugin</id>
        <url>https://maven.aliyun.com/repository/public</url>
    </pluginRepository>
</pluginRepositories>

注意:项目级配置优先于全局,但推荐全局镜像。


四、镜像配置(settings.xml)—— 加速神器!

路径:Maven 安装目录 conf/settings.xml~/.m2/settings.xml

1. 阿里云镜像(推荐,10x 加速!)

<settings>
  <mirrors>
    <mirror>
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>  <!-- * 表示镜像所有仓库 -->
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
</settings>
  • 其他阿里云子仓库(如 Spring):
  <mirror>
    <id>aliyun-spring</id>
    <mirrorOf>spring</mirrorOf>
    <url>https://maven.aliyun.com/repository/spring</url>
  </mirror>

2. 其他镜像示例

镜像URL配置 <mirrorOf>
华为云https://mirrors.huaweicloud.com/repository/maven/*
腾讯云https://mirrors.cloud.tencent.com/nexus/repository/maven-public/*

验证镜像

mvn help:system  # 查看下载源

五、私有仓库(Nexus / Artifactory)

1. 配置认证(settings.xml)

<servers>
  <server>
    <id>nexus-company</id>  <!-- 与 pom.xml 中 <id> 匹配 -->
    <username>admin</username>
    <password>123456</password>
  </server>
</servers>

2. pom.xml 中声明

<repositories>
  <repository>
    <id>nexus-company</id>
    <url>http://nexus.company.com/repository/maven-public/</url>
  </repository>
</repositories>

发布到私有仓库

mvn deploy  # pom.xml 中 <distributionManagement>
<distributionManagement>
  <repository>
    <id>nexus-releases</id>
    <url>http://nexus.company.com/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus-snapshots</id>
    <url>http://nexus.company.com/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

六、插件仓库(Plugin Repositories)

插件(如 maven-compiler-plugin)也需仓库:

<pluginRepositories>
  <pluginRepository>
    <id>aliyun-plugin</id>
    <url>https://maven.aliyun.com/repository/public</url>
  </pluginRepository>
</pluginRepositories>

七、常见问题解决

问题解决方案
下载慢/失败配置阿里云镜像,检查网络
Artifact not foundmvn dependency:purge-local-repository 清理本地重下
认证失败检查 <servers> 用户名/密码
镜像不生效<mirrorOf>*</mirrorOf> 确保覆盖中央
SNAPSHOT 更新添加 <updatePolicy>always</updatePolicy>

强制更新

mvn clean install -U

八、最佳实践

  1. 全局镜像settings.xml 配置阿里云 *
  2. 项目仓库:仅声明私有/特殊仓库。
  3. 本地路径:自定义到大磁盘(如 D:\repo)。
  4. 企业级:部署 Nexus 统一管理。

九、快速配置命令

# 1. 编辑 settings.xml 添加阿里云镜像

# 2. 测试
mvn help:system | grep repo

恭喜!你已掌握 Maven 仓库配置!


下一步推荐

方向内容
Nexus 部署Docker 一键搭建私有仓库
依赖分析mvn dependency:tree
CI/CD 仓库Jenkins + Nexus

需要 Nexus 私有仓库搭建教程?回复 Nexus 立即获取!

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注