搭建Maven私服仓库

1. 概述

Sonatype Nexus是一种特殊的远程仓库,是架设在局域网内的仓库管理器。 nexus 可以搭建局域网maven私服仓库(实现局域网用户共享),并且支持代理广域网上的远程仓库。nexus具备强大的仓库管理功能,构件搜索功能等。
好处:作为公共maven仓库的 专用代理服务器、为maven项目部署 提供存储库

理解:
没有配置 nexus 之前(没有搭建私服),如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器, 有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度, 项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多。

市面上的仓库管理器:

2.下载: nexus-2.14.21-02-bundle.zip

下载地址:
Nexus Repository Manager 2 OSS:https://help.sonatype.com/repomanager2/download 使用说明文档:https://help.sonatype.com/repomanager2/installing-and-running

3.安装卸载启动停止nexus(windows版本)

安装卸载nexus

  1. 解压 nexus-2.14.21-02-bundle.zip ,进入 bin 目录:
  2. 以管理员权限运行命令行,执行** nexus.bat install/uninstall**
  3. 安装成功后,在服务中查看有 nexus 服务:

注:以管理员身份安装;路径不要有中文

启动停止nexus服务

  • 方式一:cmd 进入 bin 目录,执行nexus.bat start/stop
  • 方式二:找到nexus服务,右键启动/停止
  • 方式三:开机自启动(在windows安装为服务,一般不需要设置开机自启)
  1. 打开windows开机自启动目录:win+R,命令框输入shell:startup
  2. 将Nexus启动脚本复制到目录下(在任务管理器-启动 可以看到nexus启动脚本(可以禁用和启用))
  3. 测试开机自启动Nexus

值得注意:
1.Sonatype组织 在$nexus-2.14.21-02\bin\jsw下内置了适应各操作系统的"安装、卸载、启动、停止"脚本,可以直接执行脚本实现 安装卸载启动停止nexus
2.nexus启动脚本支持常用的服务命令:install、uninstall、start、stop、restart、status、console(控制台)和dump(转储)

4.登录

访问: http://localhost:8081/nexus/ 注:IP端口号从nexus配置文件 conf/nexus.properties修改查看,默认本机:8081
登录log In:(账号admin,密码admin123 ,登录后可修改管理员密码)
默认有三个用户:
admin:默认密码为admin123(最高管理员)
deployment:默认密码为deployment123(拥有发布权限,可以登录上nexus删除jar)
anonymous: 免登录用户,可以在nexus界面上查看 jar包

5.nexus仓库类型

nexus 的仓库有 4 种类型:

  1. hosted,宿主仓库, 部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 、3rd party。 Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库、3rd party第三方jar包
  2. proxy,代理仓库, 用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私
    服自动去中央仓库下载 jar 包或者插件。
  3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓
    库组。
  4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件

![image.png]([object Object]&name=image.png&originHeight=253&originWidth=298&originalType=binary&ratio=1&rotation=0&showTitle=false&size=38971&status=done&style=none&taskId=uf4075a46-693f-4da6-8d51-244adb68625&title=&width=298)

私服仓库的使用

1.发布项目到私服仓库(OSS Repository Hosting,OSSRH)

用Maven发布项目到OSSRH和中央仓库教程:https://central.sonatype.org/publish/publish-maven/ 这里只演示发布到OSSRH,不演示发布到中央仓库(Nexus Staging Maven 插件实现一键部署到OSSRH和中央仓库)

  1. 首先需要将Releases仓库和Snapshots仓库的"Configuration-Deployment Policy"设置为“Allow Redeploy”
  2. 配置maven连接私服的仓库id和Nexus账号密码

安装maven环境,在maven配置文件(settings.xml)中,添加如下配置:

<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>
  1. 配置项目发布到哪个私服(仓库id和仓库访问地址)

在项目的pom.xml 增加Nexus仓库访问路径 配置
如下 仓库的访问路径配置可以在Nexus中仓库Summary复制(!!要先登录!!)
注意: pom.xml 的 和 上述settings.xml 是对应的,都是Nexus仓库id!

<distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
  1. 部署项目到OSSRH:对项目工程jar包 执行mvn clean deploy部署命令 (会上传到私服和本地仓库),根据version确定上传到snapshots仓库(version以-SNAPSHOT结尾)还是releases仓库(version以1.0.0或者1.0.0-RELEASE结尾)
  1. 修改version:

方式一:在pom.xml中设置version
方式二:通过Maven版本插件设置版本mvn versions:set -DnewVersion=1.2.3

也可以使用maven多环境配置的profile指定发布到哪个仓库,然后mvn clean deploy -P releases(即是profileId)

<profile>
    <id>releases</id>
    <repositories>
        <repository>
					<id>releases</id>
       	  <url>http://localhost:8081/nexus/content/repositories/releases/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
   			</repository>
    </repositories>
  
    <pluginRepositories> <!-- 插件仓库, maven的运行依赖插件,也需要从私服下载插件 -->
        <pluginRepository>
            <id>public</id>  <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
            <name>Public Repositories</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>
