Linux搭建nexus仓库

1.安装jdk

1.1 获取安装包,解压到指定目录:

1 tar xf jdk.tar.gz -C /opt/export

1.2 配置环境变量:

nexus搭建maven仓库管理_java
1 # vim /etc/profile
2 export JAVA_HOME=/opt/export/jdk
3 export PATH=$JAVA_HOME/bin:$PATH
4 export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
5 export RUN_AS_USER=root  # 后边启动nexus需要
6 
7 # source /etc/profile
nexus搭建maven仓库管理_java

1.3 出现下面结果,说明部署成功

1 # java -version
2 java version "1.7.0_80"
3 Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
4 Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

2.安装nexus

2.1下载安装

下载地址:

https://www.sonatype.com/download-oss-sonatype
https://help.sonatype.com/repomanager2/download/download-archives---repository-manager-oss

1 cd /opt
2 tar xf nexus-2.4.0-09-bundle.tar.gz

解压后有两个目录:

1 $ ls /opt/
2 nexus-2.4.0-09  sonatype-work

更改目录名称:

1 mv nexus-2.4.0-09 nexus

2.2 更改nexus配置文件

默认端口为8081,可根据需要修改:

 
 1 $ vim /opt/nexus/conf/nexus.properties
 2 # Jetty section
 3 application-port=8081  # 修改为10890
 4 application-host=0.0.0.0
 5 nexus-webapp=${bundleBasedir}/nexus
 6 nexus-webapp-context-path=/nexus
 7 
 8 # Nexus section
 9 nexus-work=${bundleBasedir}/../sonatype-work/nexus
10 runtime=${bundleBasedir}/nexus/WEB-INF

2.3 关闭防火墙或打开10890端口

1 /etc/init.d/iptables stop
2 chkconfig iptables off

2.4 启动nexus

1 $ /opt/nexus/bin/jsw/linux-x86-64/nexus start
2 ****************************************
3 WARNING - NOT RECOMMENDED TO RUN AS ROOT
4 ****************************************
5 Starting Nexus OSS...
6 Started Nexus OSS.

如果没有配置环境变量RUN_AS_USER=root,会报错:

1 # /opt/nexus/bin/jsw/linux-x86-64/nexus start
2 ****************************************
3 WARNING - NOT RECOMMENDED TO RUN AS ROOT
4 ****************************************
5 If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.

2.5 检查进程和端口 

nexus搭建maven仓库管理_jar_03

 3.浏览器访问nexus

1 http://ip地址:8081/nexus
2 登录,默认用户名 admin 默认密码 admin123

 访问登录:

nexus搭建maven仓库管理_Java_04

4.迁移nexus

如果想要将nexus仓库迁移,只需要打包此目录,迁移到新nexus主机:

1 $ du -sh /opt/sonatype-work/nexus/storage
2 47G    /opt/sonatype-work/nexus/storage/

由于文件过大,可采用rsync的方式。

 

Nexus高可用方案

描述:由于nexus进程会因为某些原因挂掉,为了不影响使用,决定做nexus高可用。

准备:根据上一章,准备两台服务器搭建nexus,主:192.168.51.204 maven01,备:192.168.51.207 maven02:

1.搭建keepalived

主:

 
 1 # cat /etc/keepalived/keepalived.conf
 2 ! Configuration File for keepalived
 3 
 4 global_defs {
 5    router_id maven01
 6 }
 7 vrrp_script chk_http_port {              #检测nexus端口是否存在,不存在进行vip漂移
 8     script "</dev/tcp/127.0.0.1/10890"
 9     interval 1
10     weight -30
11     fall 1
12     rise 1
13 }
14 vrrp_instance VI_1 {
15     state MASTER
16     interface eth0
17     virtual_router_id 88
18     priority 150
19     advert_int 1
20     authentication {
21         auth_type PASS
22         auth_pass 1111
23     }
24     virtual_ipaddress {
25         192.168.51.210
26     }
27     track_script {
28         chk_http_port
29     }
30 }
 

