Maven學習4: 私服

1. 私服

第3篇說明了倉庫的分類,其中講到了私服,私服的好處很明顯,這篇繼續跟着公衆號學習私服的搭建、倉庫管理、構件部署等知識。web

1. 倉庫管理軟件

  1. Sonatype Nexus:spring

  2. JFrog Artifactory: https://jfrog.com/artifactory/

2. 搭建Nexus服務

這裏學習nexus的服務搭建,JFrog的後續再寫。vim

1. 環境

  • Centos:7.6
  • jdk:1.8u231
  • maven:3.6.3

2.搭建

  1. 下載,下載安裝包到/opt/nexus目錄緩存

    [root@john nexus]# pwd
    /opt/nexus
    [root@john nexus]# ll
    total 132256
    -rw-r--r-- 1 root root 135426386 Nov 14 00:31 latest-unix.tar.gz
  2. 解壓服務器

    [root@john nexus]# tar -zxvf latest-unix.tar.gz
    [root@john nexus]# ll
    total 132264
    -rw-r--r-- 1 root root 135426386 Nov 14 00:31 latest-unix.tar.gz
    drwxr-xr-x 9 root root      4096 Dec 15 15:50 nexus-3.19.1-01
    drwxr-xr-x 3 root root      4096 Dec 15 15:50 sonatype-work
  3. 啓動服務maven

    [root@john /]# cd /opt/nexus/nexus-3.19.1-01/bin/
    [root@john bin]# ./nexus start
  4. 開放端口tcp

    • 編輯iptables文件學習

      [root@john bin]# vim /etc/sysconfig/iptables
    • 打開nexus服務默認端口8081ui

      # 添加一行內容
      -A INPUT -p tcp -m state --state NEW -m tcp --dport 8081 -j ACCEPT
    • 重啓防火牆阿里雲

      [root@john bin]# systemctl restart iptables.service
  5. 訪問nexus服務

    • 地址:http://nexus服務器ip:8081
    • 登陸:

      • user: admin
      • password: 初始密碼存放在/opt/nexus/sonatype-work/nexus3/admin.password文件中。
    • 修改密碼

3. Nexus倉庫分類

3.1 介紹

登陸nexus後,發現nexus內置了三種類型的倉庫。

  • 代理倉庫(proxy)
  • 宿主倉庫(hosted)
  • 倉庫組(group)
    image.png