2.下载jar依赖包和插件

当项目引入依赖时,依赖获取顺序:本地仓库–>私服仓库–>远程中央仓库
附:Nexus整合maven:https://help.sonatype.com/repomanager2/maven-and-other-build-tools/apache-maven 在maven配置文件(setting.xml)中配置私服仓库:

  • 镜像mirror:所有请求nexus,都会通过镜像去访问(镜像配置的仓库url会覆盖下面profile的仓库url),一个仓库只能映射一个镜像(即 不能映射多个mirrorOf相同的镜像,maven不会聚合镜像,如果镜像重复只会使用第一个)
  • 环境profile:定义了一组id为public的repository和pluginRepository(如果仓库id相同,profile的配置会覆盖maven pom的内置配置),这个覆盖很重要,因为这里启用了releases和snapshots 并且用nexus的仓库url替代maven pom的默认中央仓库配置
<settings>
  <mirrors>
    <mirror>
      <id>nexus</id><!--镜像id,mirrorId-->
      <url>http://localhost:8081/nexus/content/groups/public</url><!--Nexus的公共组/仓库访问路径-->
      <mirrorOf>*</mirrorOf> <!--映射为镜像的具体仓库id,*表示(组内)全部仓库,central表示中央仓库 -->
    </mirror>
  </mirrors>

  
<profile>
    <id>dev</id>  <!--profile的id-->
    <repositories>
        <repository>
          <id>public</id>  <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
          <!--仓库地址,即 nexus 仓库组的地址-->
          <url>http://localhost:8081/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases><!--启用releases-->
          <snapshots><enabled>true</enabled></snapshots> <!--启用snapshots-->
   			</repository>
    </repositories>
  
    <pluginRepositories> <!-- 插件仓库, maven的运行依赖插件,也需要从私服下载插件 -->
        <pluginRepository>
            <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
            <id>public</id>
            <name>Public Repositories</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>

   <!-- 使用 profile 定义仓库之后,需要指定启用哪一套-->
  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>
3.Nexus代理远程中央仓库

Maven已经把中央仓库交给Nexus管理,Nexus Repository Manager也默认代理中央仓库
Nexus默认是关闭远程中央仓库索引的,手动开启下载索引,即可浏览或搜索远程中央仓库的组件详细坐标等信息;也可以手动配置想要代理的远程中央仓库,步骤如下:
在Nexus功能面板左侧,点击 Views/Repositories–>repositories–>Type=proxy的都是Nexus代理的中央仓库。在其Configuration标签下做远程仓库相关配置:
Remote Storage Location 远程中央仓库访问地址
Default local Storage Location默认本地存储路径(可以把本地maven仓库的所有jar包通过XFTP上传到里面,再save)
Download Remote Indexes = true 开启下载远程中央仓库的索引坐标

4.导入第三方jar包(不存在于中央仓库的jar包,私人创建的jar)

目标:把第三方jar包导入本地仓库、私服仓库

  • 导入本地仓库:cmd进入jar包所在位置,执行如下maven安装命令
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar
  • 导入私服仓库
方式一 maven方式

在maven配置文件中(setting.xml),配置第三方 仓库的server信息

<server>
    <id>thirdparty</id>
    <username>admin</username>
    <password>admin123</password>
</server>

cmd进入jar包所在位置,执行如下maven安装命令

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
  • 参数说明

-DgroupId 和 -DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。自己起名字也行。
Dfile 表示需要上传的 jar 包的绝对路径。
Durl 私服上仓库的位置,打开 nexus——>repositories 菜单,可以看到该路径。
DrepositoryId 服务器的表示 id,在 nexus 的 configuration 可以看到。
Dversion 表示版本信息。
关于 jar 包准确的版本:
包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件
这个文件就有描述该包的版本信息。
比如 Specification-Version: 2.2 可以知道该包的版本了。
上传成功后,在 nexus 界面点击 3rd party 仓库可以看到这包。

方式二:Nexus控制台上传第三方jar包

![image.png]([object Object]&name=image.png&originHeight=903&originWidth=1398&originalType=binary&ratio=1&rotation=0&showTitle=false&size=145239&status=done&style=none&taskId=u0e0a92a7-cd23-40cd-a463-a9e269148db&title=&width=1398)

4. 批量导入第三方jar包 到nexus仓库----Windows版本

这些批量导入第三方jar包,maven中央仓库中存在的jar会自动创建索引;导入自建jar不会创建索引,无法根据maven坐标下载依赖,不过可以在nexus控制台手动导入并写上索引

  1. 对于maven中央仓库中已存在的依赖文件:可以直接复制到 nexus工作目录\storage\指定存储库
  2. 对于自定义的依赖文件:需要在nexus控制台 手动导入并写上maven坐标

方式一:直接拷贝文件到nexus工作目录sonatype-work\nexus\storage\指定宿主仓库下(推荐!)

方式二:shell脚本方式
  1. 编写批量上传-shell脚本
