• [maven] 使用 Nexus 管理 repository


    [maven] 使用 Nexus 管理 repository

    大概是说还有一篇笔记,两篇内容,maven 的内容就差不多过完了。这一篇笔记主要记一下 maven 的 central 管理部分,之前提到过我们公司用的就是 nexux 做了一个镜像,这里也会用 docker 去创建一个本地的 nexus

    三合一的话内容就拆得太长了……所以这里 nexus 的内容单独拆出来,剩下的就剩下 plugins & properties 了

    简介

    首先简单的过一下管理的流程,本地的项目通过 maven install build deploy 等指令和公司的 repo 进行沟通,公司的 repo 再和 central repo(maven 提供的)或是其他的 cloud 进行沟通。

    这个系列主要是讲 maven,所以主要的目标就是 Central Repo,实际操作上,我们公司的前段项目也是部署在 nexus 上,这块就对应了其他的 cloud

    maven install build deploy
    Organization Repository
    Central Repository
    Cloud

    公司自己内部 host 一个 repo 有几个好处:

    • cached 依赖

    • 控制依赖

      比如说不允许下载未被验证的版本,或者是过于老旧有很多安全隐患的依赖也可以从公司的 repo 删除

    • 上传/管理公司内部的项目

      比如说内部的 util 之类的

    使用 docker 运行 nexus

    docker 是一个本身的话可以简化一些执行和运行的过程的同时,能够在跨平台的同时还取得相同结果的平台,它主要是用的是 image 和 container 去进行处理,这里不会过多赘述。

    说起来我还得吧 docker 和 k8s 的课上完……叹气……

    docker 安装

    官网上有安装起,点点点就好了:https://www.docker.com/

    安装后可以使用下面这个指令查看 docker 是否安装成功了:

    docker --version
    Docker version 20.10.17, build 100c701
    
    • 1
    • 2

    当然,如果 docker 不是作为一个服务器启动的话,得先开启 docker 才行:

    在这里插入图片描述

    运行 nexus

    nexus 的网址在这里:https://hub.docker.com/r/sonatype/nexus3/

    直接执行下面指令就可以了:

    docker run -d -p 8081:8081 --name nexus sonatype/nexus3
    Unable to find image 'sonatype/nexus3:latest' locally
    57168402cb72: Pull complete
    6caa094755dc: Pull complete
    94656dc36e5f: Pull complete
    252d91e5c44e: Pull complete
    0100fe0780d1: Pull complete
    12a0cb9b1e42: Pull complete
    cd872a505f36: Pull complete
    Digest: sha256:a361830ede036ac2f4942464b1c9ca535cbfcba14c5d3237b0d378fcfee1f3ec
    Status: Downloaded newer image for sonatype/nexus3:latest
    7555a03ff1bbe5f9e482e1f1dc99311e386da8b669130446303c4fe06d3f05db
        demo ❯
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这个指令大致意思是说:docker 需要在 detached 模式下运行,即该容器在后台运行,不占据终端,同时它需要完成 port mapping——docker 默认容器内的 port 不会对外公布,进行一个 8081[外]:8081[内] 的 mapping,这样在容器外使用 localhost:8081 也可以访问。

    运行成功可以看到 nexus 已经运行成功了:

    docker images
    REPOSITORY        TAG       IMAGE ID       CREATED      SIZE
    sonatype/nexus3   latest    0b14bd3acc13   9 days ago   552MB
    
    • 1
    • 2
    • 3

    admin 登陆

    刚开始还没完全加载好的页面是这样的:

    在这里插入图片描述

    完了就会提示需要登录,并且提供了密码路径:

    在这里插入图片描述

    使用以下指令查看密码,登陆,修改密码即可:

    docker container exec nexus cat /nexus-data/admin.password
    
    • 1

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    repo 类型

    在这里插入图片描述

    nexus 中主要有这么几种类型:

    • proxy
    • group
    • hosted

    其提供的优点有:

    • 提供中心化管理

      对于开发来说,项目中所有地址可以指向 proxy 地址,而不用填写多个 central repo 的地址

    • 缓存

      所有下载的 repo 都会被缓存到 nexus 中,这样相对而言就比较稳定,也可以减少访问 central repo 的流量

    • 权限管理

      比如说我们访问 nexus 就必须要求使用 vpn 访问内网才能用

    • 分组权限管理

      控制那些 repo/artifact 可以被哪些组群的人访问

    同样,这些组合的话也可以提供更强的安全性,如公司内部 deploy 的 repo 只有公司内部的人可以使用

    新建 maven group

    这个在 nexus 的 UI 上操作即可,首先需要以管理员权限登录,随后到设置里面选择新建 repository:

    在这里插入图片描述

    可以看到可供选择的选项有很多:

    在这里插入图片描述

    这里选择 maven2(group) 即可

    基础内容不用管太多,需要填写的事名字和 repo 的 group:

    在这里插入图片描述

    在这里插入图片描述

    这代表着歌 group 负责中心管理(链接 central repo),snapshots 和 release 的管理

    setting xml

    这个文件是放在 .m2 根目录下的,大致内容如下:

    
    <settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
    
      <servers>
        <server>
          <id>nexus-snapshotsid>
          <username>adminusername>
          <password>admin123password>
        server>
        <server>
          <id>nexus-releasesid>
          <username>adminusername>
          <password>admin123password>
        server>
      servers>
    
      <mirrors>
        <mirror>
          <id>centralid>
          <name>centralname>
          <url>http://localhost:8081/repository/maven-group/url>
          <mirrorOf>*mirrorOf>
        mirror>
      mirrors>
    
    settings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    这会让所有的访问全都重定向到 http://localhost:8081/repository/maven-group/,这个 url 地址是之前创建的 maven-group 的网址:

    在这里插入图片描述

    mirror 是一个镜像地址,不设置的话就是 settings.xml 里的默认值

    不过这些配置也可以在 nexus 里搞,这样项目只需要稳定的从 nexus 里进行数据交互即可

    新建 maven 项目并进行配置

    这里快捷键创建了一个 maven 项目,初始 pom 如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0modelVersion>
    	<groupId>com.ga.mavengroupId>
    	<artifactId>repodemoartifactId>
    	<version>0.0.1-SNAPSHOTversion>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    当然,这些不是重点,重点的是下面的配置:

    	<repositories>
    		<repository>
    			<id>maven-groupid>
    			<url>http://localhost:8081/repository/maven-group/url>
    		repository>
    	repositories>
    
    	<distributionManagement>
    		<snapshotRepository>
    			<id>nexus-snapshotsid>
    			<url>http://localhost:8081/repository/maven-snapshots/url>
    		snapshotRepository>
    		<repository>
    			<id>nexus-releasesid>
    			<url>http://localhost:8081/repository/maven-releases/url>
    		repository>
    	distributionManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    dependencies 里面定义要到哪个 central repo 里面去下载项目,鉴于 nexus 提供了 proxy,所以这里只需要提供 nexus 的网址即可

    定义了 deploy 的 snapshot&release 放到哪里去,这里的 id 最好是与 settings.xml 中一致,我之前少打了一个 s,最终导致 deply 失败。

    install & deploy

    我把 .m2 下面的 repo 删了,这样下载看得比较清楚些

    install

    install 的时候可以看到,所有的依赖都是从 localhost 下载的:

    ls ~/.m2/repository
    ❯ mvn install
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -----------------------< com.ga.maven:repodemo >------------------------
    [INFO] Building repodemo 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom (8.1 kB at 19 kB/s)
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-plugins/23/maven-plugins-23.pom (9.2 kB at 121 kB/s)
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/maven/maven-parent/22/maven-parent-22.pom
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/maven/maven-parent/22/maven-parent-22.pom (30 kB at 327 kB/s)
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/apache/11/apache-11.pom
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/apache/11/apache-11.pom (15 kB at 218 kB/s)
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar (30 kB at 469 kB/s)
    Downloading from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom
    Downloaded from central: http://localhost:8081/repository/maven-group/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom (10 kB at 129 kB/s)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    deploy

    这里主要就最后几行,deploy 到 localhost 比较重要些

    ❯ mvn clean install deploy
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -----------------------< com.ga.maven:repodemo >------------------------
    [INFO] Building repodemo 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ repodemo ---
    [INFO] Deleting /Users/louhan/study/maven/repodemo/target
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ repodemo ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ repodemo ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ repodemo ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ repodemo ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ repodemo ---
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ repodemo ---
    [INFO] Building jar: /Users/louhan/study/maven/repodemo/target/repodemo-0.0.1-SNAPSHOT.jar
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ repodemo ---
    [INFO] Installing /Users/louhan/study/maven/repodemo/target/repodemo-0.0.1-SNAPSHOT.jar to /Users/louhan/.m2/repository/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-SNAPSHOT.jar
    [INFO] Installing /Users/louhan/study/maven/repodemo/pom.xml to /Users/louhan/.m2/repository/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-SNAPSHOT.pom
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ repodemo ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ repodemo ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ repodemo ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ repodemo ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ repodemo ---
    [INFO] Skipping execution of surefire because it has already been run for this configuration
    [INFO]
    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ repodemo ---
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ repodemo ---
    [INFO] Installing /Users/louhan/study/maven/repodemo/target/repodemo-0.0.1-SNAPSHOT.jar to /Users/louhan/.m2/repository/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-SNAPSHOT.jar
    [INFO] Installing /Users/louhan/study/maven/repodemo/pom.xml to /Users/louhan/.m2/repository/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-SNAPSHOT.pom
    [INFO]
    [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ repodemo ---
    Downloading from nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/maven-metadata.xml
    Uploading to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-20230918.015512-1.jar
    Uploaded to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-20230918.015512-1.jar (1.5 kB at 11 kB/s)
    Uploading to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-20230918.015512-1.pom
    Uploaded to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/repodemo-0.0.1-20230918.015512-1.pom (818 B at 10 kB/s)
    Downloading from nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/maven-metadata.xml
    Uploading to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/maven-metadata.xml
    Uploaded to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/0.0.1-SNAPSHOT/maven-metadata.xml (770 B at 7.9 kB/s)
    Uploading to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/maven-metadata.xml
    Uploaded to nexus-snapshots: http://localhost:8081/repository/maven-snapshots/com/ga/maven/repodemo/maven-metadata.xml (280 B at 5.7 kB/s)
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  1.693 s
    [INFO] Finished at: 2023-09-17T21:55:12-04:00
    [INFO] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    当然,图示更明显一些:

    在这里插入图片描述

  • 相关阅读:
    【云原生】Docker容器服务更新与发现之consul
    【学习笔记】《深入浅出Pandas》第15章:Pandas样式
    Neo4j学习笔记(二) 查询语言
    Linux通过端口号找到对应的服务及其安装位置
    hosts文件地址
    Oracle数据库:使用 bash脚本 + 定时任务 自动备份数据
    AXI低功耗接口
    中秋之际献上【中秋快乐】藏头诗
    1.4-20:求一元二次方程的根
    【PTHREAD】线程互斥与同步之栅栏
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/133006753