第04章 CentOS7 中 Sonatype Nexus Repository Manager3 的安装与配置

第04章 CentOS7 中 Sonatype Nexus Repository Manager3 的安装与配置

— 大神的视角,码农的温度 —

全文 2299 字,菜鸟18分钟,初级10分钟,中级5分钟,大神请绕道。

本章目的

本章介绍 Nexus Repository 依赖库私有库的搭建

  • 完成私有库的搭建
  • Sonatype Nexus Repository 的安装
  • Sonatype Nexus Repository 的配置
  • 统一管理开发过程中需要依赖的第三方库包
  • 为快速开发提供基础支撑

文章目录

2.1 Sonatype 简单介绍

2.1.1 发布更快,控制开源风险。

2.1.1 Nexus Repository

Nexus 自动化开源治理,以降低风险并加速软件创新。

DevSecOps创新的开源治理,以DevOps的速度管理开源安全性。

Nexus存储库,可用作开发组织的私有库或公用库,统一存储和管理开发组织用到的所有构建的分发。

对所有您喜欢的格式和工具的通用支持。 存储和分发Maven / Java,npm,NuGet,RubyGems,Docker,P2,OBR,APT和YUM等。 从开发到交付管理组件:二进制文件,容器,程序集和成品。 对Java虚拟机(JVM)生态系统的强大支持,包括Gradle,Ant,Maven和Ivy。 集成了流行的工具,如Eclipse,IntelliJ,Hudson,Jenkins,Puppet,Chef,Docker等。

2.1.2 Sonatype 产品提供的版本

提供了两个版本,分别是Nexus Repository OSS 和 Nexus Repository Pro。OSS 属于免费版,世界上第一个也是唯一一个免费使用的通用存储库解决方案。 Pro是属于专业版,也是商业版,但可以免费试用。两个版本有功能性差异,但都是在统一的体系架构之下的产品。免费版的基础功能对一般要求不高的科技公司来说足够使用。两个产品版本的具体区别可查看官方文档。

扫描二维码关注公众号,回复: 11609484 查看本文章

我们这里选用的是 OSS 的免费版作为在 CentOS 7.x 下的安装和配置的重点说明。

2.1.3 想要从2.x升级到3的同学可以请参考这里

Upgrading Repository Manager 2 to 3

If you’re upgrading from Repository Manager 2.x, you must first upgrade your installation to 2.14.5. See the upgrade compatibility matrix for more information.

1000万开发人员的选择 **官网地址:**https://www.sonatype.com/

2.2 Nexus 安装部署

2.2.1 Nexus Repository OSS 安装说明

Unix的发行版不包括运行时环境。如果您更喜欢使用外部运行时或使用Unix操作系统,建议使用Oracle网站提供的最新版本的Java 8

安装和运行Nexus Repository Manager非常简单。您可以将存档解压缩到您具有完全访问权限的目录中,也可以使用Docker映像进行安装。

Nexus Repository Manager需要Oracle的Java 8运行时环境(JRE)。 OSX和Windows的发行版包含适用于特定操作系统的运行时环境。 Unix的发行版不包括运行时环境。如果您更喜欢使用外部运行时或使用Unix操作系统,建议使用Oracle网站提供的最新版本的Java 8。您可以选择仅安装完整的JDK或JRE。您可以使用java -version命令确认已安装的Java版本:

官方网站地址:

https://www.sonatype.com/

Nexus Repository manager 3 官方文档地址:

https://help.sonatype.com/repomanager3

2.2.2 Nexus Repository OSS 安装

进入 /opt 目录,在目录下新建 nexus 目录,将安装包下载和解压该目录

[root@localhost nexus]# wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.12.1-01-unix.tar.gz
[root@localhost nexus]# tar zxvf nexus-3.12.1-01-unix.tar.gz
[root@localhost nexus]# ll   ## 查看解压后的目录
drwxr-xr-x 9 root root 4096 Jul  9 02:45 nexus-3.12.1-01
drwxr-xr-x 3 root root 4096 Jul  9 02:45 sonatype-work

2.2.3 安装目录说明

解压后在 nexus 目录下多了两个目录,一个是 nexus-3.12.1-01,一个是 sonatype-work

1、nexus-3.12.1-01

应用程序目录,提供 WEB 应用服务,仓库配置、私有库构建的上传和管理、检索和所有对私有中央仓库的管理、权限配置等功能。包含Nexus Repository Manager应用程序和所有必需的附加组件,如Java库和配置文件。

