什么是 Nexus

Nexus 是一个强大的 Maven 仓库管理器,极大地简化了内部仓库的维护和外部仓库的访问。2016 年 4 月 6 日 Nexus 3.0 版本发布,相较 2.x 版本有了很大的改变

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

    部署 Nexus

    我们使用 Docker 来安装和运行 Nexus,docker-compose.yml 配置如下:
    1. version: '3.1'
    2. services:
    3. nexus:
    4. restart: always
    5. image: sonatype/nexus3
    6. container_name: nexus
    7. ports:
    8. - 8081:8081
    9. volumes:
    10. - ./data:/nexus-data
    注意: 启动时如果出现权限问题可以使用 chmod 777 /usr/local/docker/nexus/data 赋予数据卷目录可读可写的权限
    如果部署报错就采用下面的第二种部署方式: ```yaml version: ‘3.1’ services: nexus: restart: always image: sonatype/nexus3 container_name: nexus ports:
    1. - 8081:8081
    volumes:
    1. - nexus-data:/nexus-data

volumes: nexus-data:

  1. 这种方式挂载的目录在 /var/lib/docker/volumes 目录下<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/1750374/1651301303077-a7e16002-ffc5-474b-ad48-848d7ef264c6.png#clientId=u084d71c0-c426-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=97&id=uedb75445&margin=%5Bobject%20Object%5D&name=image.png&originHeight=97&originWidth=537&originalType=binary&ratio=1&rotation=0&showTitle=false&size=88883&status=done&style=none&taskId=ua174cdd7-988c-45b3-acc7-256e59233a9&title=&width=537)<br />nexus_nexus-data 目录的由来
  2. - nexus_nexus-data,第一个 nexus 是你创建的目录
  3. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1750374/1651301446902-7c10efba-2496-45d5-940c-4226832f4061.png#clientId=u084d71c0-c426-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=78&id=udeb68e76&margin=%5Bobject%20Object%5D&name=image.png&originHeight=78&originWidth=272&originalType=binary&ratio=1&rotation=0&showTitle=false&size=5512&status=done&style=none&taskId=u85c88633-bf4a-494e-addf-c4d2fdf119b&title=&width=272)
  4. - nexus-data docker-compose.yml 文件里设置的挂载目录名 nexus-data
  5. 两者组合而成的文件名:nexus_nexus-data
  6. <a name="V3Kud"></a>
  7. # 验证安装是否成功
  8. - **地址:** [http://ip:port/](https://links.jianshu.com/go?to=http%3A%2F%2Fip%3Aport%2F)
  9. - **用户名:** admin
  10. - **密码:** admin123
  11. **注意:** 新版本密码在 cat /var/lib/docker/volumes/nexus_data/_data/admin.password<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/1750374/1651302267365-60ea398c-f360-456e-9f53-740ccb5da45e.png#clientId=u084d71c0-c426-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=370&id=u21a0479e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=370&originWidth=866&originalType=binary&ratio=1&rotation=0&showTitle=false&size=29570&status=done&style=none&taskId=ueb2404f4-c85b-4b92-a1f8-6df7baec955&title=&width=866)
  12. <a name="zlhUx"></a>
  13. # Maven 仓库介绍
  14. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1750374/1651303359500-406e7bb5-526e-47db-a379-a8c0befed698.png#clientId=u084d71c0-c426-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=439&id=u6beda6d7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=439&originWidth=1229&originalType=binary&ratio=1&rotation=0&showTitle=false&size=63620&status=done&style=none&taskId=u5af63df8-8c8c-4aac-96b2-6b2e86163b1&title=&width=1229)
  15. <a name="Wdlg5"></a>
  16. ## 代理仓库(Proxy Repository)
  17. - 第三方仓库
  18. - **maven-central**
  19. - **nuget.org-proxy**
  20. - 版本策略(Version Policy)
  21. - **Release:** 正式版本
  22. - **Snapshot:** 快照版本
  23. - **Mixed:** 混合模式
  24. - 布局策略(Layout Policy)
  25. - **Strict:** 严格
  26. - **Permissive:** 宽松
  27. <a name="ubi0S"></a>
  28. ## 宿主仓库(Hosted Repository)
  29. - 存储本地上传的组件和资源的
  30. - **maven-releases**
  31. - **maven-snapshots**
  32. - **nuget-hosted**
  33. - 部署策略(Deployment Policy)
  34. - **Allow Redeploy:** 允许重新部署
  35. - **Disable Redeploy:** 禁止重新部署
  36. - **Read-Only:** 只读
  37. <a name="l4PFT"></a>
  38. ## 仓库组(Repository Group)
  39. 通常包含了多个代理仓库和宿主仓库,在项目中只要引入仓库组就可以下载到代理仓库和宿主仓库中的包
  40. - **maven-public**
  41. - **nuget-group**
  42. <a name="X5t9B"></a>
  43. # 在项目中使用 Nexus
  44. <a name="semhD"></a>
  45. ## 配置认证信息
  46. Maven settings.xml 中添加 Nexus 认证信息 (**servers** 节点下)
  47. ```xml
  48. <server>
  49. <id>nexus-releases</id>
  50. <username>admin</username>
  51. <password>admin123</password>
  52. </server>
  53. <server>
  54. <id>nexus-snapshots</id>
  55. <username>admin</username>
  56. <password>admin123</password>
  57. </server>

