2.PaaS--平台即服务之Nexus搭建私服

1. 什么是Nexus?

Nexus 是一个强大的仓库管理器,极大地简化了内部仓库的维护和外部仓库的访问。

2016 年 4 月 6 日 Nexus 3.0 版本发布,相较 2.x 版本有了很大的改变:

  • 对低层代码进行了大规模重构,提升性能,增加可扩展性以及改善用户体验。
  • 升级界面,极大的简化了用户界面的操作和管理。
  • 提供新的安装包,让部署更加简单。
  • 增加对 Docker, NeGet, npm, Bower 的支持。
  • 提供新的管理接口,以及增强对自动任务的管理。

2. 基于 Docker 安装 Nexus

我们使用 Docker 来安装和运行 Nexus,docker-compose.yml配置如下:

version: '3.1'
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
      - 8081:8081
    volumes:
      - /usr/local/docker/nexus/data:/nexus-data

注: 启动时如果出现权限问题,可以先执行:chmod 777 /usr/local/docker/nexus/data 赋予数据卷目录可读可写的权限,然后再执行docker-compose up的命令
如果你使用的是阿里云云服务器,启动时控制台没有报错,但是浏览器访问不了,就要看看防火墙有没有配置规则

登录验证是否安装成功:
地址:http://ip:port/ 用户名:admin 密码:密码在 /usr/local/docker/nexus/data/目录下的admin.password文件中,直接复制登录即可

安装成功时登录页面

3.仓库介绍

3.1 代理仓库(Proxy Repository)

意为第三方仓库,如:maven-central nuget.org-proxy,用来和中央仓库同步,比如我们需要一个jar包,我们自己的仓库中没有,此时就通过代理仓库来和中央仓库同步,先将这个jar包同步到代理仓库,然后再从代理仓库下载到本地,这样就可以依赖了

  • 版本策略(Version Policy):
    Release: 正式版本
    Snapshot: 快照版本
    Mixed: 混合模式
  • 布局策略(Layout Policy):
    Strict:严格
    Permissive:宽松

3.2 宿主仓库(Hosted Repository)

  • 存储本地上传的组件和资源的,如:
    maven-releases
    maven-snapshots
    nuget-hosted

  • 部署策略(Deployment Policy):
    Allow Redeploy:允许重新部署
    Disable Redeploy:禁止重新部署
    Read-Only:只读

3.3 仓库组(Repository Group)

通常包含了多个代理仓库和宿主仓库,在项目中只要引入仓库组就可以下载到代理仓库和宿主仓库中的包,如:
maven-public
nuget-group

4. 在项目中使用Maven私服

step 1 配置认证信息

在 Maven settings.xml中添加 Nexus 认证信息(servers 节点下):

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>

<server>
  <id>nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>

作用:指定了id、登录私服的用户名和密码,其中私服的地址在下面配置

step 2 配置自动化部署

在自己项目中的pom.xml中配置如下信息

<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
    <name>Nexus Release Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>  
  </snapshotRepository>  
</distributionManagement> 

注意:ID必须要和server中配置的一致
作用:主要是存放我们自己项目打包后上传的包,是真正达到私有化目的的配置

step 3 配置代理仓库

<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

作用:用于和中央仓库进行同步。例如:我们需要一个common-logging.jar的包,就会去代理服务器目录下去找,如果代理仓库没有,代理仓库会自己去中央仓库去同步下载到代理仓库,再供项目下载使用。

参考:https://www.funtl.com/zh/nexus/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容