什么是Nexus?它有什么优势?
要了解为什么需要nexus的存在,我们不妨从以下几个问题来简单了解一下:
为什么需要搭建私服?如果没有私服会出现什么问题?
对于企业开发而言,如果没有私服,我们所有的依赖都需要从 Maven 的中央仓库或者第三方的 Maven 仓库下载到本地。如果一个团队中的所有人都重复的从 Maven 仓库下载组件无疑加大了仓库的负载和带宽的消耗,而且如果企业的带宽资源紧张,引入依赖等待时长更是灾难的存在。
另外,很多情况下项目的开发都是在内网进行的,我们某些组件不可能发布到外网,所以我们可能需要一个自己的maven私服。
搭建私服有什么优势?
Maven 私服的概念就是在本地架设一个 Maven 仓库服务器,在代理远程仓库的同时维护本地仓库。当我们需要下载一些构件(artifact)时,如果本地仓库没有,则再去私服下载,私服没有再去中央仓库下载(例如阿里云仓库)并将其缓存下来,这样一来下一个用户可以直接在本地私服得到自己需要的资源,这就避免了没必要的带宽消耗。

小结一下,这种私服的依赖部署方式会有以下几个优点:
- 减少网络带宽流量。
- 加速 Maven 构建。
- 便于获取第三方隐私组件。
- 提高稳定性、增强控制。
- 降低中央仓库的负载。
什么是Nexus?
Nexus 是一个专门管理Maven、npm等仓库管理软件,它不仅可以实现快速搭建私服,还具备如下一些优点使其日趋成为最流行的 Maven 仓库管理器:
- 提供了强大的仓库管理功能,构件搜索功能。
- 它基于 REST,友好的 UI 是一个 ext.js 的 REST 客户端。
- 它对于资源的占用较少。
- 基于简单文件系统而非数据库。
基于Linux搭建nexus
准备nexus
了解nexus之后,我们不妨基于Linux服务器搭建一个属于自己的nexus私服。首先我们需要到nexus官网下载一下nexus的资源。下载地址为:https://help.sonatype.com/repomanager3/product-information/download
这里需要补充一下,笔者nexus用的不是最新版本,所以到老版本里面找到了nexus-3.28.0-01-unix.tar.gz这个资源,地址为:https://help.sonatype.com/repomanager3/product-information/download/download-archives---repository-manager-3
选择的版本如下图所示:

完成资源下载之后,我们不妨将其上传到服务器上。以笔者为例,将其上传到tmp目录,意味一次实验性的部署。

完成上传后,我们先将其解压。
bash
tar -zxvf nexus-3.28.0-01-unix.tar.gz
然后进入nexus的bin目录。
bash
cd nexus-3.28.0-01/bin/
运行如下命令,将nexus启动(笔者这里为了方便排查问题,先将nexus前台启动)
bash
./nexus run
不久后控制台输出下面这段内容,就说明nexus启动成功了。我们不妨到浏览器访问这个页面。

启动nexus并测试可用性
nexus默认端口号为8081,所以我们到浏览器键入ip:8081即可进入登录页面,刚刚进入时需要初始化一下,这里我们稍等片刻。

完成初始化之后,我们点击sign in,准备进行登录。

初次进入时需要输入用户名和密码,用户名为admin,密码读者可以按照页面提示找到对应文件完成输入,如下图所示:

我们不妨使用cat命令查看一下,如下所示,得到一串字符串复制下来去登录。
cpp
[root@iZ8vb7bhe4b8nhhhpavhwpZ ~]# cat /tmp/sonatype-work/nexus3/admin.password
6f1d326f-b030-4043-ab37-xxxxxxx
拿着上文复制的密码点击登录。

然后我们就进入了初始化页面,这里直接点击next。

然后页面会提示我们修改密码,这里我们按照提示输入两次点击下一步即可。

下一个设置页面,我们设置允许匿名访问。

自此完成所有的基本配置。
配置nexus
接下来就开始我们nexus的配置步骤了,如果我们希望私服缓存的依赖需要指定到一个位置,我们可以自己创建一个blob,这个其实非必须的,这个blob配置完成之后,我们就可以将后续仓库的blob指定到到这个blob中,笔者这里为了简单就没有配置,直接用default的。如下图所示,关于blob的具体配置,笔者会在后续系列教程中演示。

我们希望nexus的工作流程为,现在自己本地releases仓库里面找,如果没有则去snapshots仓库里面找,如果快照仓库没有就去阿里云那边找,如果阿里云有则直接将其缓存到blob中。

为了做到这一点,我们首先需要配置一个阿里云的仓库,首先点击创建仓库

选择maven-proxy,创建一个代理仓库,如果本地没有找到依赖资源则通过这个代理帮我们查找。

如下图,读者可以按照读者的解释将名称、版本策略,原创仓库地址等配置设置好。

完成后点击下方create即可。

完成了代理仓库的创建,我们希望自己平时自定义的jar上传时可以按需传到release和snapshot上,比如我现在写了个临时的snapshot组件给其他开发用,我希望自己能够上传到到snapshot的仓库中,这样分门别类的规范便于后续团队包的控制。
所以我们首先配置一个快照的仓库,在配置过程中,我们发现nexus提供了一个现成的仓库,我们不妨修改一下它的配置直接使用。如下图所示,找到maven-snapshots直接点击箭头进入修改界面。

我们点击进去可以看到它的策略就是我们需要的仅仅存放快照包,然后我们将layout policy改为permissive。

往下查找,将部署策略也改为允许重复部署,避免我们对于同一版本的组件无法再次上传到maven私服中。

完成后到下方点击save即可保存。

同理,maven-release一样的配置,这里就不多做赘述了。

