1. 程式人生 > >Windows 下Nexus搭建Maven私服

Windows 下Nexus搭建Maven私服

4.3 快照 激活 nap cti sha 基本 新增 項目

轉自:http://blog.csdn.net/fanyuna/article/details/40145827

1、 為什麽使用Nexus

如果沒有私服,我們所需的所有構件都需要通過maven的中央倉庫和第三方的Maven倉庫下載到本地,而一個團隊中的所有人都重復的從maven倉庫下載構件無疑加大了倉庫的負載和浪費了外網帶寬,如果網速慢的話,還會影響項目的進程。很多情況下項目的開發都是在內網進行的,連接不到maven倉庫怎麽辦呢?開發的公共構件怎麽讓其它項目使用?這個時候我們不得不為自己的團隊搭建屬於自己的maven私服,這樣既節省了網絡帶寬也會加速項目搭建的進程,當然前提條件就是你的私服中擁有項目所需的所有構件。

2、Nexus下載

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

3、Nexus啟動

我下載的是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已經啟動成功。

技術分享

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


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


技術分享

圖(3)

4、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.1、創建Nexus宿主倉庫

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


技術分享


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

技術分享
點擊save按鈕後就會在倉庫列表中看到剛才新增的倉庫。
4.2、創建Nexus代理倉庫

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


技術分享

4.3、創建Nexus倉庫組

倉庫組和倉庫關系是一對多的關系,一個倉庫組可以指向多個倉庫。

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

技術分享

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


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

1) 如果想對操作系統的所有用戶統一配置maven,則只需修改maven\conf\setting.xml 文件就可以了,如果只想對用戶單獨配置maven,只需將conf\setting.xml文件復制到C:\Documents and Settings\Administrator\.m2文件夾下(我這裏假設系統裝在c盤,用戶為Administrator)。

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

    <!-- localRepository     
       | The path to the local repository maven will use to store artifacts.     
       |     
       | Default: ~/.m2/repository      
      <localRepository></localRepository>     
     -->  

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

設置localRepository如下代碼所示:

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

表示在myCenterRepository文件夾下建立本地倉庫。個人建議不要采用默認的倉庫地址,因為項目如果很多的話,那麽本地倉庫所占的磁盤空間就比較多了,所以指定倉庫地址到其他盤符,更方便管理。

5.2、在POM文件中配置Nexus倉庫

在項目的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>    
<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文件中配置只對當前項目有效,而實際開發中不可能在每個項目中重復配置信息,所以不建議在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的所有訪問請求都會指向到Nexus倉庫組。

6、部署構件到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文件中添加認證信息:
    <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>    

上面的配置中我用的是超級管理員的賬戶,開發項目中可以改為具有部署構件權限的用戶就可以了。

3)執行部署

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

    <groupId>com.ez</groupId>     
      <artifactId>TestJar</artifactId>     
      <version>1.0</version>     
      <packaging>jar</packaging>     
      <name>TestJar</name>    

從上面的信息中可以看出構件為發布版本,所以部署構件的話會自動部署至releases倉庫中。

在命令行中執行:mvn clean deploy

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


技術分享

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


技術分享

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


6.2、手動部署

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


技術分享

通過以上配置運用Nexus搭建的私服基本上可以用了,更多配置需求可參考Nexus book.
Nexus book下載地址:http://download.csdn.net/detail/yaoqinzhou1943/4589583

Windows 下Nexus搭建Maven私服