Windows 下Nexus搭建Maven私服



   

       Windows 下Nexus搭建Maven私服            

       分類:            java            2014-10-16 11:46    577人閱讀    評論(0)    收藏    舉報    html

一、 爲何使用Nexus java

        若是沒有私服,咱們所需的全部構件都須要經過maven的中央倉庫和第三方的Maven倉庫下載到本地,而一個團隊中的全部人都重複的從maven倉庫下 載構件無疑加大了倉庫的負載和浪費了外網帶寬,若是網速慢的話,還會影響項目的進程。不少狀況下項目的開發都是在內網進行的,鏈接不到maven倉庫怎麼 辦呢?開發的公共構件怎麼讓其它項目使用?這個時候咱們不得不爲本身的團隊搭建屬於本身的maven私服,這樣既節省了網絡帶寬也會加速項目搭建的進程, 固然前提條件就是你的私服中擁有項目所需的全部構件。windows

 

二、Nexus下載 網絡

       下載地址:http://www.sonatype.org/nexus/goapp

 

三、Nexus啓動 maven

       我下載的是zip包,解壓後進入\nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\,根據操做系統類型選擇文件夾,我選的是windows-x86-32文件夾,進入後可看到以下所示bat文件。測試




雙擊console-nexus.bat運行。遊覽器中輸入http://127.0.0.1:8081/nexus/,出現圖(2)所示就表明nexus已經啓動成功。url




 

8081爲默認的端口號,要修改端口號可進入nexus-2.1.2-bundle\nexus-2.1.2\conf\打開nexus.properties文件,修改application-port屬性值就能夠了。spa


默認的用戶名和密碼:admin/admin123,登陸後看到圖(3)所示:操作系統


 

圖(3)

 

四、Nexus倉庫

     nexus的倉庫類型分爲如下四種:

               group: 倉庫組

               hosted:宿主

              proxy:代理

              virtual:虛擬

 

            首次登錄nexus後能夠看到如下一個倉庫組和多個倉庫。

 

 



   Public Repositories:  倉庫組

                      3rd party: 沒法從公共倉庫得到的第三方發佈版本的構件倉庫

                      Apache Snapshots: 用了代理ApacheMaven倉庫快照版本的構件倉庫

                      Central: 用來代理maven中央倉庫中發佈版本構件的倉庫

                      Central M1 shadow: 用於提供中央倉庫中M1格式的發佈版本的構件鏡像倉庫

                      Codehaus Snapshots: 用來代理CodehausMaven 倉庫的快照版本構件的倉庫

                      Releases: 用來部署管理內部的發佈版本構件的宿主類型倉庫

                      Snapshots:用來部署管理內部的快照版本構件的宿主類型倉庫

 

4.一、建立Nexus宿主倉庫

在Repositories選項頁的菜單欄上點擊Add按鈕會出現以下所示,選擇要添加的倉庫類型。

 



這裏我點擊添加宿主類型的倉庫,在倉庫列表的下方會出現新增倉庫的配置,以下所示:




 點擊save按鈕後就會在倉庫列表中看到剛纔新增的倉庫。


4.二、建立Nexus代理倉庫


點擊菜單欄上的Add按鈕後選擇Proxy Repository,看到以下所示配置界面:

 


 

 

4.三、建立Nexus倉庫組


倉庫組和倉庫關係是一對多的關係,一個倉庫組能夠指向多個倉庫。

點擊菜單欄上的Add按鈕選擇Repository Group就能夠看到倉庫組的配置界面,以下所示:




 


點擊save後就可在倉庫列表中看到新增的倉庫組了,項目中若是要下載構件的話,配置文件中通常都用倉庫組的URL。


五、修改Maven配置文件從Nexus下載構件

1) 若是想對操做系統的全部用戶統一配置maven,則只需修改maven\conf\setting.xml 文件就能夠了,若是隻想對用戶單獨配置maven,只需將conf\setting.xml文件複製到C:\Documents and Settings\Administrator\.m2文件夾下(我這裏假設系統裝在c盤,用戶爲Administrator)。

 

2)  打開setting.xml文件,能夠看到以下代碼:

 

Java代碼 複製代碼 收藏代碼

  1. <!-- localRepository     

  2.    | The path to the local repository maven will use to store artifacts.     

  3.    |     

  4.    | Default: ~/.m2/repository      

  5.   <localRepository></localRepository>     

  6.  -->  

[java] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <!-- localRepository    

  2.    | The path to the local repository maven will use to store artifacts.    

  3.    |    

  4.    | Default: ~/.m2/repository     

  5.   <localRepository></localRepository>    

  6.  -->  

 

