关于私有仓库nexus的搭建和使用

720 阅读4分钟

本文已参与 \lceil新人创作礼\rfloor 活动,一起开启掘金创作之路

部署过程

下载nexus

地址:www.sonatype.com/nexus-repos…

历史版本:help.sonatype.com/repomanager…

部署JAVA环境

# mkdir /usr/java
# tar -zxvf jdk-8u172-linux-x64.tar.gz -C /usr/java
# vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_172
export CLASSES_PATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin# source /etc/profile
# source /etc/profile
# java -version

配置nexus

# mkdir /users/nexus -p
# useradd -M -d /users/nexus/ nexus
# cp /etc/skel/.bash* /users/nexus/
# tar -zxvf nexus-3.16.1-02-unix.tar.gz
# mv nexus-3.16.1-02 sonatype-work/ /users/nexus/
# chown -R nexus:nexus /users/nexus/
# su - nexus
$ vim nexus-3.16.1-02/bin/nexus.rc
run_as_user="nexus"

image.png

设置服务端口,默认8081:

$ vim nexus-3.16.1-02/etc/nexus-default.properties

image.png

设置数据存储目录:
$ vim nexus-3.16.1-02/bin/nexus.vmoptions image.png

设置环境变量:

# vim /etc/profile
export NEXUS_HOME=/users/nexus/nexus-3.16.1-02
export PATH=$PATH:$NEXUS_HOME/bin
# souce /etc/profile

服务启动

# su - nexus
$ nexus start
$ ps -ef | grep java
$ netstat -tnlp | grep 8081 (启动后需要等待大约一分钟)

  停止:

$ nexus stop

访问

http://IP:8081   默认用户/密码:admin/admin123 image.png image.png

使用

角色管理

image.png

用户管理

image.png

增加用户: image.png

image.png

禁用匿名用户:

image.png

image.png

查看系统信息

image.png

Repository

Blob Stores

文件存储的地方,创建一个目录的话,对应文件系统的一个目录

image.png

相关概念

image.png name说明:

1)maven-central:maven中央库,默认从repo1.maven.org/maven2/拉取ja… 

2)maven-releases:私库发行版jar 

3)maven-snapshots:私库快照(调试版本)jar 

4)maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

Nexus的仓库类型:

1)group(仓库组类型):又叫组仓库,用于方便开发人员自己设定的仓库;

2)hosted(宿主类型):内部项目的发布仓库(内部开发人员,发布上去存放的仓库);

3)proxy(代理类型):从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage Location属性的值即被代理的远程仓库的路径;

它们的关系图如下: image.png

  • Maven可直接从宿主仓库下载构件,也可以从代理仓库下载构件,而代理仓库间接的从远程仓库(比如熟悉的aliyun)下载并缓存构件
  • 为了方便,Maven还可以从仓库组下载构件,它会转向包含宿主仓库或者代理仓库获得实际构件的内容

Public Repositories下的仓库 

   1)3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库,即第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去;

   2)Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库 

   3)Central: 用来代理maven中央仓库中发布版本构件的仓库 

   4)Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库 

   5)Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库 

   6)Releases: 内部的模块中release模块的发布仓库,用来部署管理内部的发布版本构件的宿主类型仓库;release是发布版本;

   7)Snapshots:发布内部的SNAPSHOT模块的仓库,用来部署管理内部的快照版本构件的宿主类型仓库;snapshots是快照版本,也就是不稳定版本

所以自定义构建的仓库组代理仓库的顺序为:Releases,Snapshots,3rd party,Central。

创建proxy repositories

优势:只要其中一个人从中央库下来了,以后大家都是从Nexus私服上进行下来,私服一般部署在内网,这样大大节约的宽带

image.png

image.png

Name:就是为代理起个名字

Remote Storage: 代理的地址

maven.aliyun.com/nexus/conte…

Blob Store: 选择代理下载包的存放路径

image.png

image.png

最后点击save保存: image.png

再添加一个aliyun-central:

maven.aliyun.com/nexus/conte…

image.png

image.png

image.png

创建hosted repositories

Hosted是宿主机的意思,就是把第三方的Jar放到私服上。

Hosted有三种方式,Releases、SNAPSHOT、Mixed

Releases: 一般是已经发布的Jar包

Snapshot: 未发布的版本

Mixed:混合的

创建一个releases版本的hosted:

image.png

image.png

image.png

Deployment Pollcy: 需要把策略改成“Allow redeploy”

创建grouprepositories

image.png

image.png

把阿里云的两个proxy和两个hosted类型加入到组中:

image.png

和Maven做集成

集成的方式主要分以下种情况:代理中央仓库、Snapshot包的管理、Release包的管理、第三方Jar上传到Nexus上

这里我们配置所有的snapshot版本构件部署到Nexus的Snapshots仓库中, 所有的release构件部署到Nexus的Releases仓库中。由于部署需要登陆,因为我们在settings.xml中配置对应Repository id的用户名和密码。然后,在项目目录中执行mvn deploy ,你会看到maven将项目构件部署到Nexus中,浏览Nexus对应的仓库,就可以看到刚才部署的构件。当其他人构建其项目时,Maven就会从Nexus寻找依赖并下载。

1、在pom文件中配置私服的地址(比如http://192.168.60.115:8081), 日常开发生成的快照版本构件可以直接部署到Nexus中策略为Snapshot的宿主仓库中,项目正式发布的构件则应该部署到Nexus中策略为Release的宿主仓库中:


<repositories>

    <repository>

        <id>maven-central</id>

        <name>maven-central</name>

        <url>http://192.168.60.115:8081/repository/nexus-public/</url>

        <snapshots>

            <enabled>true</enabled>

        </snapshots>

        <releases>

            <enabled>true</enabled>

        </releases>

    </repository>

</repositories>

 

<distributionManagement>

    <snapshotRepository>

        <id>nexus</id>

        <name>Nexus Snapshot</name>

        <url>http://192.168.60.115:8081/repository/nexus-snapshots/</url>

</snapshotRepository>

 

    <repository>

        <id>nexus</id>

        <name>Nexus Release</name>

        <url>http://192.168.60.115:8081/repository/nexus-releases/</url>

    </repository>

</distributionManagement>

 

2、修改Maven的settings.xml文件,加入认证机制:

# vim /users/yozo/apache-maven-3.6.0/conf/settings.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">

<localRepository>/tmp</localRepository>   //设置本地仓库jar保存位置,默认存储位置为${user.home}/.m2/repository
  <pluginGroups>  

     <pluginGroup>org.sonatype.plugins</pluginGroup>  

  </pluginGroups>

  <proxies>

  </proxies>

  <servers>

        <server>  

            <id>nexus</id>   //注意和后面的id要匹配(pom中)

            <username>admin</username>  

            <password>admin123</password>  

        </server>

  </servers>

  <mirrors>
        <mirror>  //公共仓库组配置mirrorOf的值为*,代表maven的所有访问请求都会指向到public仓库组。

            <id>nexus</id>  

            <mirrorOf>*</mirrorOf>  

            <url>http://192.168.60.115:8081/repository/nexus-public/</url>  

        </mirror>      

  </mirrors>


  <profiles>

            <profile>  

            <id>nexus</id>  

            <repositories>

                <repository>

                    <id>nexus</id>  

                    <url>http://nexus.yozocloud.cn/repository/nexus-public/</url>  

                    <releases>

<enabled>true</enabled>

</releases>

                    <snapshots>

<enabled>true</enabled>

</snapshots>
                </repository>
            </repositories>

            <pluginRepositories>  

                <pluginRepository>  

                    <id>nexus</id>  

                    <url>http://nexus.yozocloud.cn/repository/nexus-public/</url>  
                    <releases>

<enabled>true</enabled>

</releases>  

                    <snapshots>

<enabled>true</enabled>

</snapshots>  

                </pluginRepository>

            </pluginRepositories>

 

        </profile>

  </profiles>

 

    <activeProfiles>  //激活配置

        <activeProfile>nexus</activeProfile>  

    </activeProfiles>

</settings>

 

配置完执行:

$ mvn clean source:jar package
$ mvn deploy -e

查看snapshots仓库就能看到上传的jar了

好了,今天的分享就到这里了,希望对大家有所帮助,我们下期再见~~