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)件的宿主類型倉庫

5、配置nexus

5.1、開啟遠(yuǎn)程索引

新搭建的neuxs環(huán)境只是一個(gè)空的倉庫,需要手動(dòng)和遠(yuǎn)程中心庫進(jìn)行同步,nexus默認(rèn)是關(guān)閉遠(yuǎn)程索引下載,最重要的一件事情就是開啟遠(yuǎn)程索引下載。登陸nexus系統(tǒng),默認(rèn)用戶名密碼為admin/admin123。

點(diǎn)擊左邊Administration菜單下面的Repositories,找到右邊倉庫列表中的三個(gè)倉庫Apache Snapshots,Codehaus Snapshots和Maven Central,然后再?zèng)]有倉庫的configuration下把Download Remote Indexes修改為true。如下圖

 

 

然后在Apache Snapshots,Codehaus Snapshots和Maven Central這三個(gè)倉庫上分別右鍵,選擇Repari Index,這樣Nexus就會(huì)去下載遠(yuǎn)程的索引文件。

 

 

 

 

這樣設(shè)置以后, Nexus會(huì)自動(dòng)從遠(yuǎn)程中央倉庫下載索引文件, 為了檢驗(yàn)索引文件自動(dòng)下載是否生效,可以卻換到Browse Index

 

 

在左邊菜單欄里面有個(gè)Artifact Search, 在輸入框里面輸入你想要搜索的構(gòu)件名字,比如:maven, 那么查詢結(jié)果如下:

 

 

5.2建立宿主倉庫

新 建公司的內(nèi)部倉庫,步驟為Repositories –> Add –> Hosted Repository,在頁面的下半部分輸入框中填入Repository ID和Repository Name即可,比如分別填入

zfy 和 zfy repostiory,另外把Deployment Policy設(shè)置為Allow Redeploy,點(diǎn)擊save就創(chuàng)建完成了。

這里我點(diǎn)擊添加宿主類型的倉庫,在倉庫列表的下方會(huì)出現(xiàn)新增倉庫的配置,如下所示:

圖(6)

點(diǎn)擊save按鈕后就會(huì)在倉庫列表中看到剛才新增的倉庫。

 

 

5.3、創(chuàng)建Nexus倉庫組

Nexus 中倉庫組的概念是Maven沒有的,在Maven看來,不管你是hosted也好,proxy也好,或者group也好,對(duì)我都是一樣的,我只管根據(jù) groupId,artifactId,version等信息向你要構(gòu)件。為了方便Maven的配置,Nexus能夠?qū)⒍鄠€(gè)倉庫,hosted或者 proxy合并成一個(gè)group,這樣,Maven只需要依賴于一個(gè)group,便能使用所有該group包含的倉庫的內(nèi)容。

neuxs- 1.9.2.3中默認(rèn)自帶了一個(gè)名為“Public Repositories”組,點(diǎn)擊該組可以對(duì)他保護(hù)的倉庫進(jìn)行調(diào)整,把剛才建立的公司內(nèi)部倉庫zfy repostiory加入其中,這樣就不需要再在maven中明確指定內(nèi)部倉庫 的地址了。同時(shí)創(chuàng)建一個(gè)Group ID為public-snapshots、Group Name為Public Snapshots Repositories的組,把Apache Snapshots、Codehaus Snapshots、Snapshots和zfy repostiory加入其中。

 

 

 

 

 

 

 

到這里neuxs的安裝配置就完成了,下面介紹如何在maven中使用自己的私服。

 

 

 

5.4、創(chuàng)建Nexus代理倉庫

點(diǎn)擊菜單欄上的Add按鈕后選擇Proxy Repository,看到如下所示配置界面:

圖(7)

      以上本人分別創(chuàng)建 了  hosted和group ,沒有創(chuàng)建proxy,經(jīng)maven測(cè)試通過。
 
 
