一、安装
1、官网下安装包
官网地址:Nexus Repository OSS - Software Component Management | Sonatype
但是需要连接V*N。
2、scp方式copy到linux想要传的目录下
3、安装
解压:
tar -zxvf nexus-x.x.x-unix.tar.gz
cd到/bin目录下,启动nexus:
./nexus start
4、访问
http://服务器IP:8081
8081是默认的,可以在etc/nexus-default.properties中修改。
二、界面配置私有仓库
1、登录
nexus3以前默认是admin/admin123,nexus3的账户名还是admin,但是password在sonatype-work/nexus3/admin.password中。
2、Repository设置
1)创建私有仓库
2)选择maven2(proxy)
3)设置name,url,这里proxy设置阿里云镜像
http://maven.aliyun.com/nexus/content/repositories/central/
(阿里云镜像链接好像有多个,如果我这个不合适可以自行参考阿里云仓库配置:仓库服务)
4)再创建一个maven2(group)仓库组
设置members顺序
3、设置roles角色、users用户
1)创建角色
分配权限
2)创建用户,分配角色
可以退出admin,用创建的用户登录了。
四、settings文件修改,pom文件修改
1、setting文件设置
修改servers、修改mirrors
<servers>
<!--第一个nx-develop要和下面的mirror中的id一致,代表拉取是也需要进行身份校验-->
<server>
<id>nx-develop</id>
<username>develop</username>
<password>****</password>
</server>
<server>
<id>nexus-releases</id>
<username>develop</username>
<password>****</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>develop</username>
<password>****</password>
</server>
</servers>
<mirrors>
<!-- 私有仓库 -->
<mirror>
<id>nx-develop</id>
<url>http://x.x.x.x:8081/repository/poppy-group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
2、修改maven项目中的pom文件
因为是微服务项目,所以在parent的pom中加上distributionManagement,子项目就可以deploy jar包了。注意这个id需要有settings文件中server配置的一样。
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>corp nexus-releases</name>
<url>http://x.x.x.x:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>corp nexus-snapshots</name>
<url>http://x.x.x.x:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
五,配置Nexus在linux开机自启动
1、新建脚本(root用户)
vim /etc/init.d/nexus
2、添加脚本内容
#!/bin/bash
#chkconfig:2345 20 90
#description:nexus
#processname:nexus
export JAVA_HOME=/usr/local/jdk1.8.0_60
case $1 in
start) su root /usr/local/nexus-3.37.1-01/bin/nexus start;;
stop) su root /usr/local/nexus-3.37.1-01/bin/nexus stop;;
status) su root /usr/local/nexus-3.37.1-01/bin/nexus status;;
restart) su root /usr/local/nexus-3.37.1-01/bin/nexus restart;;
dump) su root /usr/local/nexus-3.37.1-01/bin/nexus dump;;
console) su root /usr/local/nexus-3.37.1-01/bin/nexus console;;
*) echo "Usage: nexus {start|stop|run|run-redirect|status|restart|force-reload}"
esac
注意写自己的nexus版本和java版本。
3、给脚本分权限
chmod 744 /etc/init.d/nexus
4、service快捷命令
#启动
service nexus start
#停止
service nexus stop
#重启
service nexus restart
#查看nexus的状态
service nexus status
5、配置开机自启动
#向chkconfig添加 nexus 服务的管理
chkconfig --add nexus
#设置nexus服务自启动
chkconfig nexus on
#关闭nexus服务自启动
chkconfig nexus off
#删除nexus服务在chkconfig上的管理
chkconfig –del nexus
至此,本地的私有仓库配置已完毕,install构建一下项目,看是否是从私服拉的jar包即可,基本没什么问题了。
但是我们的测试/生产环境是通过jenkins打包发布的,发现并不能成功,报下面的错误:
六、解决jenkins连接私服打包报错的问题
1、给jenkins配置settings.xml文件,两种方法
1)jenkins系统管理 -> Managed files(没有需要去下载插件) -> Add a new Config -> Maven settings.xml -> submit -> 填写相关setting内容
2)新建maven job -》构建环境 -》"Provide Configuration files"的file选项中选择自己定义的settings文件 -》 “build”中点击“高级...” -》在“使用自定义的工作空间”中settings file选项中选择“provided settings.xml” -》点击保存
3)缺点是需要每个job都要设置build。但是也可以设置全局,不过会影响其他不需要私服的job。
2、在jenkins所在的linux服务器上配置
vim /var/lib/jenkins/.m2/settings
这样jenkins全局使用的都是这个settings,不用每个job配置了。
/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven/conf
是jenkins默认的settings文件位置。
3、报错原因
是jenkins的maven版本过高,查看maven版本是3.8.2,maven3.8.2以上都会block掉了所有HTTP协议的repositories,所以会报错。
4、解决
1)降低maven版本,我就这么解决的,重新下了一个3.6.3的maven,给jenkins设置了一下。
2)用nginx代理将http的镜像地址映射成https的就可以。(嫌麻烦,没试,可行性需要实践)