在使用`npm`的时候,我们加入了`--registry=https://registry.npm.taobao.org`参考,来将注册服务器指向了阿里提供的服务器。优秀的阿里同时也为我们准备了`maven`的国内服务器。本节中,我们共同学习如何将`maven`源指向阿里提供的国内服务器。 ## 介绍 `MAVEN`的官方为:[https://maven.apache.org/](https://maven.apache.org/),看官方地址我们能看出来,这个东西其实是`apache`的。所以如果说官方的`maven`仓库,那也应该是`apache`的,地址如下:[https://mvnrepository.com/](https://mvnrepository.com/),习惯性的被称为中央仓库(central repository),这也是`maven`会默认查找的仓库,大多数的包都位于这个仓库上。此外`MAVEN`也是支持非中央仓库的,比如`google`仓库,`spring`仓库,还有我们今天要使用的阿里仓库。但使用非中央仓库需要一些设置,如何设置这些信息正是本节要讲的内容。设置完非中央仓库后,`maven` 在查找依赖的时候,会优先查找设置的仓库,如果找不到则会继续找中央仓库。 阿里的仓库服务[https://maven.aliyun.com/mvn/view](https://maven.aliyun.com/mvn/view),主要是为了解决由于网络造成的问题,所以其仓库的内容同步了几个主要仓库的内容: ![](https://img.kancloud.cn/19/70/1970e36bab6ebcf2a5b609cfc4d1c541_1523x436.png) 上图表示:我们可以使用https://maven.aliyun.com/repository/central来替换中央仓库,可以使用https://maven.aliyun.com/repository/google来替换google仓库。 ## 查看MAVEN配置文件 在sheel中执行`mvn -X`后会出现一系列的信息,我们从中找到如下关键字:`[DEBUG] Reading global settings from`,比如我的找到的是: ``` [DEBUG] Reading global settings from /usr/local/Cellar/maven/3.5.2/libexec/conf/settings.xml [DEBUG] Reading user settings from /Users/panjie/.m2/settings.xml ``` 上述信息提示了maven的全局配置文件及用户定义的配置文件的位置分别为:`/usr/local/Cellar/maven/3.5.2/libexec/conf/settings.xml`及`/Users/panjie/.m2/settings.xml`。 ## 设置仓库 接下来我们尝试编辑用户自定义配置文件,比如我刚刚查看自己的电配置文件位于:`/Users/panjie/.m2/settings.xml`。 >[info] 如果无此文件则复制一份全局配置文件过来,按提示复制一下就好。比如此时应复制`/usr/local/Cellar/maven/3.5.2/libexec/conf/settings.xml 至 /Users/panjie/.m2/settings.xml`。 找到`<mirrors>`标签及`</mirrors>`标签,删除其中的注释并添加如下代码: ```xml <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/central</url> </mirror> ``` 添加后的样子如下: ```xml <mirrors> <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/central</url> </mirror> </mirrors> ``` 此时,再执行`mvn install`时便会首先由aliyun的仓库下载的。