Maven遠程倉庫搭建與配置

咱們知道,Maven倉庫包括本地倉庫和遠程倉庫,而遠程倉庫又包括中央倉庫,私服,Jboss倉庫和Java.net倉庫等。 html

私服,即私有的倉庫服務器,它不只能夠下降中央倉庫負荷,節省外網帶寬,還能夠加速Maven構建,本身部署構件等,從而高效地使用Maven。對於私服,咱們可使用倉庫管理軟件來建立本身的私有倉庫服務器。目前網上流行的倉庫管理軟件有Apache的Achiva,JFrog的Artifactory和Sonatype的Nexus。因爲當前Nexus是最流行的倉庫管理軟件,所以,咱們這裏採用Nexus。 windows

Nexus的安裝很簡單,只需從http://nexus.sonatype.org/downloads下載最新的版本便可。須要注意的是官網向咱們提供了兩種安裝方式,一種是自帶Web容器(Jetty)的Bundle包,另一種是war包。(能夠直接部署到Tomcat等Web容器中) 瀏覽器

假設下載的是Bundle包,解壓到指定目錄後(筆者的目錄是E:\sonatype-nexus),進入目錄E:\sonatype-nexus\nexus-2.4.0-09-bundle\nexus-2.4.0-09\bin\jsw\windows-x86-32\,找到腳本文件console-nexus.bat,打開並運行。若是沒有出現錯誤,打開瀏覽器輸入http://localhost:8081/nexus/index.html#welcome,能夠看到nexus的歡迎界面: 安全


默認狀況下是匿名登陸該網站,只能享受瀏覽,搜索等功能。若是想擁有更多控制能力,能夠點擊右上角的Login in進行登陸,Nexus內置了一個管理員帳戶,用戶名是admin,密碼是admin123。 服務器


下面開始建立本身的宿主倉庫,目的是爲了將本身編寫的構件部署到該倉庫中,供組織其它成員下載。 maven

用管理員帳戶登陸以後,在左邊導航欄框中找到Repositories,點擊後,在右框中選擇Add按鈕,彈出的菜單中選擇Hosted Repository,表示添加宿主倉庫。 網站

在新建宿主倉庫表中,填寫倉庫的ID,Name以及倉庫策略(release表示發佈版構件,snapshot表示快照版構件),好比: ui


點擊save按鈕,完成策略爲Release的宿主倉庫建立。 url

同理,建立策略爲Snapshot的宿主倉庫。 spa


至此,咱們的宿主倉庫(包括髮布版和快照版)就完成了。

接下來,須要添加權限(Privileges)

在左邊導航欄的Security中找到Privileges,點擊後,在右框中點擊Add按鈕,選擇Repository Target Privilleges


按如圖所示添加發布版和快照版。


接下來建立具備相應權限的角色(Role)


最後建立用戶。

這裏須要注意的是,User ID就是遠程倉庫的username,password就是遠程倉庫的password。


至此,宿主倉庫就算完成了。


由於遠程倉庫須要安全認證,因此須要在settings.xml中進行配置。

打開settings.xml,在settings根標籤下添加以下內容:

<server>
		<id>pro-snapshot</id>
		<username>foo</username>
		<password>123456</password>
	</server>
	
	<server>
		<id>pro-release</id>
		<username>foo</username>
		<password>123456</password>
	</server>

這裏的用戶名和密碼對應以前建立的user的userId和password。即foo和123456。另外,id元素對應pom.xml中distributionManagement裏的id元素。以後會給出相應的代碼。


安全認證配置好後,接下來打開你要部署的maven項目的pom.xml,在project元素下添加以下內容:

<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元素下添加了發佈版倉庫和快照版倉庫,它們的id對應以前在settings.xml中server的id。url爲倉庫地址,該url能夠在nexus的repositories中找到:


最後一步,打開命令提示符窗口,定位到maven根目錄下(和pom.xml同目錄),執行maven命令:

mvn clean deploy

若是配置沒有錯誤,會出現如下相似打印信息:

[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] ------------------------------------------------------------------------

進入http://localhost:8081/nexus網頁,在RestBus-Releases(由於在pom.xml中,version是1.0,不是快照版)中,能夠找到剛剛部署上去的構件bus-location-1.0.jar


固然,也能夠在網頁上進行手工部署。

相關文章
相關標籤/搜索