Sonatype Nexus 搭建Maven 私服

一、 爲何使用Nexushtml

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

二、Nexus下載apache

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

三、Nexus啓動網絡

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

(或者:1.將」F:\nexus-2.8.1-01-bundle\nexus-2.8.1-01\bin「放到環境變量的path中。就直接可用用nexus命令了。maven

             2.修改「nexus-2.8.1-01-bundle\nexus-2.8.1-01\bin\jsw\conf\wrapper.conf」中的「wrapper.java.command=E:\Program Files\Java\jdk1.7.0_65\bin\java」post

             3.cmd窗口:nexus install進行安裝,就能夠在服務管理器中看到並管理。)測試

圖(1)url

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

圖(2)

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

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

圖(3)


 

四、Nexus倉庫

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

               group: 倉庫組

               hosted:宿主

              proxy:代理

              virtual:虛擬

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


圖(4)

                       Public Repositories:  倉庫組

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

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

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

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

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

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

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

五、配置nexus

5.一、開啓遠程索引

新搭建的neuxs環境只是一個空的倉庫,須要手動和遠程中心庫進行同步,nexus默認是關閉遠程索引下載,最重要的一件事情就是開啓遠程索引下載。登錄nexus系統,默認用戶名密碼爲admin/admin123。

點擊左邊Administration菜單下面的Repositories,找到右邊倉庫列表中的三個倉庫Apache Snapshots,Codehaus Snapshots和Maven Central,而後再沒有倉庫的configuration下把Download Remote Indexes修改成true。以下圖

 

 

而後在Apache Snapshots,Codehaus Snapshots和Maven Central這三個倉庫上分別右鍵,選擇Repari Index,這樣Nexus就會去下載遠程的索引文件。

 

 

 

 

這樣設置之後, Nexus會自動從遠程中央倉庫下載索引文件, 爲了檢驗索引文件自動下載是否生效,能夠卻換到Browse Index

 

 

在左邊菜單欄裏面有個Artifact Search, 在輸入框裏面輸入你想要搜索的構件名字,好比:maven, 那麼查詢結果以下:

 

 

5.2創建宿主倉庫

新 建公司的內部倉庫,步驟爲Repositories –> Add –> Hosted Repository,在頁面的下半部分輸入框中填入Repository ID和Repository Name便可,好比分別填入

zfy 和 zfy repostiory,另外把Deployment Policy設置爲Allow Redeploy,點擊save就建立完成了。

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

圖(6)

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

 

 

5.三、建立Nexus倉庫組

Nexus 中倉庫組的概念是Maven沒有的,在Maven看來,無論你是hosted也好,proxy也好,或者group也好,對我都是同樣的,我只管根據 groupId,artifactId,version等信息向你要構件。爲了方便Maven的配置,Nexus可以將多個倉庫,hosted或者 proxy合併成一個group,這樣,Maven只須要依賴於一個group,便能使用全部該group包含的倉庫的內容。

neuxs- 1.9.2.3中默認自帶了一個名爲「Public Repositories」組,點擊該組能夠對他保護的倉庫進行調整,把剛纔創建的公司內部倉庫zfy repostiory加入其中,這樣就不須要再在maven中明確指定內部倉庫 的地址了。同時建立一個Group ID爲public-snapshots、Group Name爲Public Snapshots Repositories的組,把Apache Snapshots、Codehaus Snapshots、Snapshots和zfy repostiory加入其中。

 

 

 

 

 

 

 

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

 

 

 

5.四、建立Nexus代理倉庫

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

圖(7)


      以上本人分別建立 了  hosted和group ,沒有建立proxy,經maven測試經過。

六、      settings.xml的配置

<?xml version="1.0" encoding="UTF-8"?>  
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  
  <pluginGroups></pluginGroups>  
  <proxies></proxies>  
  
  <servers>  
      <server>  
      <id>nexus-releases</id>  
      <username>admin</username>  
      <password>admin123</password>  
    </server>  
    <server>  
      <id>nexus-snapshots</id>  
      <username>admin</username>  
      <password>admin123</password>  
    </server>  
  </servers>  
  
  <mirrors>   
    <mirror>   
      <id>nexus-releases</id>   
      <mirrorOf>*</mirrorOf>   
      <url>http://localhost:8081/nexus/content/groups/public</url>   
    </mirror>  
    <mirror>   
      <id>nexus-snapshots</id>   
      <mirrorOf>*</mirrorOf>   
      <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>   
    </mirror>   
  </mirrors>   
   
  <profiles>  
   <profile>  
      <id>nexus</id>  
      <repositories>  
        <repository>  
          <id>nexus-releases</id>  
          <url>http://nexus-releases</url>  
          <releases><enabled>true</enabled></releases>  
          <snapshots><enabled>true</enabled></snapshots>  
        </repository>  
        <repository>  
          <id>nexus-snapshots</id>  
          <url>http://nexus-snapshots</url>  
          <releases><enabled>true</enabled></releases>  
          <snapshots><enabled>true</enabled></snapshots>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
         <pluginRepository>  
                <id>nexus-releases</id>  
                 <url>http://nexus-releases</url>  
                 <releases><enabled>true</enabled></releases>  
                 <snapshots><enabled>true</enabled></snapshots>  
               </pluginRepository>  
               <pluginRepository>  
                 <id>nexus-snapshots</id>  
                  <url>http://nexus-snapshots</url>  
                <releases><enabled>true</enabled></releases>  
                 <snapshots><enabled>true</enabled></snapshots>  
             </pluginRepository>  
         </pluginRepositories>  
    </profile>  
  </profiles>  
  
  <activeProfiles>  
      <activeProfile>nexus</activeProfile>  
  </activeProfiles>  
   
</settings>

七、 maven 項目 的pom.xml配置 

<pre class="html" name="code"> <distributionManagement>  
        <!-- 兩個ID必須與 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->  
        <repository>  
            <id>nexus-releases</id>  
            <name>Nexus Release Repository</name>  
            <url>http://localhost:8081/nexus/content/repositories/releases</url>  
        </repository>  
        <snapshotRepository>  
            <id>nexus-snapshots</id>  
            <name>Nexus Snapshot Repository</name>  
            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>  
        </snapshotRepository>  
    </distributionManagement></pre><pre class="html" name="code"> </pre><pre class="html" name="code">八、mvn:deploy命令上傳包,會發現下載路徑必不是中央庫的路徑,而是nexus裏面的倉庫組路徑。這個說明配置環境成功。</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>  
<pre></pre>  
<pre></pre>
相關文章
相關標籤/搜索