Docker搭建私有Nexus-maven仓库

红太狼 2022-05-11 12:42 413阅读 0赞

本文使用docker镜像实现nexus的一键部署,并且进行相关配置,测试完成maven工程的打包上传

文章目录

    • 一 利用Docker镜像搭建Nexus仓库
        • 1 建立 nexus-data 数据卷
        • 2 启动镜像
    • 二 完成Nexus设置
        • 修改密码
        • 添加用户
        • 获取仓库url
    • 三 配置本地Maven
        • 0 maven 配置文件基础知识
        • 1 配置 servers
        • 2 配置profile
        • 3 配置activeProfiles
        • 4 配置镜像
    • 四 配置pom文件
    • 五 测试
    • 六 其他

一 利用Docker镜像搭建Nexus仓库

这部分直接参考 https://hub.docker.com/r/sonatype/nexus3/ 就好
启动大概需要两三分钟,默认的 用户/密码 是:admin / admin123

1 建立 nexus-data 数据卷

  1. docker volume create --name nexus-data

2 启动镜像

  1. docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3

二 完成Nexus设置

输入对应的ip:port进入主页,点右上角的 Sign in 按钮用默认账号登录,看到的界面大致如下
Nexus-index
左边的选项写得很清楚,分别是检索、浏览、上传功能。

修改密码

建议新用户登录立即修改密码
点击右上角的admin进入账户管理,点击Change password按钮,输入旧密码验证通过后即可修改密码

添加用户

点击上面齿轮图标进入系统设置,选择Security下的Users,Create local user创建一个本地用户,如下
创建用户
这里我将所有的角色都赋予了LinShen,使其拥有最大的权限,要定制合理角色权限的话到 Roles 选项设置

获取仓库url

默认的仓库列表如下,主要用到的有 maven-public(共有仓库,不区分snapshots和releases),maven-snapshots(snapshots私有仓库),maven-releases(relsases私有仓库)
复制出url,下面配置仓库url要用到
获取url

三 配置本地Maven

配置本地Maven安装目录下的 conf/settings.xml 文件,建议先把原文件备份
这里settings.xml的写法主要参考阿里云私有仓库给的配置文件

0 maven 配置文件基础知识

首先根据activeProfiles找到对应id的profileprofile下管理了repositoriespluginRepositories两大模块,其下有各个具体仓库,仓库配置了id,url,releases和snapshots支持信息等。根据id寻找仓库url的时候会根据mirror下的mirrorOf规则匹配到对应的镜像仓库url,并用镜像仓库url代替原仓库url

1 配置 servers

配置servers节点如下

  1. <servers>
  2. <server>
  3. <id>lin-releases</id>
  4. <username>LinShen</username>
  5. <password>密码</password>
  6. </server>
  7. <server>
  8. <id>lin-snapshots</id>
  9. <username>LinShen</username>
  10. <password>密码</password>
  11. </server>
  12. </servers>

2 配置profile

