Maven倉庫(四)
在了解Maven如何使用倉庫后,我們將能更高效的使用Maven。
前面我們已經講過Maven的坐標機制,任何Maven項目使用任何構件的方式都是完全相同的。在此基礎上,Maven可以在某個位置統一存儲所有Maven項目共享的構件,這個統一的位置就是倉庫。實際的Maven項目將不再各自存儲其依賴文件,它們只需要聲明這些依賴的坐標,在需要的時候,Maven會自動根據坐標找到倉庫中的構件,并使用它們。
1. Maven倉庫布局
Maven是如何根據坐標來定位路徑的呢?下面舉一個實際的例子:
有這樣一個構件:groupId=org.testng、artifactId=testng、version=5.8、classifier=jdk15、packaging=jar,其對應的路徑生成步驟如下:
1)基于groupId,將org.testng轉換成org/testng/。
2)基于artifactId,路徑變成org/testng/testng/。
3)使用版本信息,得到路徑為org/testng/testng/5.8/。
4)依次加上artifactId,分隔符連字號,以及version,于是構件路徑變成了org/testng/testng/5.8/testng-5.8。
5)如果有classifier信息,路徑就變成org/testng/testng/5.8/testng-5.8-jdk5。
最后由packaging決定構件的擴展名,所以最后的路徑為org/testng/testng/5.8/testng-5.8-jdk5.jar。
2. Maven倉庫的分類
倉庫分類:本地倉庫和遠程倉庫。Maven根據坐標尋找構件的時候,它先會查看本地倉庫,如果本地倉庫存在構件,則直接使用;如果沒有,則從遠程倉庫查找,找到后,下載到本地。
1)本地倉庫
默認情況下,每個用戶在自己的用戶目錄下都有一個路徑名為.m2/repository/的倉庫目錄。我們也可以自定義本地倉庫的地址,如下:
這樣,用戶的本地倉庫被設置成了D:/library/maven/repository/。我們需要從Maven安裝目錄復制$MAVEN_HOME/conf/settings.xml文件復制到D:/library/maven下進行編輯。大家不要直接修改全局目錄下的settings.xml。
我們可以將依賴從遠程倉庫下載到本地倉庫,也可以將本地倉庫安裝到Maven倉庫中。如對前面我們講過的hello-world項目執行mvn clean install(Eclipse中命令為clean install),結果如下:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hello-world 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- [INFO] Deleting D:\code\maven\hello-world\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world --- [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world --- [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world --- [INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.alvinliang.maven.HelloWorldTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.661 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world --- [INFO] Building jar: D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world --- [INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar [INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 13.245s [INFO] Finished at: Thu Nov 28 22:22:25 CST 2013 [INFO] Final Memory: 10M/55M [INFO] ------------------------------------------------------------------------從上述輸出,我們可以看到構件的hello-world-0.0.1-SNAPSHOT.jar,安裝到了本地倉庫D:\library\maven\repository下。
2)遠程倉庫
本地倉庫好比書房,而遠程倉庫就像是書店。對于Maven來說,每個用戶只有一個本地倉庫,但是可以配置多個遠程倉庫。
a.中央倉庫
Maven必須要知道至少一個可用的遠程倉庫,中央倉庫就是這樣一個默認的遠程倉庫,Maven的安裝文件自帶了中央倉庫的配置。在settings文件有如下配置:
中央倉庫包含了這個世界的絕大多數流行的開源Java構件,源碼等。
b)私服
私服是一種特殊的遠程倉庫,它是架設在局域網內的倉庫服務,私服代理廣域網上的遠程倉庫,供局域網內的Maven用戶使用。當Maven需要下載構件的時候,它先從私服請求,如果私服上沒有構件,測從外部的遠程倉庫下載,存在私服上,再為Maven用戶下載。現在可以使用最流行的Maven私服軟件--Nexus。后面專門介紹如何搭建私服。
3. 遠程倉庫的配置
見如下代碼清單:
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://nexus/sourcesense.com/nexus/content/repositories/public/</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repositories>
注意上面的id設置為central,使用nexus官方的倉庫地址來代替中央倉庫,因為有時候中央倉庫很慢。上述配置中設置snapshots為false表示關閉central倉庫對快照版本的下載支持。
4. 遠程倉庫的認證
在局部文件settings.xml中,在servers元素下配置,如下:
<servers> <server> <id>central</id> <username>lb</username> <password>123456</password> </server> <servers>
上述配置中id為關鍵元素,表示對那個遠程倉庫進行認證,這里配置的是中央倉庫central。其中認證的用戶名為lb,密碼為123456。
5. 鏡像
如果倉庫X可以提供倉庫Y存儲的所有內容,那么就可以認為X是Y的一個鏡像。也就是說,任何一個可以從倉庫Y獲得的構件,都可以從它的鏡像中獲取。例如:http://maven.net.cn/content/groups/public/是重要倉庫http://repo1.maven.org/maven2/在中國的鏡像。由于地理位置的原因,鏡像玩玩能夠提供比中央倉庫更快的服務。下面就是本人使用Nexus鏡像來代替中央倉庫的配置,編輯settings.xml,如下:
<mirrors> <mirror> <id>central</id> <mirrorOf>*</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> </mirrors>
上述配置中,mirrorOf指的是為那個倉庫提供鏡像,如果設置為central,表示為中央倉庫的鏡像,設置為*表示為任意倉庫設置的鏡像配置。
6. 倉庫搜索服務
最后我們在為Maven編寫依賴的時候,是不是不知道從何處開始,如何尋找到需要的依賴呢?下面我提供幾個非常不錯的網址:
http://mvnrepository.com/
https://repository.sonatype.org/
打開其中一個頁面,直接搜索關鍵字,可以得到依賴配置,如下圖: