如何使用artifactoryPublish发布发布和调试工件

时间:2015-08-11 19:02:34

标签: android maven gradle artifactory

我有Android Studio项目,可以在发布版本和调试版本中构建AAR或APK。我想将这些发布到我的Artifactory服务器上的不同存储库中。 JFrog examples似乎不包括此案例。

这是否意味着仅仅构建版本或仅构建调试版本,并根据构建类型选择上载内容和位置,这被认为是最佳做法?

6 个答案:

答案 0 :(得分:8)

我配置了我的android库build.gradle文件,编译的aar文件可以上传到不同的repos中,具体取决于构建类型。
例如,您希望将调试artifats发布到< libs-debug-local'存储库和发布工件到' libs-release-local'库。

(struct vector (x y z)
  #:methods gen:custom-write
  [(define (write-proc vector port mode)
     (let ([print (if mode write display)])
       (parameterize ([current-output-port port]) ;; <== new
         (write-string "<")
         (print (vector-x vector))
         (write-string ", ")
         (print (vector-y vector))
         (write-string ", ")
         (print (vector-z vector))
         (write-string ">"))))])

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

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

aar文件将上传到“libs-release-local&#39;库。
如果你跑了

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

它将上传到&#39; libs-debug-local&#39;存储库

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

答案 1 :(得分:1)

试试这个: -

def runTasks = gradle.startParameter.taskNames

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            if ('assembleRelease' in runTasks)
                repoKey = "${artifactory_repository_release}"
            else if ('assembleDebug' in runTasks)
                repoKey = "${artifactory_repository_debug}"

            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publishArtifacts = true
            if ('assembleRelease' in runTasks)
                publications("${artifactory_publication_release}")
            else if ('assembleDebug' in runTasks)
                publications("${artifactory_publication_debug}")
            publishPom = true
            publishIvy = false
        }
    }
}

其中artifactory_repository_release = libs-release-local和artifactory_repository_debug = libs-debug-local

要在其上发布库arr的artifactory仓库。

答案 2 :(得分:1)

将草稿更新为&#39; com.android.tools.build:gradle:3.x.x &#39; this不再适合我。

我的最终解决方案是:

artifactory {
    contextUrl = ARTIFACTORY_URL
    //The base Artifactory URL if not overridden by the publisher/resolver
    publish {
        repository {
            File debugFile = new File("$buildDir/outputs/aar/${SDK_NAME}-debug.aar");
            if ( debugFile.isFile() )
                repoKey = 'libs-snapshot-local'
            else
                repoKey = 'libs-release-local'

            username = ARTIFACTORY_USER
            password = ARTIFACTORY_PWD
            maven = true
        }
        defaults {
            File debugFile = new File("$buildDir/outputs/aar/${SDK_NAME}-debug.aar");
            if ( debugFile.isFile() )
                publications("debugAar")
            else
                publications("releaseAar")

            publishArtifacts = true
            // Properties to be attached to the published artifacts.
            properties = ['qa.level': 'basic', 'dev.team': 'core']
            // Is this even necessary since it's TRUE by default?
            // Publish generated POM files to Artifactory (true by default)
            publishPom = true
        }
    }
}

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) {
                writeNewPom(variant.name)
                groupId GROUP_NAME
                artifactId SDK_NAME
                version variant.name.endsWith('debug') ? VERSION_NAME + "-SNAPSHOT" : VERSION_NAME
                // Tell maven to prepare the generated "*.aar" file for publishing
                artifact("$buildDir/outputs/aar/${SDK_NAME}-${variant.name}.aar")
            }
        }
    }
}

def writeNewPom(def variant) {
    pom {
        project {
            groupId GROUP_NAME
            artifactId SDK_NAME
            version variant.endsWith('debug') ? VERSION_NAME + "-SNAPSHOT" : VERSION_NAME
            packaging 'aar'

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')

        configurations.api.allDependencies.each {dependency ->
            if (dependency.group != null) {
                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', dependency.group)
                dependencyNode.appendNode('artifactId', dependency.name)
                dependencyNode.appendNode('version', dependency.version)
            }
        }
    }.writeTo("$buildDir/publications/${variant}Aar/pom-default.xml")
}

答案 3 :(得分:1)

您无需执行复杂的Groovy代码即可实现此目的。我有这个用于build.gradle项目:

artifactory {
    contextUrl = 'https://artifactory.test.com/artifactory'
    publish {
        repository {
            repoKey = 'gradle-testing-local'
            username = artifactory_username
            password = artifactory_password
        }
        defaults {
            publications('debugAar')
            publications('releaseAar')
            publishArtifacts = true

            properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
            publishPom = true
        }
    }
}

这是我的模块build.gradle:

publishing {
    publications {
        android.buildTypes.all { variant ->
            "${variant.name}Aar"(MavenPublication) {
                groupId libraryGroupId
                version libraryVersion
                artifactId "library-${variant.name}"

                artifact("$buildDir/outputs/aar/library-${variant.name}.aar")
            }
        }
    }
}

在此处要注意的一个关键,无论您将什么内容放入build.gradle的artifactory.publish.defaults中的publications()下,都必须匹配模块build.gradle的循环:“ $ {variant.name} Aar”(MavenPublication)

另一件事是您还设置了artifactory_username和artifactory_password (来自Artifactory的加密版本)在〜/ .gradle / gradle / gradle.properties中。

答案 4 :(得分:0)

对于快照+发布版本,您可以使用sonatype(aka mavencentral)。几个星期后我做了一个简短的指南,你可能会发现它有些用处。 How to publish Android AARs - snapshots/release to Mavencentral

答案 5 :(得分:0)

无法通过@cooperok回答出版工作,否则它会帮助我。

这是我的代码:

NULL