表示若是不設置localRepository,maven會默認將本地倉庫建到/.m2/repository文件夾下。


設置localRepository以下代碼所示:

 

Html代碼 複製代碼 收藏代碼

  1. <localRepository>F:\myCenterRepository</localRepository>    

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <localRepository>F:\myCenterRepository</localRepository>    

 

表示在myCenterRepository文件夾下創建本地倉庫。我的建議不要採用默認的倉庫地址,由於項目若是不少的話,那麼本地倉庫所佔的磁盤空間就比較多了,因此指定倉庫地址到其餘盤符,更方便管理。

 

5.二、在POM文件中配置Nexus倉庫

在項目的pom文件中添加以下代碼:

 

Xml代碼 複製代碼 收藏代碼

  1. <repositories>        

  2.     <repository>        

  3.       <id>nexus</id>        

  4.       <name>my-nexus-repository</name>        

  5.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  6.       <releases>        

  7.         <enabled>true</enabled>        

  8.       </releases>        

  9.       <snapshots>        

  10.         <enabled>false</enabled>        

  11.       </snapshots>        

  12.     </repository>        

  13.   </repositories>        

  14.   <pluginRepositories>        

  15.     <pluginRepository>        

  16.       <id>nexus</id>        

  17.       <name>my-nexus-repository</name>        

  18.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  19.       <releases>        

  20.         <enabled>true</enabled>        

  21.       </releases>        

  22.       <snapshots>        

  23.         <enabled>false</enabled>        

  24.       </snapshots>             

  25.     </pluginRepository>        

  26.   </pluginRepositories>       

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <repositories>       

  2.     <repository>       

  3.       <id>nexus</id>       

  4.       <name>my-nexus-repository</name>       

  5.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  6.       <releases>       

  7.         <enabled>true</enabled>       

  8.       </releases>       

  9.       <snapshots>       

  10.         <enabled>false</enabled>       

  11.       </snapshots>       

  12.     </repository>       

  13.   </repositories>       

  14.   <pluginRepositories>       

  15.     <pluginRepository>       

  16.       <id>nexus</id>       

  17.       <name>my-nexus-repository</name>       

  18.       <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  19.       <releases>       

  20.         <enabled>true</enabled>       

  21.       </releases>       

  22.       <snapshots>       

  23.         <enabled>false</enabled>       

  24.       </snapshots>            

  25.     </pluginRepository>       

  26.   </pluginRepositories>       

 

在pom文件中配置只對當前項目有效,而實際開發中不可能在每一個項目中重複配置信息,因此不建議在pom文件中配置。

 

5.三、在setting.xml文件中配置Nexus倉庫

1)maven提供了profile來配置倉庫信息,以下所示:

 

Xml代碼 複製代碼 收藏代碼

  1. <profiles>     

  2.     <profile>     

  3.       <id>myprofile</id>     

  4.       <repositories>     

  5.             <repository>     

  6.                 <id>central</id>                                        

  7.                 <url>http://central</url>                           

  8.                 <releases>     

  9.                     <enabled>true</enabled>     

  10.                 </releases>     

  11.                 <snapshots>     

  12.                     <enabled>true</enabled>     

  13.                 </snapshots>     

  14.             </repository>     

  15.         </repositories>        

  16.          <pluginRepositories>     

  17.             <pluginRepository>     

  18.               <id>central</id>     

  19.               <url>http://central</url>     

  20.               <releases>     

  21.                 <enabled>true</enabled>     

  22.               </releases>     

  23.               <snapshots>     

  24.                 <enabled>false</enabled>     

  25.               </snapshots>     

  26.             </pluginRepository>     

  27.         </pluginRepositories>     

  28.     </profile>     

  29. </profiles>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <profiles>    

  2.     <profile>    

  3.       <id>myprofile</id>    

  4.       <repositories>    

  5.             <repository>    

  6.                 <id>central</id>                                       

  7.                 <url>http://central</url>                          

  8.                 <releases>    

  9.                     <enabled>true</enabled>    

  10.                 </releases>    

  11.                 <snapshots>    

  12.                     <enabled>true</enabled>    

  13.                 </snapshots>    

  14.             </repository>    

  15.         </repositories>       

  16.          <pluginRepositories>    

  17.             <pluginRepository>    

  18.               <id>central</id>    

  19.               <url>http://central</url>    

  20.               <releases>    

  21.                 <enabled>true</enabled>    

  22.               </releases>    

  23.               <snapshots>    

  24.                 <enabled>false</enabled>    

  25.               </snapshots>    

  26.             </pluginRepository>    

  27.         </pluginRepositories>    

  28.     </profile>    

  29. </profiles>    

 

 

 

