android - 如何使用 artifactoryPublish 发布发布和调试 Artifact

标签 android maven gradle artifactory

我的 Android Studio 项目可以在发布版和调试版中构建 AAR 或 APK。我想将这些发布到我的 Artifactory 服务器上不同的存储库。 JFrog examples似乎没有涵盖这种情况。

这是否意味着仅构建发布版本或仅构建调试版本并根据构建类型选择上传内容和上传位置被认为是最佳实践?

最佳答案

我配置了我的 android 库 build.gradle 文件,编译后的 aar 文件可以上传到不同的 repos,具体取决于构建类型。
例如,您想将调试 Artifact 发布到“libs-debug-local”存储库,并将 Artifact 发布到“libs-release-local”存储库。

//First you should configure all artifacts you want to publish
publishing {
    publications {

        //Iterate all build types to make specific 
        //artifact for every build type
        android.buildTypes.all { variant ->

            //it will create different 
            //publications ('debugAar' and 'releaseAar')
            "${variant.name}Aar"(MavenPublication) {
                def manifestParser = new com.android.builder.core.DefaultManifestParser()

                //Set values from Android manifest file
                groupId manifestParser.getPackage(android.sourceSets.main.manifest.srcFile)
                version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
                artifactId project.getName()

                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")
            }
        }
    }
}

//After configuring publications you should
//create tasks to set correct repo key
android.buildTypes.all { variant ->

    //same publication name as we created above
    def publicationName = "${variant.name}Aar"

    //new task name
    def taskName = "${variant.name}Publication"

    //in execution time setting publications and repo key, dependent on build type
    tasks."$taskName" << {
        artifactoryPublish {
            doFirst {
                publications(publicationName)
                clientConfig.publisher.repoKey = "libs-${variant.name}-local"
            }
        }
    }

    //make tasks assembleDebug and assembleRelease dependent on our new tasks
    //it helps to set corrent values for every task
    tasks."assemble${variant.name.capitalize()}".dependsOn(tasks."$taskName")
}

//Inside artifactory block just set url and credential, without setting repo key and publications
artifactory {
    contextUrl = 'http://artifactory.cooperok.com:8081/artifactory'
    publish {
        repository {
            username = "username"
            password = "password"
        }
        defaults {
            publishArtifacts = true

            // Properties to be attached to the published artifacts.
            properties = ['qa.level': 'basic', 'dev.team': 'core']
        }
    }
}

就是这样。现在,如果你运行命令

Win : gradlew assembleRelease artifactoryPublish Mac:./gradlew assembleRelease artifactoryPublish

aar 文件将上传到“libs-release-local”存储库。
如果你跑了

Win : gradlew assembleDebug artifactoryPublish Mac:./gradlew assembleDebug artifactoryPublish

它将被上传到'libs-debug-local' 仓库

此配置的一个缺点是您应该始终使用 assembleDebug/Release 任务运行 artifactoryPublish 任务

关于android - 如何使用 artifactoryPublish 发布发布和调试 Artifact ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949919/

相关文章:

gradle - 使用 Gradle 生成 uber/fat/shade 源-jar(和 javadoc-jar)

android - 模拟器:模拟器:警告:无法连接到 proxy at::1:8080:未知错误! - 安卓

Android DDMS - 将调用发送到实际设备

java - 将Amazon SimpleDB与Android结合使用

android - react-native run-android plugin not found and compile sdk is not specified

groovy - 让 Gradle 通知 GroovyShell 脚本所需的依赖项在哪里的正确方法是什么?

android - 无法将库添加到Android Studio中的项目

android - Drive API list 和与 Drive App 的集成

spring - 在 Maven 中的集成测试之间删除内存数据库中的 H2

Maven 构建模块乱序