[root@localhost etc]# ll /opt/nexus/nexus-3.12.1-01/
-rw-r--r--  1 root root 39222 Jun  8 09:46 LICENSE.txt ## 有关许可和版权声明的法律详细信息
-rw-r--r--  1 root root   395 Jun  8 09:46 NOTICE.txt ## 有关许可和版权声明的法律详细信息
drwxr-xr-x  3 root root  4096 Jul  9 02:45 bin ## 包含nexus启动脚本本身以及与启动相关的配置文件
drwxr-xr-x  2 root root  4096 Jul  9 02:45 deploy
drwxr-xr-x  7 root root  4096 Jul  9 05:56 etc  ## 包含配置文件
drwxr-xr-x  4 root root  4096 Jul  9 02:45 lib ## 包含与Apache Karaf(OSGI容器)相关的二进制库
drwxr-xr-x  3 root root  4096 Jul  9 02:45 public ## 含应用程序的公共资源
drwxr-xr-x 21 root root  4096 Jul  9 02:45 system ## 包含构成应用程序的所有组件和插件
2、 sonatype-work

数据目录,提供构建库索引、构建库、第三方构建库的位置,要确保有写的权限。是真正所有构建的仓库。包含存储库管理器提供的所有组件,存储库,配置和其他数据。 相对于安装目录,数据目录的默认位置是…/sonatype-work/nexus3。

[root@localhost etc]# ll /opt/nexus/sonatype-work/nexus3/
drwxr-xr-x 54 root root 4096 Jul  9 10:23 cache  ## 包含有关当前缓存的Karaf捆绑包的信息
drwxr-xr-x  2 root root 4096 Jul  9 10:23 etc
drwxr-xr-x  2 root root 4096 Jul  9 10:23 generated-bundles
drwxr-xr-x  2 root root 4096 Jul  9 10:23 instances
-rw-r--r--  1 root root   26 Jul  9 10:23 lock
drwxr-xr-x  2 root root 4096 Jul  9 10:23 log
drwxr-xr-x  3 root root 4096 Jul  9 10:19 orient
-rw-r--r--  1 root root    5 Jul  9 10:23 port
drwxr-xr-x  3 root root 4096 Jul  9 10:23 tmp

官方下载地址:

https://help.sonatype.com/repomanager3/download

官方安装文档:

https://help.sonatype.com/repomanager3/installation

2.3 Nexus Repository OSS 配置

配置存储库管理器运行时的细节涉及配置各种配置文件和启动脚本中的所有组件。本节详细介绍了这些内容并提供了特定任务的配置方法。

2.3.1 nexus.vmoptions 文件配置

这个文件的配置可以根据集群的实际资源进行修改,默认可以不需要修改。

运行存储库管理器的 JVM 的启动是通过安装在 $ install-dir/bin 目录中的文件来管理的。使用文件$ install-dir/bin/nexus.vmoptions 中的 JVM 配置执行应用程序启动:

-Xms1200M ## 分配堆最小内存,默认为物理内存的1/64
-Xmx1200M ## 分配最大内存,默认为物理内存的1/4
-XX:MaxDirectMemorySize=2G
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-XX:+LogVMOutput
-XX:LogFile=../sonatype-work/nexus3/log/jvm.log
-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

ByteBuffer有两种:

heap ByteBuffer -> -XX:Xmx

1.一种是heap ByteBuffer,该类对象分配在JVM的堆内存里面,直接由Java虚拟机负责垃圾回收,

direct ByteBuffer -> -XX:MaxDirectMemorySize

2.一种是direct ByteBuffer是通过jni在虚拟机外内存中分配的。通过jmap无法查看该快内存的使用情况。只能通过top来看它的内存使用情况。

JVM堆内存大小可以通过-Xmx来设置,同样的direct ByteBuffer可以通过-XX:MaxDirectMemorySize来设置,此参数的含义是当Direct ByteBuffer分配的堆外内存到达指定大小后,即触发Full GC。注意该值是有上限的,默认是64M,最大为sun.misc.VM.maxDirectMemory(),在程序中中可以获得-XX:MaxDirectMemorySize的设置的值。

MaxDirectMemorySize 说明的原文出处:https://blog.csdn.net/u010003835/article/details/52957904

2.3.2 官方内存配置推荐

2.3.2.1 General Memory Guidelines

  • set minimum heap should always equal set maximum heap
  • minimum heap size 1200MB
  • maximum heap size <= 4GB
  • minimum MaxDirectMemory size 2GB
  • minimum unallocated physical memory should be no less than 1/3 of total physical RAM to allow for virtual memory swap
  • max heap + max direct memory <= host physical RAM * 2/3

2.3.2.2 Instance Sizing Profiles

Here are instance profiles you can use to gauge the typical physical memory requirements needed for a dedicated server host running repository manager. Due to the inherent complexities of use cases, one size does not fit all and this should only be interpreted as a guideline.

