1. 程式人生 > >maven私服搭建nexus

maven私服搭建nexus

                                   maven私服

正式開發,不同的專案組開發不同的工程。dao工程開發完畢,釋出到私服。service從私服下載dao。

         公司在自己的區域網內搭建自己的遠端倉庫伺服器,稱為私服,私服伺服器即是公司內部的maven遠端倉庫,每個員工的電腦上安裝maven軟體並且連線私服伺服器,員工將自己開發的專案打成jar併發布到私服伺服器,其它專案組從私服伺服器下載所依賴的構件(jar)。       私服還充當一個代理伺服器,當私服上沒有jar包會從網際網路中央倉庫自動下載,如下圖:

4.1搭建私服環境

4.1.1下載nexus

 

         Nexus 是Maven倉庫管理器,通過nexus可以搭建maven倉庫,同時nexus還提供強大的倉庫管理功能,構件搜尋功能等。

         下載Nexus, 下載地址:http://www.sonatype.org/nexus/archived/

4.1.2安裝nexus

解壓nexus-2.12.0-01-bundle.zip,本教程將它解壓在F盤,進入bin目錄:

cmd進入bin目錄,執行nexus.bat install

安裝成功在服務中檢視有nexus服務:

4.1.3解除安裝nexus

cmd進入nexus的bin目錄,執行:nexus.bat uninstall

檢視window服務列表nexus已被刪除。

4.1.4啟動nexus

方法1:

         cmd進入bin目錄,執行nexus.bat start

方法2:

         直接啟動nexus服務

        

檢視nexus的配置檔案conf/nexus.properties

# Jetty section

application-port=8081            # nexus的訪問埠配置

application-host=0.0.0.0         # nexus主機監聽配置(不用修改)

nexus-webapp=${bundleBasedir}/nexus        # nexus工程目錄

nexus-webapp-context-path=/nexus       # nexus的web訪問路徑

# Nexus section

nexus-work=${bundleBasedir}/../sonatype-work/nexus   # nexus倉庫目錄

runtime=${bundleBasedir}/nexus/WEB-INF  # nexus執行程式目錄

訪問:

http://localhost:8081/nexus/

使用Nexus 內建賬戶admin/admin123登陸:

點選右上角的Log in,輸入賬號和密碼 登陸

登陸成功:

4.1.5倉庫型別

nexus

檢視nexus的倉庫:

nexus的倉庫有4種類型:

  1. hosted,宿主倉庫,部署自己的jar到這個型別的倉庫,包括releases和snapshot兩部分,Releases公司內部發布版本倉庫、 Snapshots 公司內部測試版本倉庫

 

  1. proxy,代理倉庫,用於代理遠端的公共倉庫,如maven中央倉庫,使用者連線私服,私服自動去中央倉庫下載jar包或者外掛。

 

  1. group,倉庫組,用來合併多個hosted/proxy倉庫,通常我們配置自己的maven連線倉庫組。
  2. virtual(虛擬):相容Maven1 版本的jar或者外掛

nexus倉庫預設在sonatype-work目錄中:

 

  • central:代理倉庫,代理中央倉庫

 

  • apache-snapshots:代理倉庫

儲存snapshots構件,代理地址https://repository.apache.org/snapshots/

  • central-m1virtual型別倉庫,相容Maven1 版本的jar或者外掛
  • releases:本地倉庫,儲存releases構件。
  • snapshots:本地倉庫,儲存snapshots構件。
  • thirdparty:第三方倉庫
  • public:倉庫組

4.2將專案釋出到私服

4.2.1需求

         企業中多個團隊協作開發通常會將一些公用的元件、開發模組等釋出到私服供其它團隊或模組開發人員使用。

         本例子假設多團隊分別開發dao、service、web,某個團隊開發完在dao會將dao釋出到私服供service團隊使用,本例子會將dao工程打成jar包釋出到私服。

 

4.2.2配置

第一步: 需要在客戶端即部署dao工程的電腦上配置 maven環境,並修改 settings.xml 檔案,配置連線私服的使用者和密碼 。

此使用者名稱和密碼用於私服校驗,因為私服需要知道上傳都 的賬號和密碼 是否和私服中的賬號和密碼 一致。

    <server>

      <id>releases</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

         <server>

      <id>snapshots</id>

      <username>admin</username>

      <password>admin123</password>

    </server>

releases 連線釋出版本專案倉庫

 

snapshots 連線測試版本專案倉庫

第二步: 配置專案pom.xml

