Appearance
仓库管理
9.1 本地仓库配置
本地仓库是 Maven 存储依赖和构建产物的地方,默认位于用户主目录下的 .m2/repository 目录。
配置本地仓库位置
可以在 settings.xml 文件中配置本地仓库的位置:
xml
<settings>
<localRepository>/path/to/local/repo</localRepository>
<!-- 其他配置 -->
</settings>清理本地仓库
当本地仓库出现问题时,可以清理本地仓库:
- 手动清理:删除本地仓库目录,然后重新构建项目
- 使用命令清理:bash
mvn dependency:purge-local-repository
9.2 远程仓库配置
远程仓库是 Maven 下载依赖的地方,默认使用 Maven 中央仓库。
配置远程仓库
可以在 POM 文件中配置远程仓库:
xml
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>配置插件仓库
插件仓库用于下载 Maven 插件:
xml
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>9.3 私有仓库搭建(Nexus)
私有仓库是企业内部搭建的仓库,用于存储企业内部的依赖和第三方依赖的缓存。Nexus 是一个常用的私有仓库解决方案。
安装 Nexus
- 下载 Nexus:从 Sonatype 官网 下载 Nexus Repository Manager
- 解压安装:解压下载的压缩包到安装目录
- 启动 Nexus:
- Windows:运行
bin/nexus.exe /run - Linux/macOS:运行
bin/nexus run
- Windows:运行
- 访问 Nexus:打开浏览器,访问
http://localhost:8081
配置 Nexus
- 登录 Nexus:默认用户名是
admin,密码在sonatype-work/nexus3/admin.password文件中 - 创建仓库:
- 点击 "Repositories" → "Create repository"
- 选择仓库类型,如 "maven2 (hosted)"、"maven2 (proxy)" 或 "maven2 (group)"
- 填写仓库信息,如名称、URL 等
- 配置权限:设置用户和权限,确保只有授权用户可以访问和部署依赖
配置 Maven 使用 Nexus
在 settings.xml 文件中配置 Nexus 作为远程仓库:
xml
<settings>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>9.4 仓库镜像配置
仓库镜像是对远程仓库的代理,可以提高依赖下载速度,特别是在网络环境较差的情况下。
配置镜像
在 settings.xml 文件中配置镜像:
xml
<settings>
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
<!-- 其他配置 -->
</settings>常用镜像
- 阿里云镜像:https://maven.aliyun.com/repository/public
- 华为云镜像:https://repo.huaweicloud.com/repository/maven/
- 腾讯云镜像:https://mirrors.cloud.tencent.com/nexus/repository/maven-public/
- 中央仓库:https://repo.maven.apache.org/maven2
9.5 依赖缓存管理
依赖缓存机制
Maven 会将下载的依赖缓存到本地仓库,下次构建时如果依赖版本没有变化,就会直接使用本地缓存的依赖,而不会重新下载。
强制更新依赖
当需要强制更新依赖时,可以使用 -U 选项:
bash
mvn clean install -U依赖解析策略
Maven 使用以下策略来解析依赖:
- 本地仓库:首先检查本地仓库中是否存在依赖
- 远程仓库:如果本地仓库中不存在,则从远程仓库下载
- 镜像:如果配置了镜像,则从镜像下载
依赖冲突解决
当多个依赖引入了相同库的不同版本时,Maven 会根据以下规则来解决冲突:
- 路径最短优先:依赖路径越短,优先级越高
- 声明顺序优先:在 POM 文件中声明较早的依赖,优先级越高
依赖分析
使用 mvn dependency:tree 命令可以查看项目的依赖树,帮助分析依赖关系:
bash
mvn dependency:tree使用 mvn dependency:analyze 命令可以分析项目的依赖使用情况:
bash
mvn dependency:analyze通过合理配置和管理仓库,可以提高 Maven 的构建效率,确保依赖的可靠性和一致性。