安装openjdk-11
安装MAVEN
安装nexus3
-
- 去官网下载最新的nexus压缩包
- 去官网下载最新的nexus压缩包
-
- 将压缩包上传到服务器
-
- 解压压缩包到/opt目录
cd /home/download
mkdir -p /opt/nexus
tar -zxvf nexus-3.69.0-02-java17-unix.tar.gz -C /opt/nexus/
解压完会有两个目录:
[root@localhost nexus]# ll
总用量 0
drwxr-xr-x. 10 root root 181 6月 10 08:28 nexus-3.69.0-02 # 包含了 Nexus 运行所需要的文件。是 Nexus 运行必须的
drwxr-xr-x. 3 root root 20 6月 10 08:28 sonatype-work # 包含了 Nexus 生成的配置文件、日志文件、仓库文件等。当我们需要备份 Nexus 的时候默认备份此目录即可
-
- 配置环境变量,并使其生效
vi /etc/profile
添加如下内容
#nexus3
export NEXUS_HOME=/opt/nexus/nexus-3.69.0-02
export PATH=$PATH:$NEXUS_HOME/bin
使环境变量生效
source /etc/profile
查看版本
nexus -version
-
- 修改默认启动用户
vi $NEXUS_HOME/bin/nexus.rc
内容就这一行,放开注释,填写用户即可
run_as_user="root"
警告:Sonatype Nexus: Recommended file descriptor limit is 65536 but count is 4096
解决办法
- 编辑
vi /etc/security/limits.conf- 添加代码:@root - nofile 65536
- 重启Nexus , 重启linux系统:reboot
-
- 修改启动端口
vi $NEXUS_HOME/etc/nexus-default.properties #默认就是8081,可以不修改
-
- 启动nexus并查看运行状态
[root@tcosmo-szls01 nexus]# nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Starting nexus
[root@tcosmo-szls01 nexus]# nexus status
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
nexus is running.
-
- 访问系统
访问http://ip:18088,登录账户admin的密码存放在/opt/nexus/sonatype-work/nexus3/admin.password目录下。
cat /opt/nexus/sonatype-work/nexus3/admin.password
-
- 使用系统服务启动
新建/etc/systemd/system/nexus.service文件
vi /etc/systemd/system/nexus.service
内容如下:
[Unit]
Description=Nexus Repository Manager
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/nexus-3.69.0-02/bin/nexus start
ExecStop=/opt/nexus/nexus-3.69.0-02/bin/nexus stop
User=root
Group=root
Restart=on-abort
Environment="JAVA_HOME=/opt/jdk-11"
[Install]
WantedBy=multi-user.target
启动:
systemctl start nexus
开机自启动:
systemctl enable nexus
Nexus3的使用
- 在maven配置中添加nexus的认证信息
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123456</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123456</password>
</server>
- 在项目的pom.xml中添加nexus的仓库地址,(如果是多module应用,可以直接在父pom里面配置)
<distributionManagement>
<repository>
<id>mynexus-releases</id>
<name>maven-releases</name>
<url>http://10.206.73.157:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>mynexus-snapshots</id>
<name>maven-snapshots</name>
<url>http://10.206.73.157:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
- 执行deploy命令,完成打包与推送
mvn clean deploy
# 如果像强行要求使用者更新本地依赖,可以 -U
4. 现在可以在maven-public中查看到我们推送的jar包
5. 在Maven项目中的使用
- 如果我们只有个别依赖需要从我们自己的nexus下载,那么只需要在项目的pom.xml文件中进行配置即可
<repositories>
<repository>`
<id>releases</id>
<name>maven-public</name>
<url>http://10.206.73.157:8081/repository/maven-public/</url>
</repository>
</repositories>
- 如果我们有很多依赖都需要从我们自己的nexus下载,那么可以通过修改maven的配置文件,setting.xml增加镜像来实现
<mirror>
<id>myNexus</id>
<mirrorOf>*</mirrorOf>
<name>myNexus</name>
<url>http://10.206.73.157:8081/repository/maven-public/</url>
</mirror>
但是,需要注意的是:
- 当maven的版本高于3.6之后,我们在项目pom.xml中是没有办法使用http协议的仓库地址的,会报错:
maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories。此时,我们要么使用https协议的仓库地址,要么在maven的setting.xml中进行配置; - 当maven的配置setting.xml中配置了多个mirror镜像的时候,是没有办法同时生效,依次检查的,只有第一个会生效;这很尴尬,因为我们一般会喜欢在setting.xml中配置阿里云的镜像仓库,不像完全替换成我们自己的。