在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' }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容