Snapshots 与 Releases 的区别

  • nexus-releases: 用于发布 Release 版本
  • nexus-snapshots: 用于发布 Snapshot 版本(快照版)

Release 版本与 Snapshot 定义

  1. Release: 1.0.0/1.0.0-RELEASE
  2. Snapshot: 1.0.0-SNAPSHOT
  • 在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。
  • SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar

    配置自动化部署

    在 pom.xml 中添加如下代码

    1. <distributionManagement>
    2. <repository>
    3. <id>nexus-releases</id>
    4. <name>Nexus Release Repository</name>
    5. <url>http://127.0.0.1:8081/repository/maven-releases/</url>
    6. </repository>
    7. <snapshotRepository>
    8. <id>nexus-snapshots</id>
    9. <name>Nexus Snapshot Repository</name>
    10. <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
    11. </snapshotRepository>
    12. </distributionManagement>

    注意事项

  • ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致

  • 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号

    部署到仓库

    mvn deploy

    配置代理仓库

    <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>
    

    MAVEN 手动上传第三方依赖

    Nexus 3.1.x开始支持页面上传第三方依赖功能,以下为手动上传命令

    # 如第三方JAR包:aliyun-sdk-oss-2.2.3.jar
    mvn deploy:deploy-file
    -DgroupId=com.aliyun.oss
    -DartifactId=aliyun-sdk-oss
    -Dversion=2.2.3
    -Dpackaging=jar
    -Dfile=D:\\aliyun-sdk-oss-2.2.3.jar
    -Durl=http://127.0.0.1:8082/repository/maven-3rd/
    -DrepositoryId=nexus-releases
    

    这个nexus-releases 指的就是server的id, 会带着这个server的账号密码去deploy.
    注意事项:

  • 建议在上传第三方JAR包时,创建单独的第三方JAR包管理仓库,便于管理有维护。(maven-3rd)

  • -DrepositoryId=nexus-releases对应的是 settings .xml中 Servers 配置的ID名称。(授权)

    也可以直接在 Nexus 中上传

    image.png

    删除多余的快照版本

    每次打包都会上传 Nexus 形成一般版本,这样会比较占据容量
    image.png
    可以在设置中设置定时任务,定时删除多余的快照版本
    image.png
    选择 Mavne-Delete SNAPSHOT
    image.png
    做如下设置
    image.png
    点击运行测试
    image.png
    多余的已经清理掉了
    image.png