Nexus搭建Maven私服(docker)


title: Nexus搭建Maven私服
date: 2020-03-22 10:29:51
categories: 微服務基礎設施


Nexus搭建Maven私服

安裝

docker pull sonatype/nexus3

運行

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 賦予數據卷目錄可讀可寫的權限
  • 默認密碼別驚訝,就是那麼大一串per

異常情況處理*

*UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.HttpHostConnectException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/69.171.245.49] failed: 連接超時
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151) [httpcore:0.0.0]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpcore:0.0.0]

處理方法:登錄賬號,打開【System】–》【Capabilities】,將【Outreach:Management】禁用即可。

在項目中使用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://192.168.1.3:8091/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.1.3:8091/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

注意事項:

  • ID 名稱必須要與 settings.xml 中 Servers 配置的 ID 名稱保持一致。
  • 項目版本號中有 SNAPSHOT 標識的,會發布到 Nexus Snapshots Repository, 否則發佈到 Nexus Release Repository,並根據 ID 去匹配授權賬號。
  • 部署到倉庫
mvn deploy -Dmaven.test.skip=true

上傳第三方 JAR 包

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

# 如第三方JAR包:kaptcha-2.3.jar
mvn deploy:deploy-file 
  -DgroupId=com.google.code.kaptcha 
  -DartifactId=kaptcha
  -Dversion=2.3 
  -Dpackaging=jar 
  -Dfile=C:\Users\swing\Desktop\kaptcha-2.3.jar
  -Durl=http://172.18.48.62:8081/repository/maven-releases/
  -DrepositoryId=nexus-releases

注意事項:

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

配置代理倉庫

<!--從私服下載-->
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Repository</name>
            <url>http://172.18.48.62:8081/repository/maven-releases/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Plugin Repository</name>
            <url>http://172.18.48.62:8081/repository/maven-snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章