在docker中部署并使用artifactory私有代码仓库

伴随着App规模的扩大,依赖关系越来越复杂,模块化的开发方式的需求就逐渐变得强烈,所以更多的时候我们希望把一些模块打包成aar包上传到maven仓库,这样便于我们使用和版本管理,同时编译速度也比多模块项目大幅提升。

首先弄懂maven和gradle是什么关系?
通常我们所的maven包含两个含义,一个是本地的编译工具,一个是服务端的代码仓库。而gradle是另一个编译工具,但是他代码仓库还是使用的maven。

安装配置

artifactory是jfrog推出的代码仓库程序,他不仅支持maven还支持pip和npm等。
首先安装artifactory

docker pull docker.bintray.io/jfrog/artifactory-oss:6.9.0

docker run --rm --name artifactory -d \
-v artifactory6_data:/var/opt/jfrog/artifactory \
-p 8081:8081 docker.bintray.io/jfrog/artifactory-oss:6.9.0

这样artifactory就运行在了8081端口上了

这时候我们需要对他进行配置

  1. 设置管理员账号?
    跳过,使用默认的admin/password,以后再改
  2. 设置代理
    跳过,artifactory支持使用了类似返现代理的工作方式去抓取并缓存jcenter等远端代码仓库,这个功能暂时用不上
  3. 新建代码仓库
    这里选择maven仓库,一切默认即可,会自动帮我们创建libs-release-locallibs-snapshot-local等仓库。

发布aar包

这里官方给出的方法并不简洁,网上流传的方法也有很多问题,这里直接贴出我的解决方案:
首先修改 ~/.gradle/gradle.properties,添加如下内容

artifactory_contextUrl=http\://yourip\:yourport/artifactory
artifactory_user=anmin
artifactory_repo_release=libs-release-local
artifactory_password=password
artifactory_repo_snapshot=libs-snapshot-local

uploadArtifactory.gradle:

//本脚本将编译后的aar上传私有artifactory仓库
//artifactory需要配置~/.gradle/gradle.properties
//import below in project.gradle
//buildscript {
//    ...
//    dependencies {
//        //Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
//        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.5"
//    }
//}

//deploy:
//./gradlew assembleRelease artifactoryPublish
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'


task testConfig {
    doLast {
        configurations.implementation.allDependencies.all {
            println it
        }
    }
}
task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    options {
        failOnError false
        encoding "UTF-8"
        charSet "UTF-8"
        addStringOption('Xdoclint:none', '-quiet')
    }
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompile.classpath
        }
    }
    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

publishing {
    publications {
        aar(MavenPublication) {
            //这三个属性需要按情况配置
            groupId project.group
            version = project.version
            artifactId project.name

            artifact bundleReleaseAar
            artifact androidJavadocsJar
            artifact androidSourcesJar
            //artifact file("$buildDir/outputs/aar/${project.name}-release.aar")
            //https://stackoverflow.com/questions/26874498/publish-an-android-library-to-maven-with-aar-and-source-jar
            pom.withXml {
                final dependenciesNode = asNode().appendNode('dependencies')

                ext.addDependency = { Dependency dep, String scope ->
                    if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                        return // ignore invalid dependencies

                    final dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dep.group)
                    dependencyNode.appendNode('artifactId', dep.name)
                    dependencyNode.appendNode('version', dep.version)
                    dependencyNode.appendNode('scope', scope)

                    if (!dep.transitive) {
                        // If this dependency is transitive, we should force exclude all its dependencies them from the POM
                        final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                        exclusionNode.appendNode('groupId', '*')
                        exclusionNode.appendNode('artifactId', '*')
                    } else if (!dep.properties.excludeRules.empty) {
                        // Otherwise add specified exclude rules
                        final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                        dep.properties.excludeRules.each { ExcludeRule rule ->
                            exclusionNode.appendNode('groupId', rule.group ?: '*')
                            exclusionNode.appendNode('artifactId', rule.module ?: '*')
                        }
                    }
                }

                // List all "compile" dependencies (for old Gradle)
                configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
                // List all "api" dependencies (for new Gradle) as "compile" dependencies
                configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                // List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
                configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
            }
        }
    }
}


artifactory {
    contextUrl = "${artifactory_contextUrl}"


    publish {
        repository {
            repoKey = project.version.endsWith('SNAPSHOT') ? "${artifactory_repo_snapshot}":"${artifactory_repo_release}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            // Tell the Artifactory Plugin which artifacts should be published to Artifactory.
            publications('aar')
            publishArtifacts = true

            // Properties to be attached to the published artifacts.
            properties = ['qa.level': 'basic', 'dev.team': 'core']
            // Publish generated POM files to Artifactory (true by default)
            publishPom = true
        }
    }
    resolve {
        repository {
            repoKey = "${artifactory_repo_release}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}


在Android libary模块的build.gradle中引用:apply from: "../deployArtifactory.gradle"

引用发布的aar包

引用很简单,不需要其他插件,直接像引用其他第三方仓库一样,例如:

allprojects {
    repositories {
        google()
        jcenter()
        maven{
            url uri("${userHome}/repo")//本地仓库 
        }
        maven {
            url "${artifactory_contextUrl}/libs-release-local" //私有仓库
        }
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
}
    原文作者:hglfNg
    原文地址: https://www.jianshu.com/p/0cb3c74c35f0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