Profile Use Case Physical Memory RAM
small, personalrepositories < 20 total blobstore size < 20GB single repository format type 4GB minimum
medium, teamrepositories < 50 total blobstore size < 200GB a few repository formats 8GB
large, enterpriserepositories > 50 total blobstore size > 200GB diverse set of repository formats 16GB+

2.3.2.3 最大内存配置参考

Physical Memory Example Maximum Memory Configuration
4GB -Xms1200M -Xmx1200M -XX:MaxDirectMemorySize=2G
8GB -Xms2703M -Xmx2703M -XX:MaxDirectMemorySize=2703M
12GB -Xms4G -Xmx4G -XX:MaxDirectMemorySize=4014M
16GB -Xms4G -Xmx4G -XX:MaxDirectMemorySize=6717M
32GB -Xms4G -Xmx4G -XX:MaxDirectMemorySize=17530M
64GB -Xms4G -Xmx4G -XX:MaxDirectMemorySize=39158M

2.3.3 官方 Disk 配置推荐

Nexus Repository Manager 3 installed consumes around 500 MB. The bulk of disk space will be held by your deployed and proxied artifacts, as well as any search indexes. This is highly installation specific, and will be dependent on the repository formats used, the number of artifacts stored, the size of your teams and projects, etc. It’s best to plan for a lot though, formats like Docker and Maven can use very large amounts of storage (500Gb easily).

2.3.3.1 File Systems to avoid

File systems known to be unreliable are:

  1. glusterfs
  2. FUSE based user space filesystems

Additionally, we strongly recommend to avoid using NFS and Amazon EFS for anything other than blob storage in Nexus Repository 3.x, especially in large installations, as this can cause severe performance degradation. If NFS is used for blob storage we recommend to only use NFSv4, NFSv3 is known to provide inadequate performance. We also have some optimization suggestions to use at your discretion. Also consider the noatime option for your Nexus Repository work directory mounts and limit the symbolic links used as this will cause increased overhead whenever paths need to be resolved to an absolute file path.

2.3.4 官方 Web Browser 使用版本推荐

Our general policy is to support the most recent modern browser version for your supported OS at time of NXRM release date. This table is updated for the most recent NXRM release.

Vendor Versions
Google Chrome latest at NXRM release
Firefox latest and ESR at NXRM release
Safari on OSX only, latest at NXRM release
Opera untested and not supported
Internet Explorer 9, 10, 11 - Check this article for known issues
Microsoft Edge latest at NXRM release

官方文档地址:https://help.sonatype.com/repomanager3/system-requirements#SystemRequirements-GeneralMemoryGuidelines

Orient 数据库内存优化(Optimizing OrientDB Database Memory) https://support.sonatype.com/hc/en-us/articles/115007093447?_ga=2.45285988.1363574621.1531384407-796464375.1531102798

2.3.5 nexus.properties 文件配置

修改 nexus-default.properties配置文件。如果没有特殊需要也可以不修改。可以改端口和上下文,我这里就修改一下应用服务的上下文。

应用程序的默认属性文件,提供默认值,例如用于HTTP和HTTPS访问的端口,以及上下文路径和主机。覆盖 $data-dir/etc/nexus.properties 中的这些默认值。不应修改此文件。

[root@localhost etc]# cp nexus.properties nexus.properties.bak ## 修改之前备份一份
[root@localhost etc]# vi nexus.properties
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081     ## 端口
application-host=0.0.0.0  ## 任意 IP 地址
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/nexus     ## 上下文

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

2.3.6 etc目录下文件配置介绍

2.3.6.1 子目录和配置文件介绍

[root@localhost etc]# pwd
/opt/nexus/nexus-3.12.1-01/etc
[root@localhost etc]# ll
drwxr-xr-x 2 root root 4096 Jul  9 02:45 fabric ## Ehcache,Elasticsearch和OrientDB的配置文件
drwxr-xr-x 2 root root 4096 Jul  9 02:45 jetty ## Eclipse Jetty的配置文件
drwxr-xr-x 2 root root 4096 Jul  9 02:45 karaf ## Apache Karaf的配置文件,详见2.3.2.2节介绍
drwxr-xr-x 2 root root 4096 Jul  9 02:45 logback ## 用于logback的配置文件
-rw-r--r-- 1 root root  341 Jun  8 09:46 nexus-default.properties ## 见下面说明
drwxr-xr-x 2 root root 4096 Jul  9 02:45 ssl ## 配置HTTPS时放置密钥库的目录

2.3.6.2 子目录 karaf 配置文件介绍

config.properties - Apache Karaf运行时的主要配置。不应修改此文件。
custom.properties - Apache Karaf使用的可自定义配置。此文件可用于将其他参数传递给Apache Karaf容器。
org.apache.* and org.ops4j.* - 各种Karaf和OSGi相关的配置文件
system.properties - 用于JVM和应用程序启动的系统属性配置文件

