1、 為什么使用Nexus
如果沒有私服,我們所需的所有構(gòu)件都需要通過maven的中央倉庫和第三方的Maven倉庫下載到本地,而一個(gè)團(tuán)隊(duì)中的所有人都重復(fù)的從maven倉庫下 載構(gòu)件無疑加大了倉庫的負(fù)載和浪費(fèi)了外網(wǎng)帶寬,如果網(wǎng)速慢的話,還會(huì)影響項(xiàng)目的進(jìn)程。很多情況下項(xiàng)目的開發(fā)都是在內(nèi)網(wǎng)進(jìn)行的,連接不到maven倉庫怎么 辦呢?開發(fā)的公共構(gòu)件怎么讓其它項(xiàng)目使用?這個(gè)時(shí)候我們不得不為自己的團(tuán)隊(duì)搭建屬于自己的maven私服,這樣既節(jié)省了網(wǎng)絡(luò)帶寬也會(huì)加速項(xiàng)目搭建的進(jìn)程, 當(dāng)然前提條件就是你的私服中擁有項(xiàng)目所需的所有構(gòu)件。
2、Nexus下載
下載地址:http://www.sonatype.org/nexus/go
3、Nexus啟動(dòng)
我下載的是zip包,解壓后進(jìn)入\nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\,根據(jù)操作系統(tǒng)類型選擇文件夾,我選的是windows-x86-32文件夾,進(jìn)入后可看到如下所示bat文件。
圖(1)
雙擊console-nexus.bat運(yùn)行。游覽器中輸入http://127.0.0.1:8081/nexus/,出現(xiàn)圖(2)所示就代表nexus已經(jīng)啟動(dòng)成功。
圖(2)
8081為默認(rèn)的端口號(hào),要修改端口號(hào)可進(jìn)入nexus-2.1.2-bundle\nexus-2.1.2\conf\打開nexus.properties文件,修改application-port屬性值就可以了。
默認(rèn)的用戶名和密碼:admin/admin123,登錄后看到圖(3)所示:
圖(3)
4、Nexus倉庫
nexus的倉庫類型分為以下四種:
group: 倉庫組
hosted:宿主
proxy:代理
virtual:虛擬
首次登陸nexus后可以看到以下一個(gè)倉庫組和多個(gè)倉庫。

