Nexus的搭建
# 120.Nexus的搭建
接下来我们讲讲私服,该内容了解即可,因为公司中一般是安装好私服后给我们使用。我们主要是知道怎么用:上传jar包到私服,和下载jar包到本地。
# 下载Nexus
Nexus是Sonatype公司发布的一款仓库管理软件,常用于搭建Maven私服。更多详情,可以访问官网 (opens new window)。
我们可以在下载页面 (opens new window)上,下载最新的Nexus:
或者去我的网盘下载,路径为编程资料/Java相关/03.JavaEE/06.Maven。
为了方便演示,接下来我们就以在Windows下为例安装Nexus,注意只支持 64 位的 Windows 操作系统,32 位的不支持。但在实际工作中,Nexus一般都是在Linux服务器上的。
我们解压到一个没有中文和空格的目录里,例如:D:\Projects\nexus-3.53.0-01-win64。此时该目录里有两个文件夹:
- nexus-3.53.0-01:nexus服务器相关的文件
- sonatype-work:存放生成的配置文件、日志文件、仓库文件等
# 启动Nexus
我们进入到目录nexus-3.53.0-01-win64\nexus-3.53.0-01\bin,然后在命令行执行:nexus.exe /run
,或者nexus /run
。启动的有点久,亲测大概有10多分钟,最后显示 Started Sonatype Nexus OSS 3.53.0-01
,表示启动成功。
随着Nexus的更新,启动方式可能会变化,以官网文档为准。
我们也可以添加添加到系统环境变量 Path 内,这样就能直接在任意目录下使用Nexus命令
# 访问Nexus
我们打开http://127.0.0.1:8081/ (opens new window),可以看到如下的页面:
点击右上角的 Sign in
可以 admin 账号登陆进行相关配置,首次点击 Sign in
,会提示管理员 admin 账号的初始密码已自动生成在文件 sonatype-work/nexus3/admin.password
内,我们打开该文件,拿到密码,然后输入并登录。
登录后会继续弹出对话框,做一些设置,例如要求修改密码:
之后会再弹出一个对话框设置是否允许匿名浏览、下载仓库中的包:(建议勾选,方便我们测试),然后就完成设置了
8081是默认端口,可以通过改配置文件来修改:nexus-3.53.0-01\etc\nexus-default.properties
# 查看仓库种类
我们点击下设置-然后点击Repositories:
可以看到仓库的类型(Type列),其中hosted就是本地仓库的意思。
注意到有个maven-snapshots仓库吗?再想想我们之前自己的项目,都是SNAPSHOT,他们是有关联的。测试版的依赖就放在这里
<groupId>com.peterjxl</groupId>
<artifactId>LearnJavaMaven</artifactId>
<version>1.0-SNAPSHOT</version>
2
3
而maven-release就是发行版。除此之外,还有proxy类型,例如maven-central,就是中央仓库。
我们在看看maven-central,它的类型是group,是配置一组仓库,后续我们经常使用的地址。
我们可以单击它,点进去看看:左下角我通过箭头配置了下仓库,其含义是首先从maven-releases仓库找依赖,然后从maven-snashots找依赖,都没有则去中央仓库找依赖。
一般工作中,一个项目会就单独配置一个单独的仓库,不和其他仓库混在一起用
# 注册为Windows服务
我们还可以注册Nexus为Windows的一个服务:
nexus /install
Installed service 'nexus'.
2
然后我们可以在管理页面,查看到Nexus的服务。
注册完后,我们就可以通过后台启动的方式,来启动Nexus了:
nexus /start
Starting service 'nexus'.
2
可以看到Nexus确实是启动了
如果要卸载:一条命令即可
nexus /uninstall
# 查看更多命令
执行 nexus /? 命令可查看可用的命令清单:
nexus /?
Usage: nexus {start|stop|run|run-redirect|status|restart|force-reload}
2
# 可能遇到的问题
启动一段时间后,可能会在命令行里看到这样的报错:
2023-05-13 10:09:39,718+0800 INFO [qtp537004006-453] *UNKNOWN org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler...
2023-05-13 10:09:39,725+0800 INFO [qtp537004006-453] *UNKNOWN org.sonatype.nexus.internal.security.anonymous.AnonymousManagerImpl - Using default configuration: OrientAnonymousConfiguration{enabled=true, userId='anonymous', realmName='NexusAuthorizingRealm'}
2023-05-13 10:10:00,022+0800 INFO [quartz-10-thread-1] *SYSTEM org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'Storage facet cleanup' [repository.storage-facet-cleanup] state change WAITING -> RUNNING
2023-05-13 10:10:00,036+0800 INFO [quartz-10-thread-1] *SYSTEM org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'Storage facet cleanup' [repository.storage-facet-cleanup] state change RUNNING -> WAITING (OK)
2023-05-13 10:10:05,447+0800 WARN [pool-5-thread-1] anonymous com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.ConnectTimeoutException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/108.160.166.62] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
2
3
4
5
6
7
8
报该错误的原因是nexus的欢迎页面会从Sonatype重定向一些信息进行显示,这个功能叫做Outreach,他的地址是国外的连不上。所以你会发现你的欢迎页面是空荡荡的,什么都没有。为了避免周期性的报错,可以把Outreach禁用。
该问题不会影响我们的使用,或者可以通过设置代理和禁用该模块来去掉报错信息。
参考官网或者StackOverflow的说明:
基于CentOS7+docker+Nexus官方镜像搭建私有maven仓库_PEACEFIRE的博客-CSDN博客 (opens new window)
How to avoid, Could not download page bundle, messages – Sonatype Support (opens new window)
sonatype - Nexus Oss error regarding connection - Stack Overflow (opens new window)
The informational log message is because your computer is not connected to the outside world. This is not a warning or error and will not affect your performance.
The screenshot strings you are seeing are because of confusing message in the NXRM3 system (and a deviance from NXRM2) as well as the fact that NXRM3 is not currently designed to reach out to the remote proxies before called. This deviance is described here: https://issues.sonatype.org/browse/NEXUS-9878 (opens new window)
These repositories are in fact ready to connect. If they were failing (such as if you try and use them and your computer is not connected to the outside world), there would be a message such. Once you connect with them (such as receiving a component from them) they will change to "Online - Remote Available".
In the next release of NXRM3 the message will change from "Online - Remote Connection Pending..." to "Online - Ready to Connect" to help alleviate this confusion. Public ticket here: https://issues.sonatype.org/browse/NEXUS-11001 (opens new window)
# 小结
本文我们简单介绍了下Windows下如何使用Nexus,后续我们介绍如何从Nexus,上传和下载依赖。