版權所有,未經授權,禁止轉載
章節
Maven Repository/存儲庫,顧名思義是一個存儲JAR文件的倉庫,Maven根據項目中pom.xml文件中提供的jar包依賴信息,從存儲庫中查找並獲取需要的jar包。
Maven Repository有3種類型:
- Local Repository - 本地庫
- Central Repository - 中央庫
- Remote Repository - 遠程庫
Maven搜索依賴項時,會按照:本地庫、中央庫和遠程庫的順序進行。
如果這些庫中沒找到依賴項,Maven將報錯。
1. Local Repository
Maven本地存儲庫是本機中的一個目錄。如果目錄不存在,執行maven時就會先創建它。
默認情況下,maven本地存儲庫是%USER_HOME%/.m2
目錄,例如:C:\Users\Kevin\.m2
本地存儲庫目錄設置
可以通過修改settings.xml文件來更改maven本地存儲庫的位置。
settings.xml文件位於MAVEN_HOME/conf/settings
中,例如:D:\opt\apache-maven-3.6.1-bin\apache-maven-3.6.1\conf\settings.xml
。
settings.xml中的默認配置:
...
<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
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
...
</settings>
修改本地存儲庫路徑,如下所示:
settings.xml
...
<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>d:/maven-local-repo</localRepository>
...
</settings>
這樣,本地存儲庫的路徑就被修改為:d:/maven-local-repo
2. Central Repository
Maven中央庫主要放置公共jar包,是由apache maven社區創建的,中央庫的網址是http://repo1.maven.org/maven2,可以通過網址http://search.maven.org/#browse查看有哪些公共jar包。
3. Remote Repository
Maven遠程庫也是位於網絡上的存儲庫。例如一個公司可能有很多共享的jar包文件,就可以搭建一個公司內部的遠程庫,供眾多開發人員使用;中央庫可以認為是一個特殊的遠程庫。
可以在pom.xml中配置遠程庫,添加下面內容到pom.xml中就配置了一個遠程庫(只是一個示例,遠程庫網址無效):
<repositories>
<repository>
<id>qikegu.code</id>
<url>http://maven.qikegu.com/maven2/lib</url>
</repository>
</repositories>
Maven官方網站mvnrepository.com可以查找jar包及其相關信息,例如下面是spring core jar包的maven依賴配置信息,可以通過這些配置獲取spring core jar。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>