文章目录
一、Nexus是什么?
Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问,也就是我们常说的私服
二、使用步骤
1.下载nexus3.x
Nexus官方下载地址
网盘下载:
链接:https://pan.baidu.com/s/1nu7HYjqanv68HE7fHlZFIA?pwd=zdhw
提取码:zdhw
2、上传nexus压缩包并解压
cd /opt
tar -zxvf nexus-3.25.1-04-unix.tar.gz
3、启动
cd /opt/nexus-3.25.1-04/bin/
./nexus run &
启动成功需要等待几分钟
启动遇到报错
解决(需要配置jdk8的环境)
修改在nexus对应的bin目录下的nexus文件:
INSTALL4J_JAVA_HOME_OVERRIDE=/opt/jdk1.8.0_212
4、开启端口号
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload
5、浏览器访问
默认8081端口
6、设置开机自启动
1.创建服务
vim /usr/lib/systemd/system/nexus.service
2.按i将下面复制进去
[Unit]
Description=nexus service
[Service]
Type=forking LimitNOFILE=65536 #警告处理
ExecStart=/opt/nexus-3.25.1-04/bin/nexus start
ExecReload=/opt/nexus-3.25.1-04/bin/nexus restart
ExecStop=/opt/nexus-3.25.1-04/bin/nexus stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
3.将服务加入开机启动
systemctl enable nexus.service
4.重新加载配置文件
systemctl daemon-reload
7、运行用户为root (编辑nexus bin下的nexus.rc)
cd /opt/nexus-3.25.1-04/bin/
vim nexus.rc
内容改为:
run_as_user="root"
8、修改nexus3启动时要使用的jdk版本
cd /data/nexus-3.25.1-04/bin/
vim nexus
按i将下面添加进去
INSTALL4J_JAVA_HOME_OVERRIDE=/data/jdk-11.0.16/
9、修改nexus3默认端口
文件在/opt/nexus-3.25.1-04/etc/nexus-default.properties
vim /opt/nexus-3.25.1-04/etc/nexus-default.properties
10、登录
默认用户名:admin
密码:admin123,如果默认密码不正确就去服务器中查找admin.password文件,密码在里面就是一串字符串。
[root@localhost data]# find -name admin.password
./sonatype-work/nexus3/admin.password
[root@localhost data]# cat ./sonatype-work/nexus3/admin.password
a3dffe94-17b2-4aea-bcc3-95d1ba126442
登录成功后按提示完成密码修
是否允许匿名访问:
三、私服新建自定义的仓库
步骤1:
点击Create repository,然后选择类型为 maven2(hosted)
步骤2:
输入仓库名称 private-release(自定义),并在 Version policy 一栏中选择 Release,表示这个仓库的存储依赖为正式发布的组件,然后在 Deployment policy 一栏中选择 Allow redeploy,允许部署和更新私服上的组件。
快照仓库的创建
四、添加角色和用户
1、添加角色
Nexus 默认只有两种角色:nx-anonymous 和 nx-admin,前者只有浏览的权限,后者为管理员权限,一般情况下,我们还需要正对开发人员创建个角色。点击 Create Role,添加一个 Role ID 为 developer 的自定义角色, 并且只添加自定义仓库的使用权限,删除操作除外
2、添加用户
点击 Create local user,填入用户名,密码等必填信息之后,关联我们先前的创建的角色即可,点击Create local user保存,创建成功。
五 使用 Maven 私服
搭建好了私服,创建完用户和角色之后,我们怎么使用呢?接着往下看。
1、修改服务端配置文件
在服务器上,打开 /opt/nexus-3.25.1-04/system/setting.xml (vim setting.xml)
cd /data/nexus-3.25.1-04/system
vim settings.xml
注意:这里添加 true 可以上传带SHAPSHOT的版本,也就是说可以上传到snapshot类型的库中,不加的话在idea中只能把SHAPSHOT后缀去掉,否则会报错
<settings>
<servers>
<server>
<id>buba-release</id>
<username>gyy</username>
<password>123456</password>
</server>
<server>
<id>buba-snapshot</id>
<username>gyy</username>
<password>123456</password>
</server>
</servers>
<profiles>
<profile>
<id>development</id>
<repositories>
<repository>
<id>buba-release</id>
<url>http://192.168.100.134:8081/repository/buba-release/</url>
<releases><enabled>true</enabled></releases>
<snapshots>false</snapshots>
</repository>
<repository>
<id>buba-snapshot</id>
<url>http://192.168.100.134:8081/repository/buba-snapshot/</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
</settings>
2、修改本地maven配置文件
在本地maven的配置文件中的标签下 ,添加连接私服的账号密码和ID配置如下
<server>
<id>buba-release</id>
<username>gyy</username>
<password>123456</password>
</server>
<server>
<id>buba-snapshot</id>
<username>gyy</username>
<password>123456</password>
</server>
3、修改idea中的POM文件
在Idea中POM文件中添加如下配置(这里的url,可以通过 Nexus 后台上仓库页面的 cpoy 按钮自动复制得到):
<distributionManagement>
<repository>
<id>buba-release</id>
<name>buba-release</name>
<url>http://192.168.100.134:8081//repository/buba-release/</url>
</repository>
<snapshotRepository>
<id>buba-snapshot</id>
<name>buba-snapshot</name>
<url>http://192.168.100.134:8081//repository/buba-snapshot/</url>
</snapshotRepository>
</distributionManagement>
到现在我们基本都配置完成了,执行部署命令 mvn clean deploy 或者利用 IDE 的 部署插件进行发布,当控制台出现 BUILD SUCCESS ,说明发布成功。
此时,可以在 Nexus 后台上仓库页面中查看我们发布的jar包。