Nexus 搭建 Maven私服竟如此Easy

前景

趁着最近雙十一,買個低配服務器搭建Maven的私服,把平常用到的一些jar包,扔到上面,方便使用公司電腦和自己電腦時不會找不到私包。

資源與預裝

機器配置: 1核2g
系統: centos

預裝Java、maven、docker、docker-compose 等環境

可以使用我簡單編寫的 腳本進行快速 安裝新機環境,將會預裝了JAVA、Maven、Gradle、Docker、docker-compose。
腳本地址: https://github.com/liaozihong/linux-install-sh

開始安裝Nexus

version: '2'

services:
  nexus:
    image: sonatype/nexus3
    restart: always
    container_name: nexus
    ports:
    - "8081:8081"
    volumes:
    - ./nexus-data:/nexus-data

注意若出現類似以下權限不足的問題

mkdir: cannot create directory '../sonatype-work/nexus3/log': Permission denied

mkdir: cannot create directory '../sonatype-work/nexus3/tmp': Permission denied

需要爲nexus-data進行授權

sudo chown -R 200:200 nexus-data

接着,在啓動nexus,若沒問題可看到,啓動成功標識:

2019-11-12 02:06:57,906+0000 INFO  [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - 
-------------------------------------------------
Started Sonatype Nexus OSS 3.19.1-01
-------------------------------------------------

進入nexus管理界面,地址就是http://host:8081/

image.png

點擊Sign in 登錄,3.19 密碼是一個臨時憑證,需要進入nexus-data目錄裏查看

cat admin.password

複製,使用admin + 憑證,首次登錄,進入重設密碼。

image.png

即可進入私有倉庫。

Nexus相關概念介紹

默認啓動nexus後,會自動幫我們新建了幾個Repositories,如下圖:

image.png
一般的,repositories有3種類型:

  • proxy 遠程倉庫的代理,比如說nexus配置了一個central repository的proxy,當用戶向這個proxy請求一個artifact的時候,會現在本地查找,如果找不到,則會從遠程倉庫下載,然後返回給用戶。
  • hosted 宿主倉庫,用戶可以把自己的一些倉庫deploy到這個倉庫中
  • group 倉庫組,是nexus特有的概念,目的是將多個倉庫整合,對用戶暴露統一的地址,這樣就不需要配置多個倉庫地址。

介紹下,幾個默認已經建好的倉庫:

  • maven-central ,這是一個 proxy 類型的倉庫,點進去可以看到,它代理的遠程倉庫地址是:https://repo1.maven.org/maven2/
  • maven-public ,這是一個 group 類型的倉庫,它涵蓋了maven-releases、maven-snapshots、maven-central倉庫,也就是我們只要在本地添加這個倉庫,就可以依賴到這3個倉庫的包了。

上傳本地庫

那麼,重點來了,我們怎麼將本地的依賴庫上傳到nexus上呢,很簡單了,確保nexus上有相應的repositories,可以上傳到默認已有的maven-*,也可以新建屬於自己的repositories,這裏演示下新建。

新建repositories

點擊Create repository,選擇maven2(hosted)類型,進入配置頁。

image.png
這個repository要存儲什麼類型的庫呢,這裏有三個選項:

  • Release 發佈版,代表已經可以正式使用的庫文件,需要以RELEASE 結尾的,或者使用命令mvn deploy -P release,版本號後不帶-SNAPSHOT故當成是正式發佈版本
  • Snapshots 快照版,以SNAPSHOTS 結尾的不穩定版本
  • Mixed 混亂版,版本無特別要求

這裏我創建一個Mixed 類型的倉庫,並且注意,Deployment pollcy 要選擇 Allow redeploy 允許庫遠程部署,才能把本地包部署到nexus上。

新建Users

配置一個個人賬號,用戶上傳本地庫是驗證,進入create頁
image.png
創建一個admin類型的賬號。

接着,在本地上,需要配置maven,編輯setting.xml,添加以下內容

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
      <server>
        <id>local</id>
        <!--自己新建的Users-->
        <username>zoey</username>
        <password>12415124</password>
      </server>
    </servers>
</settings>
  1. 創建一個項目,添加配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dashuai.commons</groupId>
    <artifactId>java-commons-base</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>java-commons-base</name>
    <description>基礎類庫</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <!--指定倉庫地址-->
    <distributionManagement>
        <repository>
            <!--此名稱要和.m2/settings.xml中設置的ID一致-->
            <id>local</id>
            <url>http://ip:8081/repository/Zoey-Mixed/</url>
        </repository>
    </distributionManagement>

    <dependencies>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
</project>

使用命令將庫部署到nexus,若想取消測試用例,加上-Dmaven.test.skip=true 即可

mvn deploy 

成功後,在Search裏就可以看到我們上傳的公共包了。
image.png

  1. 上傳本地 Jar 包至私服,使用命令即可:
mvn deploy:deploy-file -DgroupId=xxx -DartifactId=xxx -Dversion=xxx -Dpackaging=jar -Dfile=xxx -Durl=http://xxx:8081/repository/3rdParty/ -DrepositoryId=xxx

本地依賴私服倉庫

當需要在本地依賴到遠程的庫時,只需在pom.xml了中添加如下代碼。

    <dependencies>
        <dependency>
            <groupId>com.dashuai.commons</groupId>
            <artifactId>java-commons-utils</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>test</id>
            <url>http://ip:8081/repository/Zoey-Mixed/</url>
        </repository>
    </repositories>

image.png

到此,完整的nexus從搭建到使用就演示完成了,小夥伴們可自己動手實踐下。有問題歡迎留下言共同探討。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章