3.2 代理倉庫(proxy)

  1. 介紹
    maven從代理倉庫下載構件,代理倉庫會間接地從遠程倉庫下載並緩存構件。
  2. 建立代理倉庫,代理aliyun倉庫。

    • 切換頂部菜單到server administration and configuration (設置頁面)。
    • 選擇Repository > Repositories,點擊Create repository。
    • 選擇maven2(proxy)
    • 填寫Name: maven-aliyun
    • 選擇Maven2 > Version Policy:表示該倉庫爲發佈版本(Release)倉庫仍是快照(Snapshot)版本倉庫,這裏選擇Release。
    • 填寫Proxy > Remote storage :阿里雲倉庫地址(https://maven.aliyun.com/repository/public,該地址是個倉庫組,具體可查看https://maven.aliyun.com/mvn/view)
    • 點擊底部的Create repository,完成代理倉庫的建立。

3.3 宿主倉庫

  1. 介紹
    宿主倉庫主要是儲存組織內部的,或者一些沒法從公共倉庫得到的第三方構件,供你們下載使用。
  2. 建立宿主倉庫

    • 選擇Repository > Repositories,點擊Create repository。
    • 選擇maven2(hosted)
    • 填寫Name: maven-test-releases
    • 選擇Maven2 > Version Policy:這裏選擇Relase,代表該倉庫爲發佈版本倉庫。
    • 點擊底部的Create repository,完成代理倉庫的建立。
  3. 部署構件:

    • 手動部署:將第三方構件經過nexus中網頁的方式上傳到宿主倉庫。
    • maven部署:將項目構件部署到指定的nexus的宿主倉庫.

3.4 倉庫組(group)

  1. 什麼是倉庫組?

    • 倉庫組對多個倉庫地址進行聚合,自己不包含實際內容,經過轉向組內包含的宿主倉庫或者代理倉庫得到實際構件的內容。
  2. 爲何使用倉庫組?

    • 經過訪問一個倉庫組就能夠間接的訪問這個組內全部的倉庫。
    • 組內能夠包括多個代理倉庫或者宿主倉庫。
  3. 組內倉庫順序

    • 倉庫組所包含的倉庫的順序決定了倉庫組遍歷組內倉庫的次序。
    • 通常將經常使用的倉庫放在前面。

4. 配置maven從Nexus下載構件

4.1 項目級別的配置

  1. 在項目pom文件配置倉庫(${project.basedir}/pom.xml)

    <repositories>
         <repository>
             <id>maven-nexus</id>
             <url>http://nexus服務ip:8081/repository/maven-public/</url>
             <releases>
                 <enabled>true</enabled>
             </releases>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
         </repository>
     </repositories>
  2. 在用戶配置中配置倉庫認證信息(~/.m2/setting.xml

    <server>
      <!-- 注意:這裏id的值和`pom.xml中repository->id`的值一致 -->
      <id>maven-nexus</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
  3. 構件下載驗證

    • 刪除demo1項目使用到本地倉庫中的org.springframework:spring-web:5.2.1.RELEASE構件。
    • 在demo1項目根目錄執行mvn compile
    • 查看命令執行結果

      [INFO] Scanning for projects...
      [INFO] 
      [INFO] -----------------------< com.john.example:demo1 >-----------------------
      [INFO] Building HellMaven 1.0
      [INFO] --------------------------------[ jar ]---------------------------------
      Downloading from maven-nexus: http://nexus服務ip:8081/repository/maven-public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom
      Downloaded from maven-nexus: http://nexus服務ip:8081/repository/maven-public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom (1.9 kB at 15 kB/s)
      ...
      [INFO] 
      [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo1 ---
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 0 resource
      [INFO] 
      [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo1 ---
      [INFO] Nothing to compile - all classes are up to date
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time:  26.582 s
      [INFO] Finished at: 2019-12-17T23:30:09+08:00
      [INFO] ------------------------------------------------------------------------

4.2 用戶級別的配置(鏡像)

  1. 修改用戶配置文件(~/.m2/setting.xml),配置鏡像

    <mirror>
      <id>mirror-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus鏡像</name>
      <url>http://nexus服務ip:8081/repository/maven-public/</url>
    </mirror>
  2. 配置倉庫認證信息(~/.m2/setting.xml)。

    <server>
      <!-- 注意:這裏id的值和`pom.xml中repository->id`的值一致 -->
      <id>mirror-nexus</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
  3. 構件下載驗證。

    • 刪除項目pom文件中倉庫的配置。
    • 刪除demo1項目使用到本地倉庫中的org.springframework:spring-web:5.2.1.RELEASE構件。
    • 在demo1項目根目錄執行mvn compile
    • 查看命令執行結果

      [INFO] Scanning for projects...
      [INFO] 
      [INFO] -----------------------< com.john.example:demo1 >-----------------------
      [INFO] Building HellMaven 1.0
      [INFO] --------------------------------[ jar ]---------------------------------
      Downloading from mirror-nexus: http://nexus服務ip:8081/repository/maven-public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom
      Downloaded from mirror-nexus: http://nexus服務ip:8081/repository/maven-public/org/springframework/spring-web/5.2.1.RELEASE/spring-web-5.2.1.RELEASE.pom (1.9 kB at 16 kB/s)
      [INFO] 
      [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo1 ---
      [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
      [INFO] Copying 0 resource
      [INFO] 
      [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo1 ---
      [INFO] Nothing to compile - all classes are up to date
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time:  26.202 s
      [INFO] Finished at: 2019-12-17T23:49:22+08:00
      [INFO] ------------------------------------------------------------------------

5. 部署構件到Nexus

5.1 使用maven部署構件

  1. 快照版本構件

    • 平常開發生成的快照版本構件,部署到Nexus中策略爲Snapshot的宿主倉庫中。
  2. 發佈版本構件

    • 項目正式發佈的構件,部署到Nexus中策略爲Release的宿主倉庫中。
  3. 項目pom配置(${project.basedir}/pom.xml)

    <distributionManagement>
        <!-- Release 倉庫 -->
        <repository>
            <id>nexus-releases</id>
            <name>nexus私服中宿主倉庫->存放/下載發佈版本的構件</name>
            <url>http://nexus服務ip:8081/repository/maven-releases/</url>
        </repository>
    
        <!-- Snapshot 倉庫 -->
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>nexus私服中宿主倉庫->存放/下載快照版本的構件</name>
            <url>http://nexus服務ip:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
  4. 配置倉庫認證信息(~/.m2/setting.xml

    <server>
      <!-- 注意: `id`的值和pom.xml中的`distributionManagement->repository->id`的值一致。-->
      <id>nexus-releases</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    
    <server>
      <!-- 注意: `id`的值和pom.xml中的`distributionManagement->snapshotRepository->id`的值一致。-->
      <id>nexus-snapshots</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
  5. 部署項目(mvn deploy)

    • 根據項目的版本,決定是部署到Release策略的倉庫仍是Snapshot策略的倉庫。
    • <version>1.0-SNAPSHOT</version>: Uploaded to nexus-snapshots。
    • <version>1.0</version>: Uploaded to nexus-releases。
  6. 注意

    • snapshot版本的構件支持重複部署到私服。
    • release版本的構件默認不支持重複部署。這與要部署倉庫的Deployment policy設置有關。

5.2 手動部署第三方構件

  1. 上傳時,首先選擇要上傳構件的宿主倉庫。
  2. 而後選擇本地文件,輸入構件座標。
相關文章
相關標籤/搜索