配置私服倉庫的地址,本公司的自己的jar包會上傳到私服的宿主倉庫,根據工程的版本號決定上傳到哪個宿主倉庫,如果版本為release則上傳到私服的release倉庫,如果版本為snapshot則上傳到私服的snapshot倉庫

  <distributionManagement>

    <repository>

        <id>releases</id>

    <url>http://localhost:8081/nexus/content/repositories/releases/</url>

    </repository>

    <snapshotRepository>

        <id>snapshots</id>

    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>

    </snapshotRepository>

  </distributionManagement>

注意:pom.xml這裡<id> 和 settings.xml 配置 <id> 對應!

 

4.2.3測試

將專案dao工程打成jar包釋出到私服:

1、首先啟動nexus

2、對dao工程執行deploy命令

根據本專案pom.xml中version定義決定釋出到哪個倉庫,如果version定義為snapshot,執行deploy後檢視nexus的snapshot倉庫,如果version定義為release則專案將釋出到nexus的release倉庫,本專案將釋出到snapshot倉庫:

也可以通過http方式檢視:

4.3從私服下載jar包

4.3.1需求

         沒有配置nexus之前,如果本地倉庫沒有,去中央倉庫下載,通常在企業中會在區域網內部署一臺私服伺服器,有了私服本地專案首先去本地倉庫找jar,如果沒有找到則連線私服從私服下載jar包,如果私服沒有jar包私服同時作為代理伺服器從中央倉庫下載jar包,這樣做的好處是一方面由私服對公司專案的依賴jar包統一管理,一方面提高下載速度,專案連線私服下載jar包的速度要比專案連線中央倉庫的速度快的多。

4.3.2管理倉庫組

         nexus中包括很多倉庫,hosted中存放的是企業自己釋出的jar包及第三方公司的jar包,proxy中存放的是中央倉庫的jar,為了方便從私服下載jar包可以將多個倉庫組成一個倉庫組,每個工程需要連線私服的倉庫組下載jar包。

開啟nexus配置倉庫組,如下圖:

上圖中倉庫組包括了本地倉庫、代理倉庫等。

4.3.3在setting.xml中配置倉庫

         在客戶端的setting.xml中配置私服的倉庫,由於setting.xml中沒有repositories的配置標籤需要使用profile定義倉庫。

<profile>  

         <!--profile的id-->

   <id>dev</id>  

    <repositories>  

      <repository> 

                   <!--倉庫id,repositories可以配置多個倉庫,保證id不重複-->

        <id>nexus</id>  

                   <!--倉庫地址,即nexus倉庫組的地址-->

        <url>http://localhost:8081/nexus/content/groups/public/</url>  

                   <!--是否下載releases構件-->

        <releases>  

          <enabled>true</enabled>  

        </releases>  

                   <!--是否下載snapshots構件-->

        <snapshots>  

          <enabled>true</enabled>  

        </snapshots>  

      </repository>  

    </repositories> 

          <pluginRepositories> 

    <!-- 外掛倉庫,maven的執行依賴外掛,也需要從私服下載外掛 -->

        <pluginRepository> 

           <!-- 外掛倉庫的id不允許重複,如果重複後邊配置會覆蓋前邊 -->

            <id>public</id> 

            <name>Public Repositories</name> 

            <url>http://localhost:8081/nexus/content/groups/public/</url> 

        </pluginRepository> 

    </pluginRepositories> 

  </profile>

使用profile定義倉庫需要啟用才可生效。

  <activeProfiles>

    <activeProfile>dev</activeProfile>

  </activeProfiles>

配置成功後通過eclipse檢視有效pom,有效pom是maven軟體最終使用的pom內容,程式設計師不直接編輯有效pom,開啟有效pom

有效pom內容如下:

下邊的pom內容中有兩個倉庫地址,maven會先從前邊的倉庫的找,如果找不到jar包再從下邊的找,從而就實現了從私服下載jar包。

<repositories>

    <repository>

      <releases>

        <enabled>true</enabled>

      </releases>

      <snapshots>

        <enabled>true</enabled>

      </snapshots>

      <id>public</id>

      <name>Public Repositories</name>

      <url>http://localhost:8081/nexus/content/groups/public/</url>

    </repository>

    <repository>

      <snapshots>

        <enabled>false</enabled>

      </snapshots>

      <id>central</id>

      <name>Central Repository</name>

      <url>https://repo.maven.apache.org/maven2</url>

    </repository>

  </repositories>

  <pluginRepositories>

    <pluginRepository>

      <id>public</id>

      <name>Public Repositories</name>

      <url>http://localhost:8081/nexus/content/groups/public/</url>

    </pluginRepository>

    <pluginRepository>

      <releases>

        <updatePolicy>never</updatePolicy>

      </releases>

      <snapshots>

        <enabled>false</enabled>

      </snapshots>

      <id>central</id>

      <name>Central Repository</name>

      <url>https://repo.maven.apache.org/maven2</url>

    </pluginRepository>

  </pluginRepositories>