Maven 仓库是项目中依赖的第三方库,这个库所在的位置叫做仓库。
Maven 仓库有三种类型:
- 本地(local)
- 中央(central)
- 远程(remote)
4.1 本地仓库
默认情况下,不管Linux还是 Windows,Maven 本地仓库默认被创建在 %USER_HOME% 目录下, 一个路径名为 .m2/repository/ 的仓库目录。要修改默认本地仓库的位置,在%M2_HOME%\conf 目录中的 Maven 的 settings.xml 文件中定义另一个路径,示例如下:
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>D:/work/maven/repository-my</localRepository>
</settings>
PS:Maven 的本地仓库,在安装 Maven 后并不会创建,它是在第一次执行 maven 命令的时候才被创建。
4.2 中央仓库
Maven 中央仓库是由 Maven 社区提供的仓库,其中包含了大量常用的库。
中央仓库包含了绝大多数流行的开源Java构件,以及源码、作者信息、SCM、信息、许可证信息等。一般来说,简单的Java项目依赖的构件都可以在这里下载到。
中央仓库的关键概念:
- 这个仓库由 Maven 社区管理。
- 不需要配置。
- 需要通过网络才能访问。
4.3 远程仓库
远程仓库开发人员自己定制仓库,包含了所需要的代码库或者其他工程中用到的 jar 文件
在%M2_HOME%\conf 目录中的 Maven 的 settings.xml 文件中配置定制的远程仓库,示例如下:
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<profile>
<id>alwaysActiveProfile</id>
<repositories>
<repository>
<id>nexusmy</id>
<name>Team Maven Repository</name>
<url>http://192.168.0.2:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexusmy</id>
<name>Team Maven Repository</name>
<url>http://192.168.0.2:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
</activeProfiles>
</settings>
4.4 镜像仓库
Maven 中央仓库默认在国外, 国内使用难免速度很慢,国内有些组织提供了仓库,比如阿里云的仓库称为镜像仓库
配置镜像仓库如下:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<repository>
<id>alimaven</id>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
4.5 maven仓库的查找顺序
默认配置情况下,仓库的搜索如下:
- 步骤 1 在本地仓库寻找,如果找不到则执行步骤 2,如果找到了则执行后续操作。
- 步骤 2 在中央仓库中寻找,如果找不到,并且有一个或多个远程仓库已经设置,则执行步骤 4,如果找到了则下载到本地仓库中以备将来引用。
- 步骤 3 如果远程仓库没有被设置,Maven 将简单的停滞处理并抛出错误(无法找到依赖的文件)。
- 步骤 4 在一个或多个远程仓库中搜索依赖的文件,如果找到则下载到本地仓库以备将来引用,否则 Maven 将停止处理并抛出错误(无法找到依赖的文件)。
配置了私服以及镜像的情况下,仓库的搜索如下:
- 在本地仓库搜索,如果没有则进入下一步
- 在全局配置的私服仓库 (settings.xml) 中搜索,如果发现该仓库有镜像设置,则去镜像中地址中搜索,如果没有则下一步
- 在项目自身配置的私服仓库 (pom.xml) 中搜索,如果发现该仓库有镜像设置,则去镜像中地址中搜索,如没有则下一步
- 在中央仓库中搜索,如果发现该仓库有镜像设置,则去镜像中地址中搜索,如果没有Maven 将停止处理并抛出错误(无法找到依赖的文件)。
注意点:
- 构件的搜索不会发生在被镜像代理的仓库,例如:阿里云镜像配置了
<mirrorOf>central</mirrorOf>
代理了中央仓库 ,则不会在中央仓库搜索 - 如果仓库的配置中id配置为
<id>central</id>
,则会覆盖中央仓库。 - settings.xml中配置的profile(激活的)下的respository优先级高于项目中pom文件配置的respository
- 使用
mvn --settings "D:\work\software\apache-maven-3.8.3\conf\settings_my.xml" help:effective-pom
可以查看生效的pom配置
4.6 当前主流的maven私服
- Apache的Archiva
- JFrog的Artifactory
- Sonatype的Nexus