2.3.7 HTTPS 配置

  • 上传你自己的 ssl 证书到以下位置
  • 修改 jetty-https.xml 配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PmLkNA3T-1599485477106)(assets/1533027402472.png)]

  • 最后配置 Nginx 重定向即可
server {
        listen       80;
        server_name  你的域名;
        index index.html index.htm index.php;
        server_name_in_redirect off;
        rewrite ^/(.*)$ https://你的域名/$1 permanent;
        access_log  /usr/local/nginx/access/nexus3.log;
}

### HTTPS server
server {
    listen 443;
    server_name 你的域名;
    ssl on;
    index index.html index.htm;
    ssl_certificate   你的证书所路径 .pem;
    ssl_certificate_key  你的证书路径 .key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass    https://127.0.0.1:4445;

    }

    access_log  /usr/local/nginx/access/nexus3_ssl.log;

}

2.4 访问 Nexus Repository Manager 服务

2.4.1 启动服务

1、进入到 nexus bin 目录

[root@localhost bin]# cd /opt/nexus/nexus-3.12.1-01/bin 
[root@localhost bin]# pwd
/opt/nexus/nexus-3.12.1-01/bin 
[root@localhost bin]# ./nexus run  ## 启动 nexus 服务,其它命令:start, stop, restart,

2、查看端口

[root@localhost bin]# netstat -ntlp ## 列出所有端口
[root@localhost bin]# netstat -nap #会列出所有正在使用的端口及关联的进程/应用
[root@localhost bin]# firewall-cmd --list-ports ## CentOS 7 查看防火墙端口

3、修改防火墙

## 打开 8081 端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=8081/tcp --permanent 
[root@localhost ~]# firewall-cmd --reload ## 重启防火墙

firewall 命令说明:
–zone #作用域
–add-port=8081/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

4、启动日志出现警告

这个警告是不推荐使用 root 用户执行,为了安全,可以换一个普通用户安装执行。


WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************

CentOS下实现普通用户和管理团员权限的切换
使用su命令实现用户属性的转换
普通用户切换到root用户:方法有多种  
1)su->回车->输入root密码
2)su -root->回车->输入root密码
root用户切换到普通用户:su "普通用户名"

官方提示:

Important(重要提示)

As a security precaution, do not run Nexus Repository Manager 3 as the root user.

作为安全预防措施,请不要以root用户身份运行Nexus Repository Manager 3。

2.4.2 访问服务

访问 Nexus Repository 服务首页

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p7jgn5hb-1599485477109)(assets/1531312375252.png)]

 默认用户账号是:admin
 默认用户密码是:admin123  

登录后可以切换到 Repository 管理

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Wy51iF9K-1599485477111)(assets/1531312337117.png)]

2.4.2.1 页面警告信息

System Requirement: max file description [4096] likely too low, Increase to at least[65536]

解决页面这个警告信息的方法:

## 打开系统文件
[root@localhost ~]# /etc/security/limits.conf
## 在文件中增加两项配置
 *               soft    nofile          65536  ## *表示任意用户,可以指定用户
 *               hard    nofile          65536

2.4.3 管理仓库

安装完成后,通过默认用户账号密码登录后台,可以修改用户密码,开始对 nexus 私服仓库进行全部管理了,比如用户管理、授权、配置等操作。包括第三方 jar 包的维护和管理。

相对 2.x 后台管理的功能更强大、更丰富。

2.5 nexus 开机自启动服务设置

2.5.1 创建 nexus 服务文件

[root@localhost ~]# vim /usr/lib/systemd/system/nexus.service 
## 为这个文件授权 754 权限
[root@localhost ~]# chmod 754 /usr/lib/systemd/system/nexus.service 

注意:创建的服务文件必须在这个目录下:/usr/lib/systemd/system/

2.5.2 编辑服务文件配置

在创建的服务文件中添加如下配置:

[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/nexus-3.12.1-01/bin/nexus start
ExecStop=/opt/nexus/nexus-3.12.1-01/bin/nexus stop
# User=nexus
Restart=on-abort

[Install]
WantedBy=multi-user.target

servcie 配置说明:

[Unit]:服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
LimitNOFILE 要将nexus用户的软限制和硬限制的最大打开文件数
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户

2.5.3 修改 nexus 脚本文件

打开 nexus 脚本文件

[root@localhost ~]# vim /opt/nexus/nexus-3.12.1-01/bin/nexus

编辑脚本文件,看注释,重写脚本文件中的 JAVA_HOME 配置。(见下面注释位置)

# description:       nexus
### BEGIN INIT INFO
# Provides:          nexus
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nexus
### END INIT INFO

# Uncomment the following line to override the JVM search sequence
INSTALL4J_JAVA_HOME_OVERRIDE="/opt/java/jdk1.8.0_172"  ## 这个注释打开,配置你自己的 JAVA_HOME
# Uncomment the following line to add additional VM parameters
# INSTALL4J_ADD_VM_PARAMS=


INSTALL4J_JAVA_PREFIX=""
GREP_OPTIONS=""

read_db_entry() {
...

注意点

为什么修改这个脚本文件?
因为我在做开机自启动服务的时候,通过命令 systemctl start nexus 启动 nexus 服务启动不起来,会报各种莫名其妙的错误。通过命令查看 systemctl status nexus.service(查看服务启动情况) 和 journalctl -xe (查看系统日志)。

1、提示 java 版本太低的错位。(The version of the JVM must be at least 1.8 and at most 1.8.)

我的判断是通过系统命令来启动服务调用这个脚本,不能自动获取到 JAVA 环境变量,所以要修改这个脚本文件,不让脚本自动去获取了,我自己来指定 JAVA_HOME 的位置。修改后问题解决。

2、提示 警告信息 Received signal: SIGTERM 和 Lowering disk cache size from 4,096MB to 2,046MB.

这些信息不能具体判断到底是哪里出了问题。最后通过 nexus 启动日志 nexus.log 中的警告信息判断是内存不足的问题。因此调大了虚拟内存到 6 GB,我之前分配的是 4 GB 内存。

重新启动就不报各种错误了,通过系统命令启动成功。

2.5.4 设置 nexus 服务开机自启动

使用 systemctl 命令设置 nexus 服务开机自启动。

[root@localhost ~]# systemctl enable nexus.service

2.6 Maven 私服仓库配置

2.6.1 启动并登陆 Nexus Repository 3

打开浏览器,在地址栏输入访问地址,进入页面如下。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3CtibSTe-1599485477117)(assets/1533026525357.png)]

这个页面是不是很炫(~),看到 Nexus 系统正在初始化,大概几秒后,会跳转到如下页面:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kdT07UiD-1599485477121)(assets/1533026596264.png)]

进入该页面后,就可以点进右上角 Sign in 登录系统进行管理和设置了。系统默认初始账号和密码是:admin/admin123。登录页面后如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wSiaExk2-1599485477123)(assets/1533026863588.png)]

比未登录页面多了一个设置图标。点击设置图标,就可以管理和配置各种各样的仓库了,还可以创业用户和角色,设置访问权限等功能。设置页面如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Fs14DL2-1599485477124)(assets/1533026994855.png)]

建议:进入该页面后为了安全起见,建议首先可以修改管理员密码,再做其他设置和管理。

2.6.2 Repositories 仓库介绍

打开 Repositories 这个页面可能有点懵,怎么这么多?其实常用的只有四个:maven-centralmaven-publicmaven-releasesmaven-snapshots。这里说明如下:

**maven-central:**主要负责拉取公共仓库镜像,供平常使用,如:Apache Commons 系列、Spring 系列。
  **maven-public:**我们项目中配置的仓库地址,它其中包含了maven-centralmaven-releasesmaven-snapshots中的内容(可配置),其实它本身是没有内容的。
  **maven-releases:**我们使用私服就是因为团队内部有自己的工具包、私密包等之类的原因,而这些包如果来回 Copy 这用太不方便,所以最后 releases 版的包部署之后就在此仓库下。
  **maven-snapshots:**同上,只是版本为 snapshots

2.6.3 Maven setting.xml 文件仓库配置

<servers>
    <server>
      <id>nexus-xinqiu-public</id>
      <username>私服用户名</username>
      <password>私服密码</password>
    </server>
    <server>
      <id>nexus-xinqiu-releases</id>
      <username>私服用户名</username>
      <password>私服密码</password>
    </server>
    <server>
      <id>nexus-xinqiu-snapshots</id>
      <username>私服用户名</username>
      <password>私服密码</password>
    </server>
</servers>
<mirrors>
    <mirror>
        <!--此镜像一般用来作为公司第三方引用基础类库镜像,是所有仓库的镜像地址 -->
        <id>nexus-xinqiu-public</id>
        <!-- 为*表示为所有的仓库做镜像,有了这个配置,所有的构建都会包含public组,如果你想包含public-snapshots组,
        你必须添加public-snapshots这个Profile,通过在命令行使用如下的 -P 标志:$ mvn -P public-snapshots clean install -->
        <mirrorOf>*</mirrorOf>
        <url>http://host:port/repository/maven-public/</url>
    </mirror>
</mirrors>
<!-- 四个环境 -->
<profiles>
    <!-- 本地 -->
    <profile>
      <id>localhost</id>
      <properties>
        <maven.profiles.activation>localhost</maven.profiles.activation>
      </properties>
      <build>
        <finalName>${project.name}-${project.version}</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </build>
      <repositories>
        <repository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>

    <!-- 开发 -->
    <profile>
      <id>dev</id>
      <properties>
        <maven.profiles.activation>dev</maven.profiles.activation>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <finalName>${project.name}-${project.version}</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </build>
      <repositories>
        <repository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>

    <!--测试-->
    <profile>
      <id>test</id>
      <properties>
        <maven.profiles.activation>test</maven.profiles.activation>
      </properties>
      <build>
        <finalName>${project.name}-${project.version}</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </build>
      <repositories>
        <repository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>

    <!--生产-->
    <profile>
      <id>pro</id>
      <properties>
        <maven.profiles.activation>pro</maven.profiles.activation>
      </properties>
      <build>
        <finalName>${project.name}-${project.version}</finalName>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </build>
      <repositories>
        <repository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus-xinqiu-public</id>
          <url>http://central</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
</profiles>

2.6.4 项目 POM.xml 文件仓库配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- 要将源码放上去,需要加入这个插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<distributionManagement>
    <repository>
        <!-- 这里的ID要和setting的id一致 -->
        <id>nexus-xinqiu-releases</id>
        <url>http://host:port/repository/maven-releases/</url>
    </repository>
    <!--这是打成快照版本的配置,如果不用这个snapshotRepository标签,打包失败,会报权限问题 -->
    <snapshotRepository>
        <id>nexus-xinqiu-snapshots</id>
        <url>http://host:port/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

2.6.3 默认仓库介绍

  1. maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
  2. maven-releases:私库发行版jar,初次安装请将Deployment policy设置为Allow redeploy
  3. maven-snapshots:私库快照(调试版本)jar
  4. maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

Nexus默认的仓库类型有以下四种:

  1. group(仓库组类型):又叫组仓库,用于方便开发人员自己设定的仓库;
  2. hosted(宿主类型):内部项目的发布仓库(内部开发人员,发布上去存放的仓库);
  3. proxy(代理类型):从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的Configuration页签下Remote Storage属性的值即被代理的远程仓库的路径);

Policy(策略): 表示该仓库为发布(Release)版本仓库还是快照(Snapshot)版本仓库;

由于访问中央仓库有时候会比较慢,这里我添加一个阿里云的代理仓库,然后优先级放到默认中央库之前,, 阿里云的maven仓库url为http://maven.aliyun.com/nexus/content/groups/public

Nexus仓库分类的概念

1)Maven可直接从宿主仓库下载构件,也可以从代理仓库下载构件,而代理仓库间接的从远程仓库下载并缓存构件

2)为了方便,Maven可以从仓库组下载构件,而仓库组并没有时间的内容(下图中用虚线表示,它会转向包含的宿主仓库或者代理仓库获得实际构件的内容)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C8rjbfMB-1599485477127)(assets/nexus06.png)]

2.7 使用Nexus Repository 3的十二个理由

2.7.1 理由1:安装更加简单

安装变得更加方便,详细可以参看如下链接。

安装参照URL http://books.sonatype.com/nexus-book/3.0/reference/install.html?__hstc=239247836.f7854f6edce31b386d0c10d0555205f0.1487887540518.1487887540518.1489490179025.2&__hssc=239247836.3.1489490179025&__hsfp=285730640

2.7.2 理由2:官方Docker镜像

使用官方Docker镜像使得更加容易的导入Repository Manager.

项目 详细
官方镜像 https://hub.docker.com/r/sonatype/nexus3/
Easypack镜像 https://github.com/liumiaocn/easypack/tree/master/containers/standard/nexus

2.7.3 理由3:REST API

使用Nexus提供的API使得集成更容易进行。

项目 详细
API http://books.sonatype.com/nexus-book/3.0/reference/scripting.html?__hstc=239247836.f7854f6edce31b386d0c10d0555205f0.1487887540518.1487887540518.1489490179025.2&__hssc=239247836.3.1489490179025&__hsfp=285730640

2.7.4 理由4:用户界面

同Nexus 2相比,Nexus 3的界面增加了一些现代的元素,多多少少使人稍稍有些眼前一亮的感觉,虽说跟Artifactory还是明显有些差距,但是性价比也完全不在一个级别上。

2.7.5 理由5:性能

据说性能依然很好,像其标榜的那样。

2.7.6 理由6:Docker 私库

现在可以用Nexus 来管理Docker 私库了,统一管理,是不是很具有吸引力。