配置 id为nexusprofile节点,profile节点下有idrepositoriespluginRepositories三个主要部分,repositoriespluginRepositories下有各自有releasessnapshots版本的共有仓库和releasessnapshots版本的私有仓库,私有仓库的id和servers的id统一即可
这里将私有仓库的url根据releasessnapshots配置,其他的统一用共有仓库的url即可

  1. <profiles>
  2. <profile>
  3. <id>nexus</id>
  4. <repositories>
  5. <repository>
  6. <id>central</id>
  7. <url>http://120.79.168.114:9081/repository/maven-public/</url>
  8. <releases>
  9. <enabled>true</enabled>
  10. </releases>
  11. <snapshots>
  12. <enabled>false</enabled>
  13. </snapshots>
  14. </repository>
  15. <repository>
  16. <id>snapshots</id>
  17. <url>http://120.79.168.114:9081/repository/maven-public/</url>
  18. <releases>
  19. <enabled>false</enabled>
  20. </releases>
  21. <snapshots>
  22. <enabled>true</enabled>
  23. </snapshots>
  24. </repository>
  25. <repository>
  26. <id>lin-releases</id>
  27. <url>http://120.79.168.114:9081/repository/maven-releases/</url>
  28. <releases>
  29. <enabled>true</enabled>
  30. </releases>
  31. <snapshots>
  32. <enabled>false</enabled>
  33. </snapshots>
  34. </repository>
  35. <repository>
  36. <id>lin-snapshots</id>
  37. <url>http://120.79.168.114:9081/repository/maven-snapshots/</url>
  38. <releases>
  39. <enabled>false</enabled>
  40. </releases>
  41. <snapshots>
  42. <enabled>true</enabled>
  43. </snapshots>
  44. </repository>
  45. </repositories>
  46. <pluginRepositories>
  47. <pluginRepository>
  48. <id>central</id>
  49. <url>http://120.79.168.114:9081/repository/maven-public/</url>
  50. <releases>
  51. <enabled>true</enabled>
  52. </releases>
  53. <snapshots>
  54. <enabled>false</enabled>
  55. </snapshots>
  56. </pluginRepository>
  57. <pluginRepository>
  58. <id>snapshots</id>
  59. <url>http://120.79.168.114:9081/repository/maven-public/</url>
  60. <releases>
  61. <enabled>false</enabled>
  62. </releases>
  63. <snapshots>
  64. <enabled>true</enabled>
  65. </snapshots>
  66. </pluginRepository>
  67. <pluginRepository>
  68. <id>lin-releases</id>
  69. <url>http://120.79.168.114:9081/repository/maven-releases/</url>
  70. <releases>
  71. <enabled>true</enabled>
  72. </releases>
  73. <snapshots>
  74. <enabled>false</enabled>
  75. </snapshots>
  76. </pluginRepository>
  77. <pluginRepository>
  78. <id>lin-snapshots</id>
  79. <url>http://120.79.168.114:9081/repository/maven-snapshots/</url>
  80. <releases>
  81. <enabled>false</enabled>
  82. </releases>
  83. <snapshots>
  84. <enabled>true</enabled>
  85. </snapshots>
  86. </pluginRepository>
  87. </pluginRepositories>
  88. </profile>
  89. </profiles>

3 配置activeProfiles

使上文的profile生效

  1. <activeProfiles>
  2. <activeProfile>nexus</activeProfile>
  3. </activeProfiles>

4 配置镜像

这个其实是很关键的,下面的作用就是给id为 lin-releaseslin-snapshots 之外的仓库配置镜像地址,所以,其实上面可以不用配置那么多的…

  1. <mirrors>
  2. <mirror>
  3. <id>mirror</id>
  4. <mirrorOf>!lin-releases,!lin-snapshots</mirrorOf>
  5. <name>mirror</name>
  6. <url>http://120.79.168.114:9081/repository/maven-public</url>
  7. </mirror>
  8. </mirrors>

四 配置pom文件

在pom文件添加如下内容即可

  1. <distributionManagement>
  2. <repository>
  3. <id>lin-releases</id>
  4. <url>http://120.79.168.114:9081/repository/maven-releases/</url>
  5. </repository>
  6. <snapshotRepository>
  7. <id>lin-snapshots</id>
  8. <url>http://120.79.168.114:9081/repository/maven-snapshots</url>
  9. </snapshotRepository>
  10. </distributionManagement>

五 测试

在pom文件所在目录下执行mvn clean deploy即可,注意,不要使用deploy:deploy,否则容易出错
前者将在每个循环中运行所有步骤(除非你自己选择跳过),包括安装(如编译,打包,测试等)。后者甚至不会编译或打包你的代码,它只会运行那一个目标。
运行输出应该如下
测试
这个时候去Nexus看一下,先点Browse,再选择maven-snapshots仓库,可以看到如下
仓库
如果是去看maven-central内容会更多

六 其他

其实,搭这个东西本身不难,只是体验一下而已,生产开发我还是推荐直接使用阿里云 云效 的私有仓库
https://repomanage.rdc.aliyun.com/my/repo ,方便快捷,直接使用阿里云镜像仓库服务

如果工程开发中想跳过上传步骤,可以在pom文件中添加如下配置

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-deploy-plugin</artifactId>
  6. <configuration>
  7. <skip>true</skip>
  8. </configuration>
  9. </plugin>
  10. </plugins>
  11. </build>

发表评论

表情:
评论列表 (有 0 条评论,413人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Docker私有仓库

    仓库搭建 搭建私有仓库最简单的方法是在容器管理节点(物理机或者虚拟机)上搭建registry容器,并将其作为企业内部的私有仓库,存放所有需要部署的容器镜像。 首先,让我