私服介绍

私服是指私有服务器,是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构建。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

Nexus的安装

下载地址:https://www.sonatype.com/download-oss-sonatype 随便输入个邮箱,即可下载 然后下载相应的版本,我们这里使用的centos所以下载 Nexus Repository Manager OSS 3.x - Unix 地址已更新

提前在服务器上安装jdk环境(参考:安装配置java https://blog.51cto.com/4534309/2155016)

[root@bogon ~]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

建议在linux新建用户nexus(名字随便)

[root@bogon ~]# useradd nexus
[root@bogon ~]# passwd nexus
(centos7 默认创建组)
[root@bogon ~]# su - nexus
[nexus@bogon nexus]$mkdir nexus
[nexus@bogon ~]$ rz -y 
[nexus@bogon ~]$ ls -lrt
total 119236
-rw-r--r--. 1 nexus nexus 122093706 Jan 13 22:06 nexus-3.15.0-01-unix.tar.gz
[nexus@bogon ~]$ tar -zxvf nexus-3.22.1-02-unix.tar.gz  -C ./nexus && cd nexus
[nexus@bogon nexus]$ vim  nexus-3.22.1-02/etc/nexus-default.properties (修改端口号,这里面修改的是12306)
[nexus@bogon bin]$ cat /home/nexus/nexus/nexus-3.22.1-02/bin/nexus.vmoptions 
-Xms1200M
-Xmx1200M
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log
-XX:-OmitStackTraceInFastThrow
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc/karaf
-Djava.util.logging.config.file=etc/karaf/java.util.logging.properties
-Dkaraf.data=../sonatype-work/nexus3
-Djava.io.tmpdir=../sonatype-work/nexus3/tmp
-Dkaraf.startLocalConsole=false
[nexus@bogon bin]$ pwd
/home/nexus/nexus/nexus-3.15.0-01/bin
为了访问12306端口,这里简单粗暴

关闭firewalld
关闭防火墙:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

查看防火墙
firewall-cmd --state

关闭selinux
getenforce 查看状态
关闭 selinux  /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced. 激活
#     permissive - SELinux prints warnings instead of enforcing. 打印警告没有激活
#     disabled - No SELinux policy is loaded.没有激活
SELINUX=enforcing修改disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

启动nexus
[nexus@bogon bin]$ ./nexus  start
Starting nexus

启动成功后用浏览器登录http://ip:12306 我这里是:http://172.16.30.199:12306/ 看机器性能,有可能很慢,稍微等一下。我大概等了5分钟,以下基本一样

[nexus@bogon bin]$ lsof -i:12306
COMMAND    PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    115095 nexus  734u  IPv4 1793119      0t0  TCP *:12306 (LISTEN)
java    115095 nexus  738u  IPv4 1793121      0t0  TCP bogon:12306->bogon:56961 (ESTABLISHED)
java    115095 nexus  739u  IPv4 1793122      0t0  TCP bogon:12306->bogon:56962 (ESTABLISHED)
java    115095 nexus  740u  IPv4 1793123      0t0  TCP bogon:12306->bogon:56964 (ESTABLISHED)
java    115095 nexus  741u  IPv4 1793125      0t0  TCP bogon:12306->bogon:56965 (ESTABLISHED)
java    115095 nexus  742u  IPv4 1793126      0t0  TCP bogon:12306->bogon:56966 (ESTABLISHED)
java    115095 nexus  743u  IPv4 1793127      0t0  TCP bogon:12306->bogon:56967 (ESTABLISHED)

登录默认用户名admin,默认密码已经是一个文件了[/home/nexus/nexus/sonatype-work/nexus3/admin.password]

创建blob存储

开始创建私库 Repositories-->create repositories-->maven2(proxy)填写信息 create repositories-->maven2(hosted)填写信息 create repositories-->maven2(group)填写信息


本地配置setting.xml

  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
	<server>
      <id>CFCC</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers> 
   <mirrors>
		<mirror>
		<id>CFCC</id>
		<mirrorOf>*</mirrorOf>
		<name>CFCC</name>
		<url>http://192.168.37.3:12306/repository/CFCC</url>
	</mirror>
	
  </mirrors>

查看一下:

注意下面几点说明: 1.component name的一些说明: 1)maven-central:maven中央库,默认从https://repo1.maven.org/maven2/ 拉取jar 2)maven-releases:私库发行版jar 3)maven-snapshots:私库快照(调试版本)jar 4)maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。 2.Nexus默认的仓库类型有以下四种: 1)group(仓库组类型):又叫组仓库,用于方便开发人员自己设定的仓库; 2)hosted(宿主类型):内部项目的发布仓库(内部开发人员,发布上去存放的仓库); 3)proxy(代理类型):从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage Location属性的值即被代理的远程仓库的路径); 4)virtual(虚拟类型):虚拟仓库(这个基本用不到,重点关注上面三个仓库的使用); 3.Policy(策略):表示该仓库为发布(Release)版本仓库还是快照(Snapshot)版本仓库; 4.Public Repositories下的仓库 1)3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库,即第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去; 2)Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库 3)Central: 用来代理maven中央仓库中发布版本构件的仓库 4)Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库 5)Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库 6)Releases: 内部的模块中release模块的发布仓库,用来部署管理内部的发布版本构件的宿主类型仓库;release是发布版本; 7)Snapshots:发布内部的SNAPSHOT模块的仓库,用来部署管理内部的快照版本构件的宿主类型仓库;snapshots是快照版本,也就是不稳定版本 所以自定义构建的仓库组代理仓库的顺序为:Releases,Snapshots,3rd party,Central。也可以使用oschina放到Central前面,下载包会更快。 5.Nexus默认的端口是8081,可以在etc/nexus-default.properties配置中修改。 6.Nexus默认的用户名密码是admin/admin123 7.当遇到奇怪问题时,重启nexus,重启后web界面要1分钟左右后才能访问。 8.Nexus的工作目录是sonatype-work(路径一般在nexus同级目录下)