Nexus-在項目中使用Maven私服,Deploy到私服、上傳第三方jar包、在項目中使用私服jar包


場景

Ubuntu Server 上使用Docker Compose 部署Nexus(圖文教程):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/101111611

在上面已經實現部署Nexus后的效果是

 

 

為什么要搭建私服

有時合作開發時,為了不泄露源碼但是還能允許你調用,或者公司內部自己的依賴jar包,只能在本公司內用,並且再官方中央倉庫中沒有。類似情況下都需要搭建Maven私服。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

Deploy依賴到私服

配置認證信息

找到Maven的安裝目錄

 

 

conf下的setting.xml中找到server節點。

 

 

 

配置認證節點,因為私服不是誰都能使用,所以需要配置用戶名和密碼,這里的密碼是上面搭建Nexus服務時所設置的密碼。

<server>
        <id>nexus-releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    
    <server>
        <id>nexus-snapshots</id>
        <username>admin</username>
        <password>admin123</password>
    </server>

 

修改之后,保存。

 

 

注:

nexues-releases:用於發布Release版本

nexus-snapshots:用於發布Snapshot版本(快照版),快照版會自動加一個時間作為標識。

 

配置自動化部署

在項目的pom.xml中加入如下代碼:

<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.208.134:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.208.134:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

 

這里是使用IDEA新建的maven項目

注:

1.ID名稱要與settings.xml中Servers配置的ID保持一致。

2.項目版本號中有SNAPSHOT標識的,會發布到Nexus Snapshots Respository,否則發布到Nexus Release Repository,並根據ID去匹配授權賬號。

3.這里的url是Nexus服務上的url。

 

 

部署

打開IDEA下的Ternial,輸入:

mvn deploy

 

 

可以看到其部署效果

 

 

此時刷新Nexus服務的url,找到Browse下的maven-snapshots

 

 

部署成功。

然后打開IDEA--settings-maven,然后勾選上總是更新快照。

 

 

這樣就能用到最新的快照版本。

上傳第三方jar包

有時在官方倉庫沒有的jar包,需要上傳到私服上,供大家使用。

mvn deploy:deploy-file -DgroupId=com.google.code.kaptcha -DartifactId=kaptcha -Dversion=2.3.2 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\kaptcha-2.3.2.jar -Durl=http://192.168.208.134:8081/repository/maven-releases/ -DrepositoryId=nexus-releases

 

命令解釋:

-DgroupId=                          自定義
-DartifactId=                        自定義
-Dversion=                          自定義  三個自定義,構成pom.xml文件中的坐標
-Dpackaging=jar                       上傳的類型是jar類型
-Dfile=                                  jar的本地磁盤位置
-Durl=                                                                           hosted資源庫的地址
-DrepositoryId=nexus-releases                setting.xml文件中配置的ID
 

上傳成功效果

 

 

此時再回到瀏覽器,刷新。

 

 

 

 

在項目中使用私服jar包

配置代理倉庫

在需要從私服中下載jar包的項目的pom.xml中加入如下配置:

<repositories>
        <repository>
            <id>nexus</id>
            <name>Nexus Repository</name>
            <url>http://192.168.208.134:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <!-- 私服倉庫配置:從私服下載-->
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>Nexus Plugin Repository</name>
            <url>http://192.168.208.134:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

 

為什么是從public進行下載,

因為公共倉庫是發行倉庫和快照倉庫的映射,把兩個倉庫結合起來。

下面這段代碼

<releases>
  <enabled>true</enabled>
</releases>
<snapshots>
  <enabled>true</enabled>
</snapshots>

 

作用是配置是否依賴發行版和是否依賴快照版。

怎樣使用私服jar包。

找到要下載的jar包的坐標配置,加入到pom中,那么就會先從私服去找對應的jar包,然后再去官服去找jar包。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM