微服務----Nexus


Nexus簡介

Nexus 是一個強大的倉庫管理器,極大地簡化了內部倉庫的維護和外部倉庫的訪問。取代maven倉庫,可以搭建在局域網中

2016 年 4 月 6 日 Nexus 3.0 版本發布,相較 2.x 版本有了很大的改變:

  • 對低層代碼進行了大規模重構,提升性能,增加可擴展性以及改善用戶體驗。
  • 升級界面,極大的簡化了用戶界面的操作和管理。
  • 提供新的安裝包,讓部署更加簡單。
  • 增加對 Docker, NeGet, npm, Bower 的支持。
  • 提供新的管理接口,以及增強對自動任務的管理。

 

我們平常訪問的:https://mvnrepository.com/ 是maven公服,而Nexus是maven廠庫管理的私服

 

使用

Nexus3:https://hub.docker.com/r/sonatype/nexus3 

docker pull sonatype/nexus3

我們使用 Docker 來安裝和運行 Nexus,docker-compose.yml 配置如下:

version: '3.1'
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
      - 8081:8081
    volumes:
      - /usr/local/docker/nexus/data:/nexus-data   //數據卷用來管理images

啟動Nexus

docker-compose up

啟動后,會出現一個data的目錄,但是可能啟動后會直接退出。

docker-compose down   //關閉Nexus

給data目錄加上權限:chmod 777 /usr/local/docker/nexus/data 賦予數據卷目錄可讀可寫的權限,重新啟動

docker-compose up

 啟動成功后,如果需要登錄,登錄的初始用戶名:admin,密碼:admin123

 

在項目中使用 Maven 私服

1.配置認證信息

在 Maven settings.xml 中添加 Nexus 認證信息(servers 節點下):

注意IDEA自帶有maven。

<server>
  <id>nexus-releases</id>              //發行版倉庫
  <username>admin</username>           //私服的用戶名
  <password>admin123</password>        //私服的密碼(Nexus默認密碼)
</server>

<server>
  <id>nexus-snapshots</id>             //快照版倉庫
  <username>admin</username>
  <password>admin123</password>
</server> 

Snapshots 與 Releases 的區別

  • nexus-releases: 用於發布 Release 版本
  • nexus-snapshots: 用於發布 Snapshot 版本(快照版)

Release 版本與 Snapshot 定義如下:

Release: 1.0.0/1.0.0-RELEASE
Snapshot: 1.0.0-SNAPSHOT
  • 在項目 pom.xml 中設置的版本號添加 SNAPSHOT 標識的都會發布為 SNAPSHOT 版本,沒有 SNAPSHOT 標識的都會發布為 RELEASE 版本。

  • SNAPSHOT 版本會自動加一個時間作為標識,如:1.0.0-SNAPSHOT 發布后為變成 1.0.0-SNAPSHOT-20180522.123456-1.jar,開發過程中使用快照版,是最好的,可以隨時更改代碼,並重新上傳到Nexus。而發行版不能更改。

2.配置自動化部署

在 pom.xml 中添加如下代碼:

<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
    <name>Nexus Release Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>//地址獲取看下圖
  </snapshotRepository>  
</distributionManagement> 

注意事項:

  • ID 名稱必須要與 settings.xml 中 Servers 配置的 ID 名稱保持一致。
  • 項目版本號中有 SNAPSHOT 標識的,會發布到 Nexus Snapshots Repository, 否則發布到 Nexus Release Repository,並根據 ID 去匹配授權賬號。

 

3.部署到倉庫

mvn deploy 

 

成功后登錄,如果出現這個代表成功

 

4.配置代理倉庫(讓maven可以通過私服下載jar包,需要配置倉庫地址)

pom.xml

<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>  //配置公共倉庫地址,公共倉庫包含了發行版倉庫和快照版倉庫的所有的jar包
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

  

 

擴展使用

4.上傳 官服 中沒有 JAR 包

使用 maven 命令上傳第三方jar包:

mvn deploy:deploy-file
  -DgroupId=com.google.code.kaptcha
  -DartifactId=kaptcha
  -Dversion=2.3
  -Dpackaging=jar
  -Dfile=F:\谷歌瀏覽器下載\kaptcha-2.3.jar
  -Durl=http://192.168.2.104:8081/repository/maven-releases/   //可以自己創建一個專門存放第三名jar包的地址
  -DrepositoryId=nexus-releases                                //名字和之前配置認證信息中的id一樣,用來登錄用的

注意事項:

  • 建議在上傳第三方 JAR 包時,創建單獨的第三方 JAR 包管理倉庫,便於管理有維護。(maven-3rd)
  • -DrepositoryId=nexus-releases 對應的是 settings.xml 中 Servers 配置的 ID 名稱。(授權)

 

  

配置了私服,下載依賴流程

修改maven配置,方便下載的快照總是最新的

 


免責聲明!

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



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