备:

 1 # cat /etc/keepalived/keepalived.conf
 2 ! Configuration File for keepalived
 3 
 4 global_defs {
 5    router_id maven01
 6 }
 7 vrrp_script chk_http_port {
 8     script "</dev/tcp/127.0.0.1/10890"
 9     interval 1
10     weight -30
11     fall 1
12     rise 1
13 }
14 vrrp_instance VI_1 {
15     state MASTER
16     interface eth0
17     virtual_router_id 88
18     priority 100
19     advert_int 1
20     authentication {
21         auth_type PASS
22         auth_pass 1111
23     }
24     virtual_ipaddress {
25         192.168.51.210
26     }
27     track_script {
28         chk_http_port
29     }
30 }
 

2.同步数据

描述:对比同步数据方式,1⃣️定时任务rsync同步:不及时,容易遗漏数据。2⃣️nfs共享:能够解决数据一致问题,但是主一旦宕机,备库起不到任何作用。3⃣️sersync或者inotify+rsync:可以实现实时同步,最后选用inotify方式。

2.1 部署rsync

2.1.1 备:部署rsync服务端

安装rsync软件

1 yum install -y rsync

编写配置文件

 
 1 $ cat /etc/rsyncd.conf
 2 #created by yjn at 2018
 3 
 4 uid = rsync
 5 gid = rsync
 6 use chroot = no
 7 max connections = 10
 8 strict modes = yes
 9 pid file = /var/run/rsyncd.pid
10 lock file = /var/run/rsync.lock
11 log file = /var/log/rsyncd.log
12 
13 [nexus]
14 path = /opt/sonatype-work/nexus/storage
15 comment = "nexus backup dir"
16 ignore errors
17 read only = no
18 write only = no
19 hosts allow = 192.168.0.0/16
20 auth users = rsync_backup
21 secrets file = /etc/rsync.password
 

创建备份目录的管理用户

1 useradd -s /sbin/nologin -M rsync

创建安全认证文件

1 echo "rsync_backup:123" >/etc/rsync.password
2 chmod 600 /etc/rsync.password

修改备份目录属主

1 chown -R rsync.rsync  /opt/sonatype-work/nexus/storage

启动rsync服务

1 rsync --daemon
2 说明:rsync服务的端口号为873端口(tcp)

2.1.2 主:部署rsync客户端

安装rsync软件

1 yum install -y rsync

创建安全认证文件

1 echo "123" >/etc/rsync.password
2 chmod 600 /etc/rsync.password

2.2 部署inotify-tools软件

inotify软件的参考资料链接:https://github.com/rvoicilas/inotify-tools/wiki

 
1 # yum install -y inotify-tools
2 Loaded plugins: fastestmirror, security
3 Setting up Install Process
4 Loading mirror speeds from cached hostfile
5  * base: mirrors.zju.edu.cn
6  * extras: mirror.bit.edu.cn
7  * updates: mirrors.tuna.tsinghua.edu.cn
8 No package inotify-tools available.
9 Error: Nothing to do
 

没有这个包,更新epel源:

1 yum install -y epel-release && yum update

2.3 inotify+rsync结合脚本同步nexus

2.3.1 写同步脚本

 
#!/bin/bash
###########