圖(4)
Public Repositories: 倉庫組
3rd party: 無法從公共倉庫獲得的第三方發(fā)布版本的構(gòu)件倉庫
Apache Snapshots: 用了代理ApacheMaven倉庫快照版本的構(gòu)件倉庫
Central: 用來代理maven中央倉庫中發(fā)布版本構(gòu)件的倉庫
Central M1 shadow: 用于提供中央倉庫中M1格式的發(fā)布版本的構(gòu)件鏡像倉庫
Codehaus Snapshots: 用來代理CodehausMaven 倉庫的快照版本構(gòu)件的倉庫
Releases: 用來部署管理內(nèi)部的發(fā)布版本構(gòu)件的宿主類型倉庫
Snapshots:用來部署管理內(nèi)部的快照版本構(gòu)件的宿主類型倉庫
4.1、創(chuàng)建Nexus宿主倉庫
在Repositories選項(xiàng)頁的菜單欄上點(diǎn)擊Add按鈕會(huì)出現(xiàn)如下所示,選擇要添加的倉庫類型。
圖(5)
這里我點(diǎn)擊添加宿主類型的倉庫,在倉庫列表的下方會(huì)出現(xiàn)新增倉庫的配置,如下所示:
圖(6)
點(diǎn)擊save按鈕后就會(huì)在倉庫列表中看到剛才新增的倉庫。
4.2、創(chuàng)建Nexus代理倉庫
點(diǎn)擊菜單欄上的Add按鈕后選擇Proxy Repository,看到如下所示配置界面:
圖(7)
4.3、創(chuàng)建Nexus倉庫組
倉庫組和倉庫關(guān)系是一對(duì)多的關(guān)系,一個(gè)倉庫組可以指向多個(gè)倉庫。
點(diǎn)擊菜單欄上的Add按鈕選擇Repository Group就可以看到倉庫組的配置界面,如下所示:
圖(8)
點(diǎn)擊save后就可在倉庫列表中看到新增的倉庫組了,項(xiàng)目中如果要下載構(gòu)件的話,配置文件中一般都用倉庫組的URL。
5、修改Maven配置文件從Nexus下載構(gòu)件
1)如 果想對(duì)操作系統(tǒng)的所有用戶統(tǒng)一配置maven,則只需修改maven\conf\setting.xml 文件就可以了,如果只想對(duì)用戶單獨(dú)配置maven,只需將conf\setting.xml文件復(fù)制到C:\Documents and Settings\Administrator\.m2文件夾下(我這里假設(shè)系統(tǒng)裝在c盤,用戶為Administrator)。
2) 打開setting.xml文件,可以看到如下代碼:
- <!-- localRepository
- | The path to the local repository maven will use to store artifacts.
- |
- | Default: ~/.m2/repository
- <localRepository></localRepository>
- -->
表示如果不設(shè)置localRepository,maven會(huì)默認(rèn)將本地倉庫建到/.m2/repository文件夾下。
設(shè)置localRepository如下代碼所示:
- <localRepository>F:\myCenterRepository</localRepository>
表示在myCenterRepository文件夾下建立本地倉庫。個(gè)人建議不要采用默認(rèn)的倉庫地址,因?yàn)轫?xiàng)目如果很多的話,那么本地倉庫所占的磁盤空間就比較多了,所以指定倉庫地址到其他盤符,更方便管理。
5.2、在POM文件中配置Nexus倉庫 在項(xiàng)目的pom文件中添加如下代碼:
- <repositories>
- <repository>
- <id>nexus</id>
- <name>my-nexus-repository</name>
- <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>nexus</id>
- <name>my-nexus-repository</name>
- <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
在pom文件中配置只對(duì)當(dāng)前項(xiàng)目有效,而實(shí)際開發(fā)中不可能在每個(gè)項(xiàng)目中重復(fù)配置信息,所以不建議在pom文件中配置。
5.3、在setting.xml文件中配置Nexus倉庫 1)maven提供了profile來配置倉庫信息,如下所示:
- <profiles>
- <profile>
- <id>myprofile</id>
- <repositories>
- <repository>
- <id>central</id>
- <url>http://central</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>central</id>
- <url>http://central</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
2) 激活profile
- <activeProfiles>
- <activeProfile>myprofile</activeProfile>
- </activeProfiles>
3)配置鏡像
- <mirrors>
- <mirror>
- <id>nexus</id>
- <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
- <mirrorOf>*</mirrorOf>
- </mirror>
- </mirrors>
這里配置mirrorOf的值為*,代表maven的所有訪問請(qǐng)求都會(huì)指向到Nexus倉庫組。
6、部署構(gòu)件到Nexus倉庫 6.1、maven部署
1) 修改pom文件
在pom文件中添加如下配置:
- <distributionManagement>
- <repository>
- <id>my-nexus-releases</id>
- <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>
- </repository>
-
- <snapshotRepository>
- <id>my-nexus-snapshot</id>
- <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>
- </snapshotRepository>
- </distributionManagement>
2)在setting.xml文件中添加認(rèn)證信息:
- <servers>
- <server>
- <id>my-nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>my-nexus-snapshot</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
上面的配置中我用的是超級(jí)管理員的賬戶,開發(fā)項(xiàng)目中可以改為具有部署構(gòu)件權(quán)限的用戶就可以了。
3)執(zhí)行部署
測(cè)試的構(gòu)件項(xiàng)目信息如下:
- <groupId>com.ez</groupId>
- <artifactId>TestJar</artifactId>
- <version>1.0</version>
- <packaging>jar</packaging>
- <name>TestJar</name>
從上面的信息中可以看出構(gòu)件為發(fā)布版本,所以部署構(gòu)件的話會(huì)自動(dòng)部署至releases倉庫中。
在命令行中執(zhí)行:mvn clean deploy
如果之前沒用執(zhí)行過該命令,maven會(huì)自動(dòng)到中央倉庫中下載部署所需的插件。最后在命令行中看到如下所示就代表構(gòu)件已經(jīng)部署成功。
圖(9)
到nexus的releases倉庫中查看剛剛部署好的構(gòu)件信息如下所示:
圖(10)
如果部署失敗的要檢查一下用戶是否有部署的權(quán)限,目標(biāo)倉庫是否允許部署,是否缺少部署所需的構(gòu)件。
6.2、手動(dòng)部署
手動(dòng)部署構(gòu)件則更為簡(jiǎn)單了,在nexus的倉庫列表中點(diǎn)擊要部署的目標(biāo)倉庫,然后點(diǎn)擊Artifact Upload選項(xiàng)卡看到下圖所示:
圖(11)
通過以上配置運(yùn)用Nexus搭建的私服基本上可以用了,更多配置需求可參考Nexus book.
Nexus book下載地址:
http://download.csdn.net/detail/yaoqinzhou1943/4589583