本篇文章主要介绍如何在 CentOS 系统上使用 nexus 搭建 Maven 服务器。

博主博客

nexus 搭建 Maven 服务器

一、安装 Java(已安装跳过)

注: nexus 3.X 必须使用 Java 8, 我在下方安装的是 Java 11。

1.一键安装 JDK 11。

yum install java-11-openjdk -y

2.测试。

[root@server ~]# java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)

二、nexus下载

1.进入 官网
2.点击 GET REPOSITORY OSS 下载社区版的 nexus。

三、nexus 安装

# 解压到 /opt/nexus/ 目录下
tar -zxvf nexus-3.37.1-01-unix.tar.gz -C /opt/nexus/

解压后会产生两个目录nexus-3.37.1-01(nexus 服务目录)和sonatype-work(私有库目录)。

四、 nexus 配置

# 查看配置
cat /opt/nexus/nexus-3.37.1-01/etc/nexus-default.properties

默认 application-port=8081 即服务端口为 8081, 需要防火墙开启端口。

五、 启动 nexus

# 进入文件夹
cd /opt/nexus/nexus-3.37.1-01/bin
# 启动 nexus
./nexus start

如果提示

[root@server bin]# ./nexus 
No suitable Java Virtual Machine could be found on your system.
The version of the JVM must be 1.8.
Please define INSTALL4J_JAVA_HOME to point to a suitable JVM.

需要配置 INSTALL4J_JAVA_HOME_OVERRIDE

1.JDK 8 解决方案。

vim nexus

设置 INSTALL4J_JAVA_HOME_OVERRIDE 的值(根据自己 JVM 的位置填写)。

# Uncomment the following line to override the JVM search sequence
INSTALL4J_JAVA_HOME_OVERRIDE=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-3.el8_5.x86_64

我上面使用 JDK 11 的路径是运行不了的, 但使用 yum 安装的 JDK 8 位置一样的, 调成自己位置即可。

2.JDK 11 解决方案。

# 下载 JDK 8 解压到 /opt/ 目录
tar -zxvf jdk-8u311-linux-x64.tar.gz  -C /opt/
# 修改配置
vim /opt/nexus/nexus-3.37.1-01/bin/nexus

设置 INSTALL4J_JAVA_HOME_OVERRIDE 的值(根据自己 JVM 的位置填写)。

# Uncomment the following line to override the JVM search sequence
INSTALL4J_JAVA_HOME_OVERRIDE=/opt/jdk1.8.0_311/jre

六、设置开机自启动

# 编辑配置文件
vim /etc/init.d/nexus

设置下面内容

#!/bin/bash
export JAVA_HOME=/opt/jdk1.8.0_311
case $1 in
	start) su root /opt/nexus/nexus-3.37.1-01/bin/nexus start;;
	stop) su root /opt/nexus/nexus-3.37.1-01/bin/nexus stop;;
	status) su root /opt/nexus/nexus-3.37.1-01/bin/nexus status;;
	restart) su root /opt/nexus/nexus-3.37.1-01/bin/nexus restart;;
	dump) su root /opt/nexus/nexus-3.37.1-01/bin/nexus dump;;
	console) su root /opt/nexus/nexus-3.37.1-01/bin/nexus console;;
	*) echo "Usage: nexus {start|stop|run|run-redirect|status|restart|force-reload}"
esac

设置脚本权限

chmod +x /etc/init.d/nexus 

可以使用 service 命令启动 nexus

service nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus

添加到自启动

chkconfig nexus on

查看 nexus 开机启动

chkconfig --list nexus
 
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
 
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
 
nexus           0:off   1:off   2:on    3:on    4:on    5:on    6:off

六、登录

1.nexus 服务启动后, 可通过浏览器访问 http://IP:8081 点击右上角的 Sign in 进行登录。
2.首次登录需要密码, admin 用户的密码在 /opt/nexus/sonatype-work/nexus3/admin.password, 我们可查看该文件内容, 输入密码。
3.首次登录后, 系统会要求重置密码, 根据自己需要设置即可。

七、创建代理仓库

1.点击 repositories
2.点击 Create repository
3.选择maven2(proxy)
4.填写Name aliyun-proxy(可任意填写)。
5.填写Remote storage **http://maven.aliyun.com/nexus/content/groups/public/**。
6.点击 Create repository 即可创建。
7.*点击 repositories, 点击 maven-public, 把 aliyun-proxy 移到 Members 中, 在 maven-public 中应用 aliyun-proxy。
8.*点击 repositories, 点击 maven-publiccopy 按钮, 即可使用该地址设置客户端的 repositories

其他

1.仓库说明

(1)默认仓库说明:

maven-central:maven 中央库,默认从 https://repo1.maven.org/maven2/ 拉取 jar
maven-releases:私库发行版 jar,初次安装请将 Deployment policy 设置为 Allow redeploy
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 或项目 pom.xml 中使用

(2)仓库类型说明:

group:这是一个仓库聚合的概念,用户仓库地址选择 Group 的地址,即可访问 Group 中配置的,用于方便开发人员自己设定的仓库。maven-public 就是一个 Group 类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x 默认为 Releases、Snapshots、Central,当然你也可以自己设置。
hosted:私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的 jar 文件
snapshots:本地项目的快照仓库
releases: 本地项目发布的正式版本
proxy:代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的 Configuration 页签下 Remote Storage 属性的值即被代理的远程仓库的路径),如可配置阿里云 maven 仓库
central:中央仓库