美文网首页
Docker之MAVEN私服

Docker之MAVEN私服

作者: felixfeijs | 来源:发表于2020-08-04 15:31 被阅读0次

    Docker之MAVEN私服

    目录

    • nexus简单介绍
    • Docker安装MAVEN nexus
    • Maven nexus批量上传jar包

    nexus简单介绍

    • Nexus 是Maven仓库管理器,可以在自有服务器上搭建自己的MAVEN仓库.

    Docker安装MAVEN nexus

    1. Docker安装
    2. 查询nexus安装镜像
      docker search nexus
    3. 下载镜像
      docker pull docker.io/sonatype/nexus3
    4. 查看镜像
      docker images
    5. 启动镜像
      docker run -d -p 8081:8081 -p 8082:8082 -p 8083:8083 --name nexus3 -v /opt/nexus-data:/nexus-data -u root --privileged=true --restart=always docker.io/sonatype/nexus3
    1. 在运行是可能会出现Permission denied的提示,这是因为容器目录和挂载目录的组名和用户名不同,导致使用对应的用户在宿主机目录下没有读写权限.
    1. 开放防火墙8081端口
      firewall-cmd --zone=public --add-port=8081/tcp --permanent
    2. 访问maven私服
      ip:8081
    3. 查看私服用户admin密码
      find / -name 'admin.password'
    • copy其中的内容为密码
    1. 登陆成功后进入设置代理仓库
    • 红色框是远程代理仓库,也就是本地仓库没有的jar,将会通过远程的仓库下载
      nexus之私服仓库地址.jpg
    1. 点击保存

    2. 创建本地用户

      nexus之用户创建.jpg
    3. 使用idea创建一个maven项目进行测试,pom中加入如下代码

     <distributionManagement>
            <repository>
                <id>nexus-releases</id>
                <url>http://ip:port/repository/maven-releases/</url>
            </repository>
            <snapshotRepository>
                <id>nexus-snapshots</id>
                <url>http://ip:port/repository/maven-snapshots/</url>
            </snapshotRepository>
        </distributionManagement>
    
    
    1. 配置配置$MAVEN_HOME/conf/settings.xml的nexus私服用户,id和pom的配置一致
    <server>
       <id>nexus-releases</id>
       <username>felixfei</username>
       <password>felixfeinexus</password>
    </server>
     
    <server>
       <id>nexus-snapshots</id>
       <username>felixfei</username>
       <password>felixfeinexus</password>
    </server>
    
    1. 配置完成之后使用 mvn deploy在idea开发工具中进行打包上传

    2. 登陆maven nexus进行查看如图


      打包并上传私服.jpg
    3. 创建demo项目并进行调用,如图


      jar-pom.jpg

    Maven nexus批量上传jar包

    1. 先将本地repository下的文件打包成一个zip包
    2. 使用sftp工具上传到linux /opt目录
    3. 使用unzip repository.zip命令进行解压
    4. 进入该目录cd /opt/repository
    5. 创建mavenimport.sh脚本,内容如下
      #!/bin/bash
      # copy and run this script to the root of the repository directory containing files
      # this script attempts to exclude uploading itself explicitly so the script name is important
      # 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}/{} ;
      
    6. 输入chmod a+x mavenimport.sh授权该脚本可执行
    7. 执行导入命令(地址和账号密码更换为自己的maven nexus信息)
      ./mavenimport.sh -u user -p password -r http://ip:port/repository/maven-releases/
    8. 执行完毕后进行刷新maven nexus即可看到

    相关文章

      网友评论

          本文标题:Docker之MAVEN私服

          本文链接:https://www.haomeiwen.com/subject/euswrktx.html