Nexus 安裝配置教程


為什么使用 Nexus

Nexus 最為大家熟知的功能就是 maven 的依賴包管理器。其實 Nexus 的功能不僅僅是 maven 的包管理,它還可以做 .net 包管理,docker 鏡像管理,甚至還可以做 yum 源。這篇文章主要介紹 Nexus 的 maven 依賴管理功能,介紹 Nexus 的安裝,配置。

架設 Nexus 私服有以下優點:

  • 節省公司外網貸款;
  • 因為依賴會被緩存到私服上,所以依賴的下載速度會很快;
  • 方便上傳團隊內部的依賴,統一管理,共享。

Docker 模式安裝 Nexus

我們選擇使用 Docker 的方式安裝 Nexus。

Nexus的官方網站:https://www.sonatype.com/products/repository-oss-download

Nexus的官方幫助文檔:https://help.sonatype.com/repomanager3

Nexus的Docker安裝介紹:https://help.sonatype.com/repomanager3/installation/installation-methods#InstallationMethods-InstallingwithDocker

官方有兩種方式

  1. 使用 docker 的 data volume (推薦)
  2. 使用本地目錄作為 container 的 volume

使用 data volume

docker volume create --name nexus-data
docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3

使用本地目錄

mkdir nexus
cd nexus
docker run -d -p 8081:8081 --name nexus -v $PWD/nexus-data:/nexus-data sonatype/nexus3

安裝起來特別簡單。

安裝完畢,訪問 127.0.0.1:8081,可以直接登陸。

image-20210618181621641

Nexus 配置

關於怎么配置 Nexus 這邊不做太詳細的說明。對默認的幾個倉庫坐下說明。

默認情況下,Nexus 會幫我們創建幾個倉庫:

  • maven-central:代理倉庫,一般會連接外部的中央倉庫;
  • maven-public:倉庫組,一般提供這個倉庫給公司內部的同事使用;
  • maven-release:本地倉庫,一般用於存放公司內部開發的Jar包;
  • maven-snapshots:本地倉庫,存放公司開發的snapshot版本的包;
  • maven-3rd-party:本地倉庫,存放第三方的Jar包。

配置 Blob Stores

Nexus 使用

包下載

如果你只需要使用包下載功能,只需要替換本地的settings.xml即可。

<settings>
    <localRepository>D:\software\maven\Repository</localRepository>
    <proxies></proxies>
    <servers>
        <server>
            <id>nexus</id>
            <username>devops</username>
            <password>password</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>nexus</id>
            <mirrorOf>nexus</mirrorOf>
            <url>http://xx.xx.xx.xx:18081/repository/maven2-public/</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus</id>
		    <url>http://nexus</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
		    <url>http://nexus</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

包上傳

方法一:通過 Nexus 界面上傳

給用戶開通上傳Jar包的權限。用戶就可以通過頁面上傳Jar包了。

image-20210621100640169

方法二:通過配置pom文件進行Jar包上傳

在項目的pom中加入以下的配置。

<distributionManagement>
        <snapshotRepository>
            <!--注意,這個Id需要和上面的setting文件中的server標簽中的id一一致-->
            <id>nexus</id>
            <name>nexus-snapshot</name>
            <url>http://xx.xx.xx.xx:18081/repository/maven2-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>nexus</id>
            <name>nexus-release</name>
            <url>http://xx.xx.xx.xx:18081/repository/maven2-releases/</url>
        </repository>
</distributionManagement>

完成此步驟后,我們就可以通過執行mvn clean deploy進行發布了。Deploy插件會根據Maven項目中定義的version值決定是使用nexus-snapshot倉庫還是nexus-release倉庫。當version值是以-SNAPSHOT后綴結尾時,則發布到nexus-snapshot倉庫。

方法三:通過命令行(實質和上面pom的形式一樣)

mvn deploy:deploy-file -DgroupId=com.jd.jr.xx -DartifactId=gateway-xx -Dversion=1.8.xx-SNAPSHOT -Dpackaging=jar -Dfile=xx-api-1.8.0-RELEASE.jar -Durl=http://124.205.xx.xx:18081/repository/maven2-snapshots/ -DrepositoryId=nexus

上面的-DrepositoryId=nexus需要和配置的server標簽匹配起來。

參考


免責聲明!

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



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