美文网首页
[技术] Nexus3配合Gradle搭建私有Maven仓库

[技术] Nexus3配合Gradle搭建私有Maven仓库

作者: ttdevs | 来源:发表于2017-11-05 22:18 被阅读454次

    [TOC]

    0x00 Nexus3配合Gradle搭建私有仓库

    场景:将自己的代码通过gradle上传到使用nexus3搭建的私有仓库。

    0x01 Nexus配置

    docker

    • 安装 dockerkitmatic

    • 安装 nexus3

      kitematic 搜索 nexus3,选择 Sonatype nexus3(Sonatype Nexus Repository Manager3)

    • 配置

      • 配置端口号
      • 配置存储目录

    nexus3

    • 默认管理员:admin:admin123

    • 修改管理员密码

      依次点击:右上角admin > Change password

    • 关闭匿名用户访问权限

      依次点击:顶部设置 > Administration > Security > Anonymous > Allow anonymous users to access the server

    • 创建仓库

      • ttdevs-releases

        Administration > Repository > Repositories > Create repository > maven2(hosted) > ttdevs-releases

        • Maven 2
          • Version policy: Release
          • Layout policy: Strict
        • Storage
          • Blob store: default
        • Hosted
          • Deployment policy: Disable redeploy
      • ttdevs-snapshot

        Administration > Repository > Repositories > Create repository > maven2(hosted) > ttdevs-snapshot

        • Maven 2
          • Version policy: Snapshot
            • Layout policy: Permissive
          • Storage
            • Blob store: default
          • Hosted
            • Deployment policy: Allow redeploy

      group, hosted, proxy

    • 创建角色

      Administration > Security > Roles > Nexus role

      • uploader

        • nx-repository-admin-maven2-ttdevs-release-*
        • nx-repository-view-maven2-ttdevs-release-*
      • snapshot

        • nx-repository-admin-maven2-ttdevs-snapshot-*
        • nx-repository-view-maven2-ttdevs-snapshot-*
      • reader

        • nx-repository-admin-maven2-ttdevs-release-browse
        • nx-repository-admin-maven2-ttdevs-release-read
        • nx-repository-view-maven2-ttdevs-release-browse
        • nx-repository-view-maven2-ttdevs-release-read
        • nx-repository-admin-maven2-ttdevs-snapshot-browse
        • nx-repository-admin-maven2-ttdevs-snapshot-read
        • nx-repository-view-maven2-ttdevs-snapshot-browse
        • nx-repository-view-maven2-ttdevs-snapshot-read
    • 创建用户

      Administration > Security > Users > Create user

      • Uploader

        赋予上步创建的 uploader 权限

      • Snapshot

        赋予上步创建的 snapshot 权限

      • Reader

        赋予上步创建的 reader 权限

    0x02 Android Library

    • 创建Project:AndroidLibrary

    • 创建Module:log,编写业务和测试代码

      如果此处有引用第三方代码,建议不要在后面添加 @aar !!!

    • 创建 config.gradle

      ext {
          nexusConfig = [
                  repository: 'http://ttdevs.vicp.cc:9020/repository/ttdevs-releases/',
                  repositorySnapshot: 'http://ttdevs.vicp.cc:9020/repository/ttdevs-snapshot/'
          ]
      }
      
    • 修改 gradle.properties

      nexusUploader=Uploader
      nexusUploaderPwd=uploader_password
      nexusSnapshot=Snapshot
      nexusSnapshotPwd=snapshot_password
      

      为了安全起见,上传的权限不放入git仓库

    • 修改 build.gradle,添加 apply from: 'config.gradle'

    • 创建 publish.gradle

      apply plugin: 'maven'
      
      uploadArchives {
          configuration = configurations.archives
          repositories {
              mavenDeployer {
                  repository(url: nexusConfig.repository) {
                      authentication(userName: nexusUploader, password: nexusUploaderPwd)
                  }
      
                  snapshotRepository(url: nexusConfig.repositorySnapshot) {
                      authentication(userName: nexusSnapshot, password: nexusSnapshotPwd)
                  }
                  
                  pom.project {
                      version project.version
                      artifactId project.name
                      groupId 'com.ttdevs.lib'
                      packaging 'aar'
                      description project.description
                  }
              }
          }
      
          task androidSourcesJar(type: Jar) {
              classifier = 'sources'
              from android.sourceSets.main.java.sourceFiles
          }
      
          artifacts {
              archives androidSourcesJar
          }
      }
      
    • 修改 log/build.gradle

      ext {
          project.version = '0.1.0'
      //   project.version = '0.1.0-SNAPSHOT'
          project.description = 'ttdevs log'
      }
      
      apply from: getRootDir().getAbsolutePath() + '/publish.gradle'
      
    • 上传

      ./gradlew :log:uploadArchives

      ➜  AndroidLibrary ./gradlew :log:uploadArchives
      Could not find metadata com.ttdevs.lib:log/maven-metadata.xml in remote (http://ttdevs.vicp.cc:9020/repository/ttdevs-releases/)
      
      BUILD SUCCESSFUL in 3s
      26 actionable tasks: 1 executed, 25 up-to-date
      ➜  AndroidLibrary 
      

    0x03 Android Demo

    • 创建Project:AndroidDemo
    • 修改 AndroidDemo/build.gradle
    ...
    allprojects {
        repositories {
            jcenter()
    
            maven {
                url "http://ttdevs.vicp.cc:9020/repository/ttdevs-releases/"
                credentials {
                    username 'Reader'
                    password 'ttdevs'
                }
            }
            maven {
                url "http://ttdevs.vicp.cc:9020/repository/ttdevs-snapshot/"
                credentials {
                    username 'Snapshot'
                    password 'ttdevs'
                }
            }
        }
    }
    ...
    
    • 添加引用

      修改 app/build.gradle 添加引用:

      implementation 'com.ttdevs.lib:log:0.1.0'

    0xFF 参考


    相关文章

      网友评论

          本文标题:[技术] Nexus3配合Gradle搭建私有Maven仓库

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