自此我们完成了大半部分的工作,接下来我们就要实现我们的私服查找jar包策略了,即:
- 先看看release,没有看snapshots
- snapshots没有,到代理仓库中找。
- 代理仓库中有,直接缓存到blob中。
要做到这一点,我们还是需要创建一个仓库,将上述我们配置的仓库组合起来,对外暴露。

选择配置一个maven-group,将我们上文配置的仓库按照顺序组合起来。

设置好group名称之后,我们滑倒最下方设置依赖查找顺序,以笔者为例,可以看到策略就是我们上文所说的方式自顶向下配置,完成后点击save即可。

测试从nexus私服获取资源
自此我们完成的nexus的配置了,现在我们不妨就来试试看nexus私服配置是否生效,我们到自己本机上配置一下maven的setting.xml。
首先指定本地仓库地址为E:/repository,方便自己后续查看私服下载是否成功。
bash
<localRepository>E:/repository</localRepository>
然后我们需要配置maven私服的地址,所以我们到nexus的页面找到我们配置的group点击copy。

此时页面就会弹出这个地址的值,我们将其复制下来。

然后在镜像中配置这个私服地址,确保我们后续的依赖都是通过这个私服地址去下载,而该私服地址就是我们上文配置的组合地址,它会严格按照 release仓库->snapshots仓库->阿里云仓库
顺序依次查找,配置如下所示:
bash
<mirrors>
<mirror>
<id>nexus</id>
<name>nexus repository</name>
<url>http://192.168.43.98:8081/repository/my-group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
补充说明,如果需要下载快照包的读者需要将mirror注释掉,改用下面这段配置,原因很简单,笔者在测试过程中发现maven默认不允许拉取快照包的,所以我们保留id和url的值,改用下面这段配置确保快照包可以正常拉取。
cpp
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.43.98:8081/repository/my-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
用到私服时,我们需要其账户密码,所以在setting.xml上方我们需要配置一下和我们mirror的id一直的server标签,表明私服的账户和密码值。
bash
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
这两个配置的关系如下所示,通过id来进行关联的。

然后我们创建一个spring boot项目,maven和setting.xml都设置为我们上文修改的maven。打开项目点击reimport。如下图所示,可以看到我们的maven就会到maven私服中下载资源。

完成后查看我们上文配置的仓库,可以发现jar包都从私服那边下载下来了。

回到nexus管理页面,我们也可以看到nexus把代理仓库的jar都缓存下来了。

还记得我们上文提到一个缓存jar包到blob中吗?这个blob默认名字为default,我们不妨到这个目录查看一下default有什么变化。
bash
cd /tmp/sonatype-work/nexus3/blobs/default/
输入du查看大小,可以看到大小变为90M,由此可知阿里云这个代理仓库的依赖都被缓存下来了。
bash
[root@localhost blobs]# du -sh *
90M default
上传自定义组件
接下来我们再来测试一下组件的上传,对此我们首先编写一个maven项目。写下一个测试类。

然后我们在mave项目的pom文件中添加下面这段配置,意为:如果是releases版本则传到releases对应的url,如果是snapshots则传到snapshots的url。
bash
<!-- 如果是releases版本则传到releases对应的url,如果是snapshots则传到snapshots的url-->
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.43.98:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.43.98:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
为了确保源码可以被下载,我们还需添加下面这段配置
bash
<build>
<plugins>
<!-- 要将源码放上去,需要加入此配置 -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
最后指定我们的maven项目版本为1.0,这意味着这个项目是发布版本,所以它会传到release仓库中。
bash
<groupId>org.example</groupId>
<artifactId>test-nexus</artifactId>
<version>1.0</version>
总结一下每个配置的位置如下图所示,读者可以自行核对一下。

注意,我们上文指定了两个新的url,其id对应的账户密码我们也得补充的maven的setting.xml中,内容如下所示:
bash
<server>
<id>releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>nexus</id>
<username>admin</username>
<password>Z120194199</password>
</server>
</servers>
截图如下:

完成后,我们点击clean,再点击deploy。

如果deploy输出下面这段结果就说明部署成功了。

我们不妨到nexus上找到点击browse选择发布仓库。

可以看到我们的jar包传上来了。

同样的我们到maven一栏中通过精确搜索的方式也能找到这个jar包。快照版的jar发布方式也是一样的,这里就不多做介绍了。

下载自定义组件
为了测试jar包是否可用,我们不妨创建一个新的maven项目试着将其引入看看。这里我们不妨介绍一下如何通过nexus获取自定义组件的坐标,首先点击箭头。

然后点击jar包

选择usage,即可看到maven坐标,我们将其复制下来。

然后到新项目中引入,如下图所示

然后等待该项目将从私服中不断下载依赖。

随着时间的推移,我们可以看到该我们自己的组件也下载成功了。

在项目中也能正常使用这个类。

步入源码,点击下载也能正常下载。

可以看到源码正常下载了。

自此我们nexus私服的简单入门就完成了。
一些常见的操作
删除jar包
有时候我们很多没必要的jar包上传多了会占用没必要的内存,我们希望将其从nexus中删除。我们可以选择browser,然后选择发布仓库。

找到要删除的jar包点击删除,随后页面就会提示该删除在后台进行。

随后我们就可以看到这个仓库对应的依赖空了。

我们回到本地,将对应快照包删除,尝试从私服拉取看看能不能拉取到,可以看出私服删除后,本地就拉取不到这个jar包了。

上传jar包
我们不妨接着上一个操作演示一下jar包的上传,点击upload选择mave-release

点击该选项,查找本地jar包

然后手动输入坐标,点击上传

回到broswer,可以看到这个jar包我们上传上来了。

再一次拉取,同步成功,由此nexus基本入门完成。