6、      settings.xml的配置
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   
  3.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  5.   
  6.   <pluginGroups></pluginGroups>  
  7.   <proxies></proxies>  
  8.   
  9.   <servers>  
  10.       <server>  
  11.       <id>nexus-releases</id>  
  12.       <username>admin</username>  
  13.       <password>admin123</password>  
  14.     </server>  
  15.     <server>  
  16.       <id>nexus-snapshots</id>  
  17.       <username>admin</username>  
  18.       <password>admin123</password>  
  19.     </server>  
  20.   </servers>  
  21.   
  22.   <mirrors>   
  23.     <mirror>   
  24.       <id>nexus-releases</id>   
  25.       <mirrorOf>*</mirrorOf>   
  26.       <url>http://localhost:8081/nexus/content/groups/public</url>   
  27.     </mirror>  
  28.     <mirror>   
  29.       <id>nexus-snapshots</id>   
  30.       <mirrorOf>*</mirrorOf>   
  31.       <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>   
  32.     </mirror>   
  33.   </mirrors>   
  34.    
  35.   <profiles>  
  36.    <profile>  
  37.       <id>nexus</id>  
  38.       <repositories>  
  39.         <repository>  
  40.           <id>nexus-releases</id>  
  41.           <url>http://nexus-releases</url>  
  42.           <releases><enabled>true</enabled></releases>  
  43.           <snapshots><enabled>true</enabled></snapshots>  
  44.         </repository>  
  45.         <repository>  
  46.           <id>nexus-snapshots</id>  
  47.           <url>http://nexus-snapshots</url>  
  48.           <releases><enabled>true</enabled></releases>  
  49.           <snapshots><enabled>true</enabled></snapshots>  
  50.         </repository>  
  51.       </repositories>  
  52.       <pluginRepositories>  
  53.          <pluginRepository>  
  54.                 <id>nexus-releases</id>  
  55.                  <url>http://nexus-releases</url>  
  56.                  <releases><enabled>true</enabled></releases>  
  57.                  <snapshots><enabled>true</enabled></snapshots>  
  58.                </pluginRepository>  
  59.                <pluginRepository>  
  60.                  <id>nexus-snapshots</id>  
  61.                   <url>http://nexus-snapshots</url>  
  62.                 <releases><enabled>true</enabled></releases>  
  63.                  <snapshots><enabled>true</enabled></snapshots>  
  64.              </pluginRepository>  
  65.          </pluginRepositories>  
  66.     </profile>  
  67.   </profiles>  
  68.   
  69.   <activeProfiles>  
  70.       <activeProfile>nexus</activeProfile>  
  71.   </activeProfiles>  
  72.    
  73. </settings>  
  1.    
  1. 7、 maven 項(xiàng)目 的pom.xml配置  
  1.    
  1. <pre class="html" name="code"> <distributionManagement>  
  2.         <!-- 兩個(gè)ID必須與 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->  
  3.         <repository>  
  4.             <id>nexus-releases</id>  
  5.             <name>Nexus Release Repository</name>  
  6.             <url>http://localhost:8081/nexus/content/repositories/releases</url>  
  7.         </repository>  
  8.         <snapshotRepository>  
  9.             <id>nexus-snapshots</id>  
  10.             <name>Nexus Snapshot Repository</name>  
  11.             <url>http://localhost:8081/nexus/content/repositories/snapshots</url>  
  12.         </snapshotRepository>  
  13.     </distributionManagement></pre><pre class="html" name="code"> </pre><pre class="html" name="code">8、mvn:deploy命令上傳包,會(huì)發(fā)現(xiàn)下載路徑必不是中央庫的路徑,而是nexus里面的倉庫組路徑。這個(gè)說明配置環(huán)境成功。</pre><pre class="html" name="code"><img alt="" src="http://img.my.csdn.net/uploads/201301/10/1357787083_3866.png"><img alt="" src="http://img.my.csdn.net/uploads/201301/10/1357787109_8992.png"></pre><pre class="html" name="code"> </pre>  
  14. <pre></pre>  
  15. <pre></pre>