项目 详细
参照内容 https://www.sonatype.com/concepts-benefits-repo-management?__hstc=239247836.f7854f6edce31b386d0c10d0555205f0.1487887540518.1487887540518.1489490179025.2&__hssc=239247836.3.1489490179025&__hsfp=285730640

2.7.7 理由7:npm与bower

支持npm和bower的package管理,对前端工程师造成了很大的诱惑,目前此项优势继续保持中。

2.7.8 理由8:Raw repositories

在Nexus 3中支持一种新的方式:raw repositories。利用这种方式,任何文件都可以像Maven管理对象文件那样被管理起来,对所有的artifacts进行统一集成管理。

2.7.9 理由9:NuGet repositories

支持NuGet repositories,对于.Net开发者来说,这无疑是一个福音。

2.7.10 理由10:支持检索

对于Nexus所支持的任何类型都支持检索功能,这使得无论任何情况下我们都能利用这些功能进行精确定位。

2.7.11 理由11:支持浏览

支持对其仓库的内容进行浏览,非常方便。

2.7.12 理由12:检查机制

对Maven/NuGet/npm仓库,支持安全以及license的检查,使得使用起来更无后顾之忧。

2.7.13 总结

Nexus物美价廉,又提供功能全面的oss版,加之支持种类众多的倚赖管理,又可以统一管理docker镜像,界面也在慢慢好看起来,这些不禁给了我们对Nexus 3更多的期待。

2.7.1.4 参照文档

以上十二条理由是根据Nexus Repository 3官方文档介绍的特性做了一个大致的阐述。

网上资料来源:https://blog.csdn.net/liumiaocn/article/details/62050525

2.8 Docker 私服仓库配置

Nexus 3 可以创建三种类型的 Docker 仓库:

  1. docker (proxy) 代理和缓存远程仓库,例如 Docker Hub、Google Container Registry 等,只能 pull。
  2. docker (hosted) 托管仓库,创建私有仓库,可以 push 和 pull。
  3. docker (group) 将多个 proxy 和 hosted 仓库添加到一个组,组合成一个源,只访问这一个组地址即可,只能pull。

这里我们创建一个 hosted 类型的私有仓库。

2.8.1 创建一个 Docker Hosted 仓库

admin 登录 —> 设置 —> Repository —> Repositories —> docker (hosted)。

  • 输入 Name 这里我输入 docker-hosted。
  • HTTP 设置,可以设置一个其他端口号(当服务经过代理后可使用),这里我设置成18443,下边 push 时往该端口 push。
  • Force basic authentication 设置,是否允许任何人 pull,也即是否公开。这里设置不公开。
  • Enable Docker V1 API 设置,是否支持 V1 API,这里设置支持,这样支持类型更多了。
  • Deployment policy 设置,是否允许重新部署或者只读,这里设置为 Allow redeploy 允许重新部署。

2.8.2 Docker 操作

这里我们以 nginx:1.11 为例,测试能否 push 到指定 docker-hosted 仓库。

$ sudo docker tag nginx:1.11 192.168.56.76:8083/common/nginx:1.11
$ docker images
  ......
  10.236.65.131:18443/common/nginx        1.11           01f818af747d        9 months ago        182MB
  nginx                                   1.11           01f818af747d        9 months ago        182MB
  ......
$ docker login -u admin -p admin123 10.236.65.131:18443
  Login Succeeded
$ docker push 10.236.65.131:18443/common/nginx:1.11

2.9 Nexus的调度任务

默认安装好之后是没有索引和jar文件的,因为你要自己定义任务去执行。

Nexus提供了一系列可配置的调度任务来方便用户管理系统。用户可以设定这些任务运行的方式,例如每天、每周等。调度任务会在适当的时候在后台运行。

要建立一个调度任务,单击左边导航菜单中的Tasks,点击Create Task,然后选择一个任务类型。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w5w8aKfb-1599485477129)(assets/nexus11.png)]

以下几种常用类型的调度任务:

  • Execute script:执行自定义脚本
  • Purge开头:清理一些不使用的资源。
  • Rebuild repository index:为仓库重新编纂索引,从远仓库下载最新的索引。
  • Rebuild Maven repository metadata:基于仓库内容重新创建仓库元数据文件,同时重新创建每个文件的校验和md5与sha1。
  • Remove snapshots from Maven repository:把快照删了,这个是在稳定版发布后清除

比如我新建一个重构索引的任务,然后选择aliyun仓库,让它把远程索引取下来,手动执行。不过最好别这样做,因为需要很大的硬盘空间。

最好是让它自己去维护,请求一个依赖的时候如果私服没有会自动去远仓库取的。

2.10 Nexus搜索页

这个不需要登录就可以访问,用来查询jar包。支持模糊查询

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V5UYDSbd-1599485477131)(assets/nexus07.png)]