inotifywait -mrq /opt/sonatype-work/nexus/storage  --format '%w%f'  -e create,delete,close_write,moved_to|\
while read line
do
  rsync -az --delete  /opt/sonatype-work/nexus/storage/*  rsync_backup@192.168.51.207::nexus --password-file=/etc/rsync.password &>/dev/null
done
 

2.3.2 后台执行:

1 sh /yjn/scripts/backup.sh &

2.3.3 发现报错:

1 Failed to watch /opt/sonatype-work/nexus/storage; upper limit on inotify watches reached!
2 Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.

  注意:inotify默认监控同步文件的个数是有限制的,8192,通过配置文件/proc/sys/fs/inotify/max_user_watches可以调整监控的个数。此问题明显就是文件太多,导致无法监控。

 2.3.4 解决:

1 echo 8192000 > /proc/sys/fs/inotify/max_user_watches

此时再执行脚本,没有报错信息,验证可以同步。

 

-------- 

 

一 前提准备

1 maven环境 3.5+,java环境 1.8+。

##maven环境变量


M2_HOME ##创建maven环境变量
F:\Mysoft\maven\apache-maven-3.5.2-bin\apache-maven-3.5.2 ##安装路径 bin文件夹父目录

%M2_HOME%\bin; ##加入path
2 安装好nexus3 window安装nexus(maven私服)

二 实现目标

1 配置maven本地仓库

2 配置远程仓库

名称 类型 作用
仓库组(group) maven2-group 虚拟仓库组,用于集中管理仓库
第三方依赖仓库(3dr) maven2-hosted 本地仓库,用于管理公网无资源的第三方jar,例如oracle驱动
稳定发行仓库(releases) maven2-hosted 本地仓库,用于管理稳定发行的jar
内测快照仓库(snapshots) maven2-hosted 本地仓库,用于管理内测发行的jar
代理中央仓库(proxy) maven2-proxy 代理中央仓库,用于指定公网仓库地址,例如指定阿里云maven中央仓库
3 配置使用maven私服,以及发布下载jar

三 架构图

1、结构图

 

如图:为maven-nexus-proxymaven的交互架构图
蓝色虚线为使用外网代理私服的路线
红色虚线为使用内网资源转移下载路线


##外网

maven本地仓库 指定本地位置缓存下载的jar 默认为官方中央仓库 可配置为私服代理(阿里私服)
nexus私服仓库 通过代理仓库下载缓存jar 提供给局域网内各个maven资源
maven本地仓库 可以通过发布jar到nexus私服进行管理 提供给其他用户使用

##内网

maven本地仓库 指定本地位置缓存下载的jar 必须配置私服 否则下载时超时
nexus私服仓库 通过代理仓库下载缓存jar 如果内网有穿透的机子可以使用代理,若无则要通过存储媒介在外网下载jar后 内网上传至nexus私服仓库 提供给局域网内各个maven资源
maven本地仓库 可以通过发布jar到nexus私服进行管理 提供给其他用户使用
2 部署图

 

##根据开发规范细分私服如上

1、3rd 私服本地库:用于存放三方包,包括oracle驱动,公司外部支持jar
2、releases 私服本地库:用于存放稳定版本的jar 内网环境下可以上传外网下载的jar
3、snapshots 私服本地库:用于存放内测版本的jar 可以设置更新策略为实时
4、proxy 私服代理库:用于存放外网中央仓库地址 内网环境下一般不存在
5、group 私服公共库:将多个库虚拟成一个库 供方便引用和管理

注意:开发上传外网资源可以存在稳定版本release库,则公司内部jar在内网中如同外网jar
开发正在开发的小版本库可以上传内测snapshot库,一般情况使用svn去管理
开发使用的公司外部jar,需要上传3rd库,以便其他用户下载
开发下载插件和依赖jar均使用公共库group即可
 四 配置nexus

1、检测maven环境

mvn -v ##需要配置 JAVA_HOME,M2_HOME


2、启动nexus并登陆

http://localhost:8082/ ##nexus服务ip:port 账号密码默认 admin admin123
 

如果不修改默认有4个库  1个proxy,1个group,1个release和1个snapshot,3rd在低版本有 nexus3默认没有了 

如果需要使用,需要按实际情况配置,一下可选为*

 

(1)* 配置仓库 

可点击新建仓库(略)

 

(2)* 配置proxy

点击maven-central 进入配置

配置外网代理maven仓库,例如 http://maven.aliyun.com/nexus/content/groups/public/ 阿里云

 

选择缓存文件位置,默认只有一个在nexus安装路径 

 

(3)配置maven-snapshot和maven-release

启用maven-snapshot与maven-release发布功能 (snapshot修改相同)

 

(4)*配置maven-public仓库  

可以将现有仓库聚合

 

3、*配置用户

用户默认为admin,admin123

 

(1)* 新增一个zhangsan

 

五 配置maven

(如果配置在maven setting中则为全局配置  如果配置为项目pom中则为项目配置)

1、修改maven setting.xml配置

##文件坐标
F:\mysoft\apache-maven-3.5.2-bin\apache-maven-3.5.2\conf\setting.xml
 关键配置


<!--maven 私服管理配置-->
<servers>
<server>
<id>maven-releases</id>
<!--保持id唯一 用于引用-->
<username>admin</username>
<password>admin123</password>
<!--这个密码就是你设置的密码-->
</server>
<server>
<id>maven-snapshots</id>
<!--保持id唯一 用于引用-->
<username>admin</username>
<password>admin123</password>
<!--这个密码就是你设置的密码-->
</server>
</servers>
详细配置(可省略)

<!--maven本地仓库配置-->
<localRepository>F:\mysoft\apache-maven-3.5.2-bin\maven_repository</localRepository>


<!--maven 私服管理配置-->
<servers>
<server>
<id>maven-releases</id>
<!--保持id唯一 用于引用-->
<username>admin</username>
<password>admin123</password>
<!--这个密码就是你设置的密码-->
</server>
<server>
<id>maven-snapshots</id>
<!--保持id唯一 用于引用-->
<username>admin</username>
<password>admin123</password>
<!--这个密码就是你设置的密码-->
</server>
</servers>


<!--以下是中央仓库配置 如同使用proxy指定下载点 再无私服公共仓库时直接到中央仓库下载-->
<mirrors>
<!--以下是默认配置,原本为注释-->
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
<!--可以新增 配置私服公共仓库 配置阿里云公共仓库-->
</mirrors>


<!--以下是一般不配置 因为项目多个情况下还是使用pom文件配置最佳-->
<!--动态配置参数-->
<profiles>
<!--各个环境配置-->
<profile>
<id>env-test</id>
<!--省略 仓库分类指定,版本库刷新等,详见pom.xml-->
</profile>
<profile>
<id>env-dev</id>
</profile>
</profiles>

<!--激活配置参数-->
<activeProfiles>
<activeProfile>env-dev</activeProfile>
</activeProfiles>
2、修改项目pom.xml文件 (父pom即可)

(1)配置下载使用maven公共库maven-group

<!--仓库配置 如同maven 配置镜像-->
<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://localhost:8082/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<!--插件库配置 -->
<pluginRepositories>
<pluginRepository>
<id>mmaven-public</id>
<name>maven-public</name>
<url>http://localhost:8082/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
(2)配置发布到maven本地库 maven-releases以及maven-snapshots

<!--配置仓库管理-->
<distributionManagement>
<!--设置发布稳定仓库-->
<repository>
<id>maven-releases</id>
<!--id必须与setting中对应 使用setting配置用户-->
<name>User Project Release</name>
<url>http://localhost:8082/repository/maven-releases/</url>
</repository>
<!--设置发布的快照库-->
<snapshotRepository>
<id>maven-snapshots</id>
<!--id必须与setting中对应 使用setting配置用户-->
<name>User Project SNAPSHOTS</name>
<url>http://localhost:8082/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
(3)使用profile(可省略)

<profiles>
<profile>
<id>env-dev</id>
<!--命名唯一-->
<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://localhost:8082/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://localhost:8082/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>User Project Release</name>
<url>http://localhost:8082/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://localhost:8082/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</profile>
<!--以下省略其他profile-->
</profiles>


六 使用(以idea为例)

(1)配置使用maven 

 file--》setting--》maven--》指定安装的maven

 

(2)使用maven工具

ctrl+shift+A   输入  maven projects  点击则右边侧边栏出现maven project管理工具

 

 

1、下载远程仓库文件

 

2、发布release

 

##
注意release版本发布 版本号必须不能以snapshot结尾


3、上传第三方文件

语法:
mvn deploy:deploy-file
-DgroupId= <group-id>\包名前缀 com.公司名
-DartifactId= <artifact-id>\包项目名 xxx项目
-Dversion= <version>\包版本号 v1.0.0.1(纯数字最佳)
-Dpackaging= <type-of-packaging>\打包类型 一般为jar
-Dfile= <path-to-file>\需要上传文件的路径
-DrepositoryId= <id-to-map-on-server-section-of-settings.xml>\serverid(setting指定)
-Durl= <url-of-the-repository-to-deploy>

例子:

mvn deploy:deploy-file -DgroupId=com.oracle
-DartifactId=jdbc -Dversion=1.0 -Dfile=ojdbc6.jar
-DrepositoryId=maven-release -Durl=http://ip:prot/repository/maven-releases/

---------------------
版权声明:本文为「MarsAres」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。