#!/bin/bash
# 复制该脚本到 需要上传的仓库的根目录
# 该脚本会排除自己不上传到nexus,所以脚本命名需要注意(也可以修改脚本命令)

# timer start
start=$(date +%s)
# Get command line params
while getopts ":r:u:p:" opt; do
case $opt in
    r) REPO_URL="$OPTARG"
    ;;
    u) USERNAME="$OPTARG"
    ;;
    p) PASSWORD="$OPTARG"
    ;;
  esac
done

find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
# timer end
end=$(date +%s)
take=$(( end - start ))
echo execute finish!
echo ${take} seconds.
  1. 在git的bash命令行 执行shell脚本:(前提是window已经安装git)

命令:./mavenimport.sh -u admin -p admin123 -r 仓库访问地址
示例:./mavenimport.sh -u admin -p admin123 -r http://localhost:8081/nexus/content/repositories/thirdparty

  1. 仓库查看是否上传成功,测试表明 上传2m文件消耗16s
5.Nexus OSS私服仓库的备份与迁移


Nexus安装后的重要目录结构说明

nexus主目录

nexus-2.14.21-02

conf/nexus.properties里面有sonatype-work的地址

工作目录

sonatype-work

nexus/conf/nexus.xml里面有storage的地址

存储目录

storage

里面主要是nexus管理的jar包等

迁移过程不难,只需要打包-备份、拉取-启动即可:这里拿linux系统演示(window很简单不做演示)

  1. 打包:工作目录sonatype-work

_命令:tar -zcvf 目标文件名 源文件 _
示例:tar -zcvf sonatype-work-Beifen.tar.gz ./sonatype-work

  1. 备份拉取:使用Gitlab实现从远程服务器拷贝文件到本地
scp 源用户名@IP地址:文件完整路径 本地用户名@IP地址:完整文件名
确认是否连接?
输入连接 源文件所在服务器的密码

附:
本地的用户名@IP地址可以不写,直接写个文件路径即可
可能有用的几个参数 :
-v 和大多数 linux 命令中的-v意思一样,用来显示进度,可以用来查看连接,认证,或是配置错误
-C 使能压缩选项
-P 选择端口 . 注意 -p 已经被 rcp 使用
-4 强行使用 IPV4 地址
-6 强行使用 IPV6 地址
  1. 解压启动:
  • 解压拷贝过来的sonatype-work压缩文件tar -zxvf sonatype-work-Beifen.tar.gz
  • 修改新Nexus的工作目录指向拷贝过来的sonatype-work:安装目录下conf/nexus.properties
  • 启动nexus
6.运行在反向代理之后

为什么要运行在反向代理后?
Nexus Repository Manager是一个复杂的服务器应用程序,具有web应用程序用户界面,使用高性能的servlet容器Eclipse Jetty15响应HTTP请求。 组织有时需要在反向代理后运行像Nexus Repository Manager Pro或Nexus Repository Manager OSS这样的应用程序。理由包括:

  • 安全和审核问题
  • 网络管理员熟悉
  • 组织政策
  • 整合不同应用程序(集群?)
  • 虚拟主机
  • 在受保护的端口上暴露应用
  • SSL终端

如何配置反向代理?
Repository Manager有两个可能影响反向代理的内置配置属性,建议进行修改完善:

  1. 修改Nexus访问路径Webapp Context Path:用反斜杠/ 代替 /nexus (在安装目录下/conf/nexus.properties配置文件),修改完重启Nexus访问测试一下,确保成功!!
  2. Application Server Settings可以修改Base URL,但不要勾选Force Base URL:可能导致反向代理无法正常工作

提供两种方式配置反向代理:
Apache httpd

ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
 ServerName www.example.com
 ServerAdmin admin@example.com
 ProxyPass /nexus http://localhost:8081/nexus
 ProxyPassReverse /nexus http://localhost:8081/nexus
 ErrorLog logs/www.example.com/nexus/error.log
 CustomLog logs/www.example.com/nexus/access.log common
</VirtualHost>

nginx

http {
   proxy_send_timeout 120;
   proxy_read_timeout 300;
   proxy_buffering off;
   proxy_request_buffering off;
   keepalive_timeout 5 5;
   tcp_nodelay on;
   server {
     listen *:80;
     server_name www.example.com;
     # allow large uploads of files - refer to nginx documentation
     client_max_body_size 1G;
     # optimize downloading files larger than 1G - refer to nginx doc before adjusting
     #proxy_max_temp_file_size 2G;
   location /nexus {
     proxy_pass http://localhost:8081/nexus;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  	 }
   }
}
Nexus安装后检查清单(Nexus建议)
  1. 修改管理员密码(默认admin123)和 Email Address
  2. 配置SMTP Settings
  3. 配置Default HTTP and HTTPS Proxy Settings
  4. 开启Remote Index Downloads
  5. 修改部署员Deployment 密码(默认 deployment123 )
  6. 如果需要的话,设置 LANG Environment Variable
  7. 配置路由