Maven的倉庫分兩大類:1.本地倉庫 2.遠(yuǎn)程倉庫
1.本地倉庫
maven的本地倉庫,在安裝maven后并不會創(chuàng)建,它是在第一次執(zhí)行maven命令的時(shí)候才被創(chuàng)建。
maven本地倉庫的默認(rèn)位置:無論是Windows還是Linux,在用戶的目錄下都有一個(gè).m2/repository/的倉庫目錄。
修改本地倉庫的方法有兩個(gè):
方法一:修改特定用戶下的本地倉庫
在用戶目錄/.m2/目錄下創(chuàng)建settings.xml文件,然后在~/.m2/settings.xml中配置localRepository元素的值為想要的倉庫地址

方法二:修改全局所有用戶下的本地倉庫
需要修改的文件為M2_HOME/conf/settings.xml,修改的方法同方法一;需要注意的是,這種修改后所有的用戶都會受到影響,而且如果maven進(jìn)行升級,那么所有的配置都會被清除,所以要提前做好備份。
2.遠(yuǎn)程倉庫
遠(yuǎn)程倉庫又可以分為:中央倉庫 、私服、其他公共倉庫
2.1 中央倉庫
maven在安裝的時(shí)候,自帶的就是中央倉庫的配置,配置信息放在M2_HOME/conf/settings.xml中。
中央倉庫包含了絕大多數(shù)流行的開源Java構(gòu)件,以及源碼、作者信息、SCM、信息、許可證信息等。一般來說,簡單的Java項(xiàng)目依賴的構(gòu)件都可以在這里下載到。
所有的maven項(xiàng)目都會繼承超級pom,具體的說,包含了下面配置的pom我們就稱之為超級pom。

2.2 私服
私服是一種特殊的遠(yuǎn)程倉庫,它是架設(shè)在局域網(wǎng)內(nèi)的倉庫服務(wù),供局域網(wǎng)內(nèi)的Maven用戶使用。當(dāng)Maven需要下載構(gòu)件的時(shí)候,它從私服請求,如果私服上不存在該構(gòu)件,則從外部的遠(yuǎn)程倉庫下載,緩存在私服上之后,再為Maven的下載請求提供服務(wù)。
Maven私服的特點(diǎn):
1.節(jié)省自己的外網(wǎng)帶寬:減少重復(fù)請求造成的外網(wǎng)帶寬消耗
2.加速M(fèi)aven構(gòu)件:如果項(xiàng)目配置了很多外部遠(yuǎn)程倉庫的時(shí)候,構(gòu)建速度就會大大降低
3.部署第三方構(gòu)件:有些構(gòu)件無法從外部倉庫獲得的時(shí)候,我們可以把這些構(gòu)件部署到內(nèi)部倉庫(私服)中,供內(nèi)部maven項(xiàng)目使用
4.提高穩(wěn)定性,增強(qiáng)控制:Internet不穩(wěn)定的時(shí)候,maven構(gòu)建也會變的不穩(wěn)定,一些私服軟件還提供了其他的功能
5.降低中央倉庫的負(fù)荷:maven中央倉庫被請求的數(shù)量是巨大的,配置私服也可以大大降低中央倉庫的壓力
當(dāng)前比較主流的maven私服:
1.Apache的Archiva
2.JFrog的Artifactory
3.Sonatype的Nexus
2.3 遠(yuǎn)程倉庫的配置
需要在settings.xml文件中加入<repositories>和<repository>配置;在<repositories>元素下,可以使用 <repository>子元素聲明一個(gè)或者多個(gè)遠(yuǎn)程倉庫。

<updatePolicy>元素表示更新的頻率,有never、always、interval、daily幾個(gè)值,其中daily 為默認(rèn)值。
<checksumPolicy>元素表示maven檢查和檢驗(yàn)文件的策略,有fail、ignore、warn幾個(gè)值,其中warn為默認(rèn)值。
遠(yuǎn)程倉庫的安全配置,在settings.xml文件中加入<servers>和<server>配置,其中這里的<id>必須跟POM中需要認(rèn)證的repository元素的Id一致。