2) 激活profile

Xml代碼 複製代碼 收藏代碼

  1. <activeProfiles>     

  2.     <activeProfile>myprofile</activeProfile>     

  3.   </activeProfiles>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <activeProfiles>    

  2.     <activeProfile>myprofile</activeProfile>    

  3.   </activeProfiles>    

 

3)配置鏡像

Xml代碼 複製代碼 收藏代碼

  1. <mirrors>     

  2.     <mirror>        

  3.      <id>nexus</id>         

  4.      <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>        

  5.      <mirrorOf>*</mirrorOf>        

  6.    </mirror>     

  7.  </mirrors>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <mirrors>    

  2.     <mirror>       

  3.      <id>nexus</id>        

  4.      <url>http://127.0.0.1:7788/nexus/content/groups/public/</url>       

  5.      <mirrorOf>*</mirrorOf>       

  6.    </mirror>    

  7.  </mirrors>    

 

這裏配置mirrorOf的值爲*,表明maven的全部訪問請求都會指向到Nexus倉庫組。

 

六、部署構件到Nexus倉庫

6.一、maven部署

1) 修改pom文件

在pom文件中添加以下配置:

Xml代碼 複製代碼 收藏代碼

  1. <distributionManagement>     

  2.        <repository>     

  3.            <id>my-nexus-releases</id>     

  4.            <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>     

  5.        </repository>     

  6.             

  7.        <snapshotRepository>     

  8.            <id>my-nexus-snapshot</id>     

  9.            <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>     

  10.        </snapshotRepository>     

  11.   </distributionManagement>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <distributionManagement>    

  2.        <repository>    

  3.            <id>my-nexus-releases</id>    

  4.            <url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>    

  5.        </repository>    

  6.            

  7.        <snapshotRepository>    

  8.            <id>my-nexus-snapshot</id>    

  9.            <url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>    

  10.        </snapshotRepository>    

  11.   </distributionManagement>    

 2)在setting.xml文件中添加認證信息:

Xml代碼 複製代碼 收藏代碼

  1. <servers>     

  2.      <server>     

  3.       <id>my-nexus-releases</id>     

  4.       <username>admin</username>     

  5.       <password>admin123</password>     

  6.     </server>     

  7.     <server>     

  8.       <id>my-nexus-snapshot</id>     

  9.       <username>admin</username>     

  10.       <password>admin123</password>     

  11.     </server>     

  12. </servers>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <servers>    

  2.      <server>    

  3.       <id>my-nexus-releases</id>    

  4.       <username>admin</username>    

  5.       <password>admin123</password>    

  6.     </server>    

  7.     <server>    

  8.       <id>my-nexus-snapshot</id>    

  9.       <username>admin</username>    

  10.       <password>admin123</password>    

  11.     </server>    

  12. </servers>    

 上面的配置中我用的是超級管理員的帳戶,開發項目中能夠改成具備部署構件權限的用戶就能夠了。

3)執行部署

測試的構件項目信息以下:

Xml代碼 複製代碼 收藏代碼

  1. <groupId>com.ez</groupId>     

  2.   <artifactId>TestJar</artifactId>     

  3.   <version>1.0</version>     

  4.   <packaging>jar</packaging>     

  5.   <name>TestJar</name>    

[xml] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. <groupId>com.ez</groupId>    

  2.   <artifactId>TestJar</artifactId>    

  3.   <version>1.0</version>    

  4.   <packaging>jar</packaging>    

  5.   <name>TestJar</name>    

 

從上面的信息中能夠看出構件爲發佈版本,因此部署構件的話會自動部署至releases倉庫中。

 

在命令行中執行:mvn clean deploy

若是以前沒用執行過該命令,maven會自動到中央倉庫中下載部署所需的插件。最後在命令行中看到以下所示就表明構件已經部署成功。

 

 


到nexus的releases倉庫中查看剛剛部署好的構件信息以下所示:

 


若是部署失敗的要檢查一下用戶是否有部署的權限,目標倉庫是否容許部署,是否缺乏部署所需的構件。


6.二、手動部署

手動部署構件則更爲簡單了,在nexus的倉庫列表中點擊要部署的目標倉庫,而後點擊Artifact Upload選項卡看到下圖所示:

 


經過以上配置運用Nexus搭建的私服基本上能夠用了,更多配置需求可參考Nexus book.


Nexus book下載地址:http://download.csdn.net/detail/yaoqinzhou1943/4589583



       

    相關文章
    相關標籤/搜索