搭建Nexus私服
一、安装启动Nexus
1. 准备环境
服务端 Centos7.5 + Nexus3 + JDK1.8
客户机 window + maven + JDK1.8
2.下载Nexus3
官网地址:https://www.sonatype.com/2023 (较慢、vpn好些)
或 https://download.csdn.net/download/only_xiao_/87952466
3.安装启动
3.1 安装
- 1.在usr/local目录下创建Nexus安装目录:
cd /usr/local
mkdir soft
cd soft
mkdir nexus
cd nexus
- 2.将资源放在nexus文件下
- 3.解压nexus文件
tar zxvf nexus-3.56.0-01-unix.tar.gz
- 4.配置 nexus 环境变量
vim /etc/profile
# 按 END 建 快速定位到文档最后,按i 输入 以下内容
# nexus 配置(解压后的nexus文件夹路径)
export MAVEN_HOME=/usr/local/soft/nexus/nexus-3.56.0-01
export PATH=$PATH:$MAVEN_HOME/bin
# 使环境变量生效
cd /etc
source profile
3.2 启动
- 1.进入安装目录bin文件下启动
cd /usr/local/soft/nexus/nexus-3.56.0-01/bin
nexus start
# 查看状态
nexus status
注:我是root用户启动有警告但不影响
- 2.如果默认端口8081被占用可尝试修改端口
# 1. nexus 默认端口是 8081 , 如果我们的端口被占用了,则需要重新为 nexus 指定端口,端口的配置文件在安装目录下的 etc 目录
cd /usr/local/soft/nexus/nexus-3.56.0-01/etc/
# 2.进入 etc 目录,找到 nexus-default.properties 文件
# 3.用 vim 打开 nexus-default.properties 文件
vim nexus-default.properties
# 4.将application-port后端口号进行修改如图
- 3.开放端口
# 开启8081端口
firewall-cmd --zone=public --add-port=8081/tcp --permanent
# 重启防火墙
firewall-cmd --reload
- 4.重启服务
# 重启
nexus restart
-
- 其他指令
# 启动
nexus start
# 停止
nexus stop
# 重启
nexus restart
# 查看状态
nexus status
- 6.重新安装(安装有问题,重新安装)
# 停止服务
nexus stop
# 删除旧解压文件
cd /usr/local/soft/nexus
rm -rf nexus-3.56.0-01
rm -rf sonatype-work
# 回到第3.2中第3步骤 重新解压...
3.3外部访问登录
-
- 访问地址:http://192.168.163.136:8081/
端口如果变更换成自己的端口
- 访问地址:http://192.168.163.136:8081/
- 2.首次初始化密码位置的弹窗
## 首次登录初始化密码存放的位置
cd /usr/local/soft/nexus/sonatype-work/nexus3
## 查看密码(是一个UUID)
cat admin.password
# 或者一步到位
cat /usr/local/soft/nexus/sonatype-work/nexus3/admin.password
注:1.只有首次加载访问才会有admin.password这个文件,这个文件不要动,这里修改密码无效的 2.需要通过管理端首次登陆后会有修改密码提示(修改密码后admin.password文件会自动删除的)、是否启用匿名访问选项、其他一路Next
二、使用配置Nexus
1.使用admin账户登录 , 然后点击齿轮(Configuration) > Repositories(仓库)
干货:
仓库有3类 , proxy(代理仓库-也就是别人的仓库) hosted(私有仓库 - 也就是自己的仓库) group(聚合仓库 - 一般引用都是使用这个库)
2.配置ali的maven镜像,提高速度 (代理库)
阿里rep地址 : https://maven.aliyun.com/repository/public
2.1 点击 Create repository
2.2选择maven2(proxy)
2.3定义名字、填写URL、最下面点击Create repository
3.在group中加入ali镜像
找到maven-public的group仓库 , 点最右边的>
进入修改
在最下面 Group处 , 把ali的镜像加入
4.私有仓库(hosted)不用创建 , 使用原有的 maven-releases 和 maven-snapshots即可
三、配置Maven客户端和pom.xml实现发布
1. 配置setting.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">
<!-- 配置本地jar包存放路径 -->
<localRepository>D:/repository</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<!-- 配置从nexus私服下载jar包所需要的账号密码 -->
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- 配置往nexus私服上传jar包所需要的账号密码 -->
<server>
<!--注意这个id 需要和pom.xml的对应-->
<id>maven-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- 配置往nexus私服上传jar包所需要的账号密码 -->
<server>
<!--注意这个id 需要和pom.xml的对应-->
<id>maven-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!-- 配置镜像代理 -->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf><!-- 配置为*的意思是将所有请求都转发到镜像仓库上 -->
<url>http://192.168.163.136:8081/repository/maven-public/</url>
</mirror>
<!--这2个备用 以免在外网环境连不上私服-->
<!-- <mirror>-->
<!-- <id>alimaven</id>-->
<!-- <mirrorOf>central</mirrorOf>-->
<!-- <url>https://maven.aliyun.com/repository/public/</url>-->
<!-- </mirror>-->
<!-- <mirror>-->
<!-- <id>alimaven_central</id>-->
<!-- <mirrorOf>central</mirrorOf>-->
<!-- <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>-->
<!-- </mirror>-->
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Override the repository (and pluginRepository) "central" from the Maven Super POM
to activate snapshots for both! -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>jdk1.8</activeProfile>
</activeProfiles>
</settings>
2.pom.xml配置
在pom.xml中插入发布相关信息
<!-- 配置私服上传jar包地址 -->
<distributionManagement>
<repository>
<!-- release包上传地址 -->
<id>maven-releases</id>
<url>http://192.168.163.136:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<!-- snapshots包上传地址 -->
<id>maven-snapshots</id>
<url>http://192.168.163.136:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
完成 , 可直接通过deploy发布到私服
查看私服是否有上传文件