3.Maven解析POM中依賴
(1) Maven直接從POM文件中解析構(gòu)件
(2)根據(jù)依賴坐標(biāo)計(jì)算倉庫路徑后,嘗試直接從本地倉庫尋找構(gòu)件,如果發(fā)現(xiàn)相應(yīng)構(gòu)件,則解析成功
(3)在本地倉庫不存在相應(yīng)的構(gòu)件情況下,如果依賴的版本是顯示的發(fā)布版本構(gòu)件,則遍歷所有的遠(yuǎn)程倉庫,發(fā)現(xiàn)后下載使用
(4)如果依賴的版本是RELEASE或LATEST, 則基于更新策略讀取所有遠(yuǎn)程倉庫的元數(shù)據(jù),將其于本地倉庫的對應(yīng)元數(shù)據(jù)合并后,計(jì)算出RELEASE或者LATEST的真實(shí)值,然后基于這個(gè)真實(shí)值檢查本地倉庫
(5)如果依賴的版本是SNAPSHOT, 則基于更新策略讀取所有遠(yuǎn)程倉庫的元數(shù)據(jù), 將其與本地倉庫的對應(yīng)元數(shù)據(jù)合并后,得到最新快照版本的值,然后基于該值檢查本地倉庫或從遠(yuǎn)程倉庫下載
(6)如果最后解析到的構(gòu)件版本是時(shí)間戳格式的快照,則復(fù)制其時(shí)間戳格式的文件 至 非時(shí)間戳格式,并使用該非時(shí)間戳格式的構(gòu)件
4.一份setting.xml的例子
<?xml version="1.0" encoding="UTF-8"?>
<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">
? ? <!-- 本地倉庫的位置 -->
? ? <localRepository>${user.home}/.m2/repository</localRepository>
? ? <!-- Apache Maven 配置 -->
? ? <pluginGroups/>
? ? <proxies/>
? ? <!-- 私服發(fā)布的用戶名密碼 -->
? ? <servers>
? ? ? ? <server>
? ? ? ? ? ? <id>releases</id>
? ? ? ? ? ? <username>deployment</username>
? ? ? ? ? ? <password>He2019</password>
? ? ? ? </server>
? ? ? ? <server>
? ? ? ? ? ? <id>snapshots</id>
? ? ? ? ? ? <username>deployment</username>
? ? ? ? ? ? <password>He2019</password>
? ? ? ? </server>
? ? </servers>
? ? <!-- 阿里云鏡像 -->
? ? <mirrors>
? ? ? ? <mirror>
? ? ? ? ? ? <id>alimaven</id>
? ? ? ? ? ? <name>aliyun maven</name>
? ? ? ? ? ? <!-- https://maven.aliyun.com/repository/public/ -->
? ? ? ? ? ? <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
? ? ? ? ? ? <mirrorOf>central</mirrorOf>
? ? ? ? </mirror>
? ? </mirrors>
? ? <!-- 配置: java8, 先從阿里云下載, 沒有再去私服下載? -->
? ? <!-- 20190929 hepengju 測試結(jié)果: 影響下載順序的是profiles標(biāo)簽的配置順序(后面配置的ali倉庫先下載), 而不是activeProfiles的順序 -->
? ? <profiles>
? ? ? ? <!-- 全局JDK1.8配置 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>jdk1.8</id>
? ? ? ? ? ? <activation>
? ? ? ? ? ? ? ? <activeByDefault>true</activeByDefault>
? ? ? ? ? ? ? ? <jdk>1.8</jdk>
? ? ? ? ? ? </activation>
? ? ? ? ? ? <properties>
? ? ? ? ? ? ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? ? ? ? ? ? <maven.compiler.source>1.8</maven.compiler.source>
? ? ? ? ? ? ? ? <maven.compiler.target>1.8</maven.compiler.target>
? ? ? ? ? ? ? ? <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
? ? ? ? ? ? </properties>
? ? ? ? </profile>
? ? ? ? <!-- Nexus私服配置: 第三方j(luò)ar包下載, 比如oracle的jdbc驅(qū)動(dòng)等 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>dev</id>
? ? ? ? ? ? <repositories>
? ? ? ? ? ? ? ? <repository>
? ? ? ? ? ? ? ? ? ? <id>nexus</id>
? ? ? ? ? ? ? ? ? ? <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
? ? ? ? ? ? ? ? ? ? <releases>
? ? ? ? ? ? ? ? ? ? ? ? <enabled>true</enabled>
? ? ? ? ? ? ? ? ? ? </releases>
? ? ? ? ? ? ? ? ? ? <snapshots>
? ? ? ? ? ? ? ? ? ? ? ? <enabled>true</enabled>
? ? ? ? ? ? ? ? ? ? </snapshots>
? ? ? ? ? ? ? ? </repository>
? ? ? ? ? ? </repositories>
? ? ? ? ? ? <pluginRepositories>
? ? ? ? ? ? ? ? <pluginRepository>
? ? ? ? ? ? ? ? ? ? <id>public</id>
? ? ? ? ? ? ? ? ? ? <name>Public Repositories</name>
? ? ? ? ? ? ? ? ? ? <url>http://nexus.hepengju.cn:8081/nexus/content/groups/public/</url>
? ? ? ? ? ? ? ? </pluginRepository>
? ? ? ? ? ? </pluginRepositories>
? ? ? ? </profile>
? ? ? ? <!-- 阿里云配置: 提高國內(nèi)的jar包下載速度 -->
? ? ? ? <profile>
? ? ? ? ? ? <id>ali</id>
? ? ? ? ? ? <repositories>
? ? ? ? ? ? ? ? <repository>
? ? ? ? ? ? ? ? ? ? <id>alimaven</id>
? ? ? ? ? ? ? ? ? ? <name>aliyun maven</name>
? ? ? ? ? ? ? ? ? ? <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
? ? ? ? ? ? ? ? ? ? <releases>
? ? ? ? ? ? ? ? ? ? ? ? <enabled>true</enabled>
? ? ? ? ? ? ? ? ? ? </releases>
? ? ? ? ? ? ? ? ? ? <snapshots>
? ? ? ? ? ? ? ? ? ? ? ? <enabled>true</enabled>
? ? ? ? ? ? ? ? ? ? </snapshots>
? ? ? ? ? ? ? ? </repository>
? ? ? ? ? ? </repositories>
? ? ? ? ? ? <pluginRepositories>
? ? ? ? ? ? ? ? <pluginRepository>
? ? ? ? ? ? ? ? ? ? <id>alimaven</id>
? ? ? ? ? ? ? ? ? ? <name>aliyun maven</name>
? ? ? ? ? ? ? ? ? ? <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
? ? ? ? ? ? ? ? </pluginRepository>
? ? ? ? ? ? </pluginRepositories>
? ? ? ? </profile>
? ? </profiles>
? ? <!-- 激活配置 -->
? ? <activeProfiles>
? ? ? ? <activeProfile>jdk1.8</activeProfile>
? ? ? ? <activeProfile>dev</activeProfile>
? ? ? ? <activeProfile>ali</activeProfile>
? ? </activeProfiles>
</settings>