我們知道,Maven倉(cāng)庫(kù)包括本地倉(cāng)庫(kù)和遠(yuǎn)程倉(cāng)庫(kù),而遠(yuǎn)程倉(cāng)庫(kù)又包括中央倉(cāng)庫(kù),私服,Jboss倉(cāng)庫(kù)和Java.net倉(cāng)庫(kù)等。
私服,即私有的倉(cāng)庫(kù)服務(wù)器,它不僅可以降低中央倉(cāng)庫(kù)負(fù)荷,節(jié)省外網(wǎng)帶寬,還可以加速M(fèi)aven構(gòu)建,自己部署構(gòu)件等,從而高效地使用Maven。對(duì)于私服,我們可以使用倉(cāng)庫(kù)管理軟件來(lái)創(chuàng)建自己的私有倉(cāng)庫(kù)服務(wù)器。目前網(wǎng)上流行的倉(cāng)庫(kù)管理軟件有Apache的Achiva,JFrog的Artifactory和Sonatype的Nexus。由于當(dāng)前Nexus是最流行的倉(cāng)庫(kù)管理軟件,因此,我們這里采用Nexus。
Nexus的安裝很簡(jiǎn)單,只需從http://nexus.sonatype.org/downloads下載最新的版本即可。需要注意的是官網(wǎng)向我們提供了兩種安裝方式,一種是自帶Web容器(Jetty)的Bundle包,另外一種是war包。(可以直接部署到Tomcat等Web容器中)
假設(shè)下載的是Bundle包,解壓到指定目錄后(筆者的目錄是E:\sonatype-nexus),進(jìn)入目錄E:\sonatype-nexus\nexus-2.4.0-09-bundle\nexus-2.4.0-09\bin\jsw\windows-x86-32\,找到腳本文件console-nexus.bat,打開并運(yùn)行。如果沒有出現(xiàn)錯(cuò)誤,打開瀏覽器輸入http://localhost:8081/nexus/index.html#welcome,可以看到nexus的歡迎界面:
默認(rèn)情況下是匿名登錄該網(wǎng)站,只能享受瀏覽,搜索等功能。如果想擁有更多控制能力,可以點(diǎn)擊右上角的Login in進(jìn)行登錄,Nexus內(nèi)置了一個(gè)管理員賬戶,用戶名是admin,密碼是admin123。
下面開始創(chuàng)建自己的宿主倉(cāng)庫(kù),目的是為了將自己編寫的構(gòu)件部署到該倉(cāng)庫(kù)中,供組織其它成員下載。
用管理員賬戶登錄之后,在左邊導(dǎo)航欄框中找到Repositories,點(diǎn)擊后,在右框中選擇Add按鈕,彈出的菜單中選擇Hosted Repository,表示添加宿主倉(cāng)庫(kù)。
在新建宿主倉(cāng)庫(kù)表中,填寫倉(cāng)庫(kù)的ID,Name以及倉(cāng)庫(kù)策略(release表示發(fā)布版構(gòu)件,snapshot表示快照版構(gòu)件),比如:
點(diǎn)擊save按鈕,完成策略為Release的宿主倉(cāng)庫(kù)創(chuàng)建。
同理,創(chuàng)建策略為Snapshot的宿主倉(cāng)庫(kù)。
至此,我們的宿主倉(cāng)庫(kù)(包括發(fā)布版和快照版)就完成了。
接下來(lái),需要添加權(quán)限(Privileges)
在左邊導(dǎo)航欄的Security中找到Privileges,點(diǎn)擊后,在右框中點(diǎn)擊Add按鈕,選擇Repository Target Privilleges
按如圖所示添加發(fā)布版和快照版。
接下來(lái)創(chuàng)建具有相應(yīng)權(quán)限的角色(Role)
最后創(chuàng)建用戶。
這里需要注意的是,User ID就是遠(yuǎn)程倉(cāng)庫(kù)的username,password就是遠(yuǎn)程倉(cāng)庫(kù)的password。
至此,宿主倉(cāng)庫(kù)就算完成了。
因?yàn)檫h(yuǎn)程倉(cāng)庫(kù)需要安全認(rèn)證,所以需要在settings.xml中進(jìn)行配置。
打開settings.xml,在settings根標(biāo)簽下添加如下內(nèi)容:
- <server>
- <id>pro-snapshot</id>
- <username>foo</username>
- <password>123456</password>
- </server>
-
- <server>
- <id>pro-release</id>
- <username>foo</username>
- <password>123456</password>
- </server>
這里的用戶名和密碼對(duì)應(yīng)之前創(chuàng)建的user的userId和password。即foo和123456。另外,id元素對(duì)應(yīng)pom.xml中distributionManagement里的id元素。之后會(huì)給出相應(yīng)的代碼。
安全認(rèn)證配置好后,接下來(lái)打開你要部署的maven項(xiàng)目的pom.xml,在project元素下添加如下內(nèi)容:
- <distributionManagement>
- <repository>
- <id>pro-release</id>
- <name>Proj Release Repository</name>
- <url>http://localhost:8081/nexus/content/repositories/RestBus-Releases</url>
- </repository>
-
- <snapshotRepository>
- <id>pro-snapshot</id>
- <name>Proj Snapshot Repository</name>
- <url>http://localhost:8081/nexus/content/repositories/RestBus-Snapshots</url>
- </snapshotRepository>
- </distributionManagement>
這里在distributionManagement元素下添加了發(fā)布版?zhèn)}庫(kù)和快照版?zhèn)}庫(kù),它們的id對(duì)應(yīng)之前在settings.xml中server的id。url為倉(cāng)庫(kù)地址,該url可以在nexus的repositories中找到:
最后一步,打開命令提示符窗口,定位到maven根目錄下(和pom.xml同目錄),執(zhí)行maven命令:
mvn clean deploy
如果配置沒有錯(cuò)誤,會(huì)出現(xiàn)以下類似打印信息:
- [INFO] Scanning for projects...
- [INFO]
- [INFO] ------------------------------------------------------------------------
- [INFO] Building bus-location 1.0
- [INFO] ------------------------------------------------------------------------
- [INFO]
- [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ bus-location ---
- [INFO] Deleting G:\bus-location\target
- [INFO]
- [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ bus-location ---
- [debug] execute contextualize
- [INFO] Using 'UTF-8' encoding to copy filtered resources.
- [INFO] skip non existing resourceDirectory G:\bus-location\src\main\resources
- [INFO]
- [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ bus-location ---
- [INFO] Compiling 1 source file to G:\bus-location\target\classes
- [INFO]
- [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ bus-location ---
- [debug] execute contextualize
- [INFO] Using 'UTF-8' encoding to copy filtered resources.
- [INFO] skip non existing resourceDirectory G:\bus-location\src\test\resources
- [INFO]
- [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ bus-location ---
- [INFO] Compiling 1 source file to G:\bus-location\target\test-classes
- [INFO]
- [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ bus-location ---
- [INFO] Surefire report directory: G:\bus-location\target\surefire-reports
-
- -------------------------------------------------------
- T E S T S
- -------------------------------------------------------
- Running zjut.edu.soft.bus.AppTest
- Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
-
- Results :
-
- Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
-
- [INFO]
- [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ bus-location ---
- [INFO] Building jar: G:\bus-location\target\bus-location-1.0.jar
- [INFO]
- [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ bus-location ---
- [INFO] Installing G:\bus-location\target\bus-location-1.0.jar to C:\Users\tsw\.m2\repository\zjut\edu\soft\bus\bus-location\1.0\bus-location-1.0.jar
- [INFO] Installing G:\bus-location\pom.xml to C:\Users\tsw\.m2\repository\zjut\edu\soft\bus\bus-location\1.0\bus-location-1.0.pom
- [INFO]
- [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ bus-location ---
- Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.jar
- 2/3 KB
- 3/3 KB
-
- Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.jar (3 KB at 7.3 KB/sec)
- Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.pom
- 2/2 KB
-
- Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.pom (2 KB at 10.1 KB/sec)
- Downloading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml
-
- Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml
- 305/305 B
-
- Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml (305 B at 3.1 KB/sec)
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 4.762s
- [INFO] Finished at: Wed May 22 13:23:16 CST 2013
- [INFO] Final Memory: 14M/33M
- [INFO] ------------------------------------------------------------------------
進(jìn)入http://localhost:8081/nexus網(wǎng)頁(yè),在RestBus-Releases(因?yàn)樵趐om.xml中,version是1.0,不是快照版)中,可以找到剛剛部署上去的構(gòu)件bus-location-1.0.jar
當(dāng)然,也可以在網(wǎng)頁(yè)上進(jìn)行手工部署。