2.11 Blob Stores

文件存储的地方,创建一个目录的话,对应文件系统的一个目录,可供仓库上传文件使用,如图所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WpasRMNi-1599485477134)(assets/nexus08.png)]

第三方构建私有库

安装当前最新版本的nexus,安装教程网上搜,不多说了。  因为nexus3x版本没有2x版本中内置的3rd_part,所以不能在界面中上传jar包,必须使用maven的命令行。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jVHgAM9Z-1599485477135)(assets/1533817631466.png)]

添加第三方仓库,名字叫3rd_part

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0tb0Iq00-1599485477137)(assets/1533817672969.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WrRMxTOm-1599485477140)(assets/1533817718132.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vZcjV02t-1599485477144)(assets/1533817745155.png)]

添加到maven-public组中

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B19JkurF-1599485477146)(assets/1533817778637.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Ie6SuTN-1599485477148)(assets/1533817806757.png)]

添加保存,并记下第三方库的地址,稍后通过mvn命令上传要用,记得上传的用户要有仓库的写权限。

打开cmd,上传命令(如果没有配置环境变量先去到“maven安装目录/bin/“目录下才能找到mvn命令):

mvn deploy:deploy-file -DgroupId=com.csource -DartifactId=fastdfs-client-java -Dversion=1.24 -Dpackaging=jar -Dfile=D:\fastdfs_client_v1.24.jar -Durl=http://localhost:1122/repository/3rd_part/ -DrepositoryId=base-3rdPart

说明:  其中的http://localhost:1122/repository/3rd_part/为刚才配置的仓库地址。

-DgroupId=com.csource -DartifactId=fastdfs-client-java -Dversion=1.24这三个参数根据你的jar包的构建和版本号,-DrepositoryId=3rd-part-id,3rd-part-id为在settings.xml中设置的用户id值。

执行命令完成后,进入仓库查看验证是否上传成功。

2.9 小结

根据官方的发布说明,官方开始全力推动 Nexus Respository Manager3.x 版本,2.x的版本支持将尽力而为。

安装只需要将下载的安装包解压到相应的位置,就可以启动 nexus 服务了。我这里总结下安装和配置要注意的几个事项:

1、前提是要必须先要安装 Java 运行环境,并配置好环境变量。因为 nexus 私服仓库管理是 java 开发的,安装包不自带 java 运行时环境,并要确定好安装包要求的 Java 版本。

2、配置文件中的内存配置,一般小创业公司默认配置就足够了。中大型公司需要根据实际情况调大内存。

3、端口和应用上下文的配置可以根据实际情况修改,注意端口是否被占用或冲突的问题。

477140)]

[外链图片转存中…(img-vZcjV02t-1599485477144)]

添加到maven-public组中

[外链图片转存中…(img-B19JkurF-1599485477146)]

[外链图片转存中…(img-3Ie6SuTN-1599485477148)]

添加保存,并记下第三方库的地址,稍后通过mvn命令上传要用,记得上传的用户要有仓库的写权限。

打开cmd,上传命令(如果没有配置环境变量先去到“maven安装目录/bin/“目录下才能找到mvn命令):

mvn deploy:deploy-file -DgroupId=com.csource -DartifactId=fastdfs-client-java -Dversion=1.24 -Dpackaging=jar -Dfile=D:\fastdfs_client_v1.24.jar -Durl=http://localhost:1122/repository/3rd_part/ -DrepositoryId=base-3rdPart

说明:  其中的http://localhost:1122/repository/3rd_part/为刚才配置的仓库地址。

-DgroupId=com.csource -DartifactId=fastdfs-client-java -Dversion=1.24这三个参数根据你的jar包的构建和版本号,-DrepositoryId=3rd-part-id,3rd-part-id为在settings.xml中设置的用户id值。

执行命令完成后,进入仓库查看验证是否上传成功。

2.9 小结

根据官方的发布说明,官方开始全力推动 Nexus Respository Manager3.x 版本,2.x的版本支持将尽力而为。

安装只需要将下载的安装包解压到相应的位置,就可以启动 nexus 服务了。我这里总结下安装和配置要注意的几个事项:

1、前提是要必须先要安装 Java 运行环境,并配置好环境变量。因为 nexus 私服仓库管理是 java 开发的,安装包不自带 java 运行时环境,并要确定好安装包要求的 Java 版本。

2、配置文件中的内存配置,一般小创业公司默认配置就足够了。中大型公司需要根据实际情况调大内存。

3、端口和应用上下文的配置可以根据实际情况修改,注意端口是否被占用或冲突的问题。

4、最重要的一点是在防火墙中开启端口。

猜你喜欢

转载自blog.csdn.net/pointdew/article/details/108456772