Maven私服Nexus的搭建

私服簡介

私服是架設(shè)在局域網(wǎng)的一種特殊的遠(yuǎn)程倉庫,目的是代理遠(yuǎn)程倉庫及部署第三方構(gòu)件。有了私服之后,當(dāng) Maven 需要下載構(gòu)件時,直接請求私服,私服上存在則下載到本地倉庫;否則,私服請求外部的遠(yuǎn)程倉庫,將構(gòu)件下載到私服,再提供給本地倉庫下載。

nexus

我們可以使用專門的 Maven 倉庫管理軟件來搭建私服,比如:Apache ArchivaArtifactory,Sonatype Nexus。這里我們使用 Sonatype Nexus。

Nexus是常用的私用Maven服務(wù)器,一般是公司內(nèi)部使用。

安裝Nexus

Nexus 專業(yè)版是需要付費(fèi)的,這里我們下載開源版 Nexus OSS。Nexus 提供兩種安裝包,一種是包含 Jetty 容器的 bundle 包,另一種是不包含容器的 war 包。下載地址:http://www.sonatype.org/nexus/go

[root@snails ~]# wget http://download.sonatype.com/nexus/3/latest-unix.tar.gz
[root@snails ~]# tar -zxvf nexus-{version}-unix.tar.gz -C /data/
[root@snails ~]# cd /data/nexus/bin
[root@snails bin]# nexus
Usage: /data/nexus/bin/nexus { console | start | stop | restart | status | dump }
[root@snails bin]# nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Removed stale pid file: /data/nexus-{version}/bin/../bin/jsw/linux-x86-64/nexus.pid
Started Nexus OSS.

Tips:{version}是實際版本號的替代位。

本地訪問驗證

打開瀏覽器,訪問:http://ip:port/nexus/

nexus panel

Nexus預(yù)置的倉庫

nexus repositories

Nexus 的倉庫分為這么幾類:

  • hosted 宿主倉庫:主要用于部署無法從公共倉庫獲取的構(gòu)件(如 oracle 的 JDBC 驅(qū)動)以及自己或第三方的項目構(gòu)件;
  • proxy 代理倉庫:代理公共的遠(yuǎn)程倉庫;
  • virtual 虛擬倉庫:用于適配 Maven 1版本;
  • group 倉庫組:Nexus 通過倉庫組的概念統(tǒng)一管理多個倉庫,這樣我們在項目中直接請求倉庫組即可請求到倉庫組管理的多個倉庫。


    nexus repository type

添加代理倉庫

以 Sonatype 為例,添加一個代理倉庫,用于代理 Sonatype 的公共遠(yuǎn)程倉庫。點擊菜單 Add - Proxy Repository :


  填寫Repository ID - sonatype;Repository Name - Sonatype Repository;Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 保存:

將添加的 Sonatype 代理倉庫加入 Public Repositories 倉庫組。選中 Public Repositories,在 Configuration 選項卡中,將 Sonatype Repository 從右側(cè) Available Repositories 移到左側(cè) Ordered Group Repositories,save 保存:

搜索構(gòu)件

為了更好的使用 Nexus 的搜索,我們可以設(shè)置所有 proxy 倉庫的 Download Remote Indexes 為 true,即允許下載遠(yuǎn)程倉庫索引。


  索引下載成功之后,在 Browse Index 選項卡下,可以瀏覽到所有已被索引的構(gòu)件信息,包括坐標(biāo)、格式、Maven 依賴的 xml 代碼:

  有了索引,我們就可以搜索了:

配置Maven使用私服

私服搭建成功,我們就可以配置 Maven 使用私服,以后下載構(gòu)件、部署構(gòu)件,都通過私服來管理。
  在 settings.xml 文件中,為所有倉庫配置一個鏡像倉庫,鏡像倉庫的地址即私服的地址(這兒我們使用私服公共倉庫組 Public Repositories 的地址):

   <servers>
     <server>
         <id>releases</id>
         <username>admin</username>
         <password>admin321</password>
     </server>
     <server>
         <id>snapshots</id>
         <username>admin</username>
         <password>admin321</password>
     </server>
   </servers>
 
   <mirrors>
     <mirror>
       <id>nexus</id>
       <mirrorOf>*</mirrorOf>
       <url>http://{ip:port}/nexus/content/groups/public</url>
     </mirror>
   </mirrors>

Tips:{ip:port}是實際ip和port的替代位。settings.xml主要是用來配置私服的下載地址、上傳用的帳號和密碼以及activeProfile選項。

項目發(fā)布

在pom.xml文件中定義私服信息,主要是上傳用到的repository,一般會包含releases和snapshots。

     <!-- 公司Maven私服Nexus地址用于下載 -->
     <repositories>
         <repository>
             <id>releases</id>
             <name>Internal Releases</name>
             <url>http://{ip:port}/nexus/content/repositories/releases</url>
         </repository>
         <repository>
             <id>Snapshots</id>
             <name>Internal Snapshots</name>
             <url>http://{ip:port}/nexus/content/repositories/snapshots</url>
         </repository>
     </repositories>
     <!-- 公司Maven私服Nexus地址用于發(fā)布 -->
     <distributionManagement>
         <repository>
             <id>releases</id>
             <url>http://{ip:port}/nexus/content/repositories/releases</url>
         </repository>
         <snapshotRepository>
             <id>snapshots</id>
             <url>http://{ip:port}/nexus/content/repositories/snapshots</url>
         </snapshotRepository>
     </distributionManagement>

發(fā)布jar時依賴于settings.xml配置賬號密碼。注意server的id與repository的id必須對應(yīng)。如果不在項目中發(fā)布,僅發(fā)布jar文件,可登錄nexus進(jìn)行Artifact upload。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • |-1-更新內(nèi)容[6.從倉庫解析依賴的機(jī)制(重要)] 1Maven倉庫作用 倉庫用來存儲所有項目使用到構(gòu)件,在ma...
    zlcook閱讀 6,472評論 0 25
  • 首先私服是一種衍生出來的特殊的Maven遠(yuǎn)程倉庫,構(gòu)建私服的好處請看3.5私服 可以幫助大家建立私服的倉庫管理軟件...
    zlcook閱讀 10,838評論 0 32
  • Nexus是Maven倉庫管理器,管理開發(fā)所需要的構(gòu)件。如果你每次都是從Apache提供的Maven中央倉庫去下載...
    點融黑幫閱讀 3,928評論 0 6
  • 介紹 私服的優(yōu)點:解決中央倉庫網(wǎng)絡(luò)、重復(fù)下載、本公司非公開組件多項目依賴等問題。 在團(tuán)隊協(xié)作開發(fā)中,為了提高開發(fā)效...
    madfrog_hc閱讀 9,020評論 0 7
  • 我本將心向明月,奈何明月照溝渠。 白素是一個再普通不過的女子。溫婉而本分的默默做著一個女子該做的事。聽了家里的話而...
    金面笑飛俠閱讀 380評論 0 1

友情鏈接更多精彩內(nèi)容