第 03 章 平臺即服務(3.2)

什麼是 Nexus

Nexus 是一個強大的倉庫管理器,極大地簡化了內部倉庫的維護和外部倉庫的訪問。

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

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

基於 Docker 安裝 Nexus

李衛民 發表於 2018-05-22
我們使用 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

注: 啓動時如果出現權限問題可以使用:chmod 777 /usr/local/docker/nexus/data 賦予數據卷目錄可讀可寫的權限

登錄控制檯驗證安裝

地址:http://ip:port/
用戶名:admin
密碼:admin123
這裏寫圖片描述

Maven 倉庫介紹

代理倉庫(Proxy Repository)

意爲第三方倉庫,如:

maven-central
nuget.org-proxy
版本策略(Version Policy):

Release: 正式版本
Snapshot: 快照版本
Mixed: 混合模式
佈局策略(Layout Policy):

Strict:嚴格
Permissive:寬鬆
宿主倉庫(Hosted Repository)

存儲本地上傳的組件和資源的,如:

maven-releases
maven-snapshots
nuget-hosted
部署策略(Deployment Policy):

Allow Redeploy:允許重新部署
Disable Redeploy:禁止重新部署
Read-Only:只讀
倉庫組(Repository Group)

通常包含了多個代理倉庫和宿主倉庫,在項目中只要引入倉庫組就可以下載到代理倉庫和宿主倉庫中的包,如:

maven-public
nuget-group

在項目中使用 Maven 私服

配置認證信息

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

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</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
配置自動化部署

在 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 去匹配授權賬號。
部署到倉庫

mvn deploy
上傳第三方 JAR 包

Nexus 3.0 不支持頁面上傳,可使用 maven 命令:

如第三方JAR包:aliyun-sdk-oss-2.2.3.jar

mvn deploy:deploy-file
-DgroupId=com.aliyun.oss
-DartifactId=aliyun-sdk-oss
-Dversion=2.2.3
-Dpackaging=jar
-Dfile=D:\aliyun-sdk-oss-2.2.3.jar
-Durl=http://127.0.0.1:8081/repository/maven-3rd/
-DrepositoryId=nexus-releases
注意事項:

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

<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>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章