[Maven專題-03] -Maven私服構(gòu)建

私服概述

  • 私服是一種特殊的遠(yuǎn)程倉庫,它一般假設(shè)在公司局域網(wǎng)內(nèi)。當(dāng)Maven下載構(gòu)件時(shí)從私服上取,如果不存在則私服從遠(yuǎn)程倉庫中取緩存在私服上,方便后續(xù)人員取用。它的優(yōu)點(diǎn)如下:
    • 節(jié)省外網(wǎng)寬帶,構(gòu)件高效。
    • 有利于在中央倉庫不存在的第三方構(gòu)件和自己生成的構(gòu)件。
    • 加速構(gòu)建過程,提供穩(wěn)定性以及降低中央倉庫的負(fù)荷等。

私服搭建

  • 選擇Ubuntu 18.04平臺(tái),openjdk-8以及使用Nexus OSS 3.13.0版本庫管理工具, 搭建過程如下:

    #1 下載nexus-3.13.0-01-unix.tar.gz文件至家目錄下 官網(wǎng)下載速度奇慢,使用Baidu網(wǎng)盤下載
    #2 解壓縮至/opt/目錄下,
    yjf@v-box:~$ sudo tar -zxvf nexus-3.13.0-01-unix.tar.gz -C /opt/
    #3 進(jìn)入/opt/nexus-3.13.0-01/bin目錄,執(zhí)行以下命令運(yùn)行nexus
    yjf@v-box:/opt/nexus-3.12.0-01/bin$ sudo ./nexus start 
    #4 nexus的常用命令
    #4.1 start  開啟 
    #4.2 stop   停止
    #4.3 status 狀態(tài)
    
  • 成功啟動(dòng)后,使用http://192.168.0.210:8081/#browse/welcome進(jìn)行訪問,進(jìn)行配置需要登錄,默認(rèn)的用戶名和密碼為admin:admin123如下圖:

  • Nexus倉庫的分類:

    • Nexus宿主倉庫,主要用于管理第三方構(gòu)件,分為RELEASE和SNAPSHOT版本。
    • 代理倉庫,主要代理遠(yuǎn)程中央倉庫,在本私服上不存在指定構(gòu)件時(shí)從中央倉庫上直接下載。
    • 倉庫組:可以聚合以上兩種倉庫形成列表,作為倉庫供maven配置下載,倉庫便利的順序按配置從上到下遍歷。
  • Nexus宿主倉庫建立

    • 點(diǎn)擊上圖的配置按鈕,選擇Repositories連接,并點(diǎn)擊Create Repository,在彈出的選擇列表中選擇maven2(hosted)即可進(jìn)行創(chuàng)建,配置如下:

  • Nexus代理倉庫建立

    • 步驟同上,在點(diǎn)擊Create Repository,在彈出的選擇列表中選擇maven2(proxy)即可進(jìn)行創(chuàng)建,配置如下:

  • Nexus 倉庫組建立

    • 步驟同上,在點(diǎn)擊Create Repository,在彈出的選擇列表中選擇maven2(group)即可進(jìn)行創(chuàng)建,配置如下:

Maven配置私服

  • 拉取構(gòu)件,主要分在項(xiàng)目中配置和setting.xml中配置兩種方式。

    <!-- 項(xiàng)目中配置,只針對此項(xiàng)目有效 
      1 未配置任何倉庫的情況下,由于超級POM的作用,默認(rèn)配置中央倉庫。
      2 在項(xiàng)目中覆蓋配置中央倉庫,配置如下:  該方式配置與超級POM中的中央倉庫合并 首先在該倉庫中找 找不到則在中央倉庫中找。
    -->
    <repositories>
        <repository>
          <id>jboss</id>
            <name>JBoss Repository</name>
            <url>http://192.168.0.210:8081/repository/jboss-central/</url>   
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
            <layout>default</layout>
        </repository>
    </repositories>
    
    <!-- 
      3 在setting.xml中配置profile, 覆蓋超級POM中的中央倉庫和插件倉庫的配置
    -->
    <profiles>
      <profile>
            <id>nexusProfile</id>
            <repositories>
                <repository>
                  <id>central</id>
                    <name>Central Repository</name>
                    <url>http://192.168.0.210:8081/repository/maven-public/</url>   
                    <snapshots><enabled>false</enabled></snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                  <id>central</id>
                    <name>Central Repository</name>
                    <url>http://192.168.0.210:8081/repository/maven-public/</url>   
                    <snapshots><enabled>false</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
      <activeProfile>nexusProfile</activeProfile>
    </activeProfiles>
    
    
  • 推送構(gòu)件到私服倉庫,需要配置如下:

    <!-- setting.xml 配置如下:-->
    <servers>
        <server>
          <id>nexus-server</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>
    <distributionManagement>
        <snapshotRepository>
            <id>nexus-server</id>
            <name>snapshots-hosted</name>
            <url>http://192.168.0.210:8081/repository/snapshot-hosted/</url>
        </snapshotRepository>
        <!-- 推送正式版本時(shí)請務(wù)必在pom.xml文件中版本號中刪除SNAPSHOT字樣 否則產(chǎn)生400錯(cuò)誤 -->
        <repository>
            <id>nexus-server</id>
            <name>release-hosted</name>
            <url>http://192.168.0.210:8081/repository/release-hosted/</url>
        </repository>
    </distributionManagement>
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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