Gradle Artifactory插件 - 如何从项目中的多个模块发布工件?

时间:2016-03-07 18:32:24

标签: android gradle build.gradle artifactory maven-publish

我有一个项目,它有SharedCode(Java)模块,其次是Android(Android库)模块,它取决于SharedCode模块。我想从jar模块发布SharedCode工件,从aar模块发布Android工件。我无法弄清楚如何编写我的build.gradle文件,以便在artifactoryPublish任务运行时两个模块都发布到Artifactory。目前只有SharedCode模块将其工件发布到Artifactory。

我的build.gradle文件如下。请注意,maven-publish文件的build.gradle方面似乎是正确的,因为当我运行publishToMavenLocal任务时,我会看到本地Maven文件夹中两个模块的工件(即{{1} })。

首先,我'~/.m2/repository'模块中的build.gradle文件如下:

SharedCode

其次,我apply plugin: 'java' apply plugin: 'maven-publish' apply plugin: 'com.jfrog.artifactory' group = "${projectGroupId}" version = "${projectVersionName}" dependencies { compile 'com.google.guava:guava:18.0' } publishing { publications { SharedCode(MavenPublication) { groupId "${projectGroupId}" artifactId 'SharedCode' version "${projectVersionName}" from components.java } } } artifactory { contextUrl = "${artifactory_url}" publish { repository { repoKey = 'libs-release-local' username = "${artifactory_username}" password = "${artifactory_password}" } defaults { publications('SharedCode') publishArtifacts = true properties = ['qa.level': 'basic', 'dev.team': 'core'] publishPom = true } } } 模块中的build.gradle文件如下:

Android

如果我在根目录,项目级别或apply plugin: 'com.android.library' apply plugin: 'maven-publish' apply plugin: 'com.jfrog.artifactory' group = "${projectGroupId}" version = "${projectVersionName}" android { // android stuff here... } dependencies { compile project(':SharedCode') } publishing { publications { Android(MavenPublication) { groupId "${projectGroupId}" artifactId 'Android' version "${projectVersionName}" artifact "$buildDir/outputs/aar/Android-release.aar" } } } artifactory { contextUrl = "${artifactory_url}" publish { repository { repoKey = 'libs-release-local' username = "${artifactory_username}" password = "${artifactory_password}" } defaults { publications('Android') publishArtifacts = true properties = ['qa.level': 'basic', 'dev.team': 'core'] publishPom = true } } } 模块级别运行artifactoryPublish任务,那么我看到输出如下:

SharedCode

请注意,在这种情况下只发布18:23:38: Executing external task 'artifactoryPublish'... Publication named 'SharedCode' does not exist for project ':Android' in task ':Android:artifactoryPublish'. :SharedCode:generatePomFileForSharedCodePublication :SharedCode:artifactoryPublish Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/mycompany/sdk/SharedCode/0.0.2/SharedCode-0.0.2.jar Deploying artifact: http://localhost:8081/artifactory/libs-release-local/com/mycompany/sdk/SharedCode/0.0.2/SharedCode-0.0.2.pom Deploying build descriptor to: http://localhost:8081/artifactory/api/build Build successfully deployed. Browse it in Artifactory under http://localhost:8081/artifactory/webapp/builds/client-sdk/1457375019604 BUILD SUCCESSFUL 工件。

如果我在SharedCode模块级别运行artifactoryPublish任务,那么我看到输出如下:

Android

请注意,在这种情况下不会发布任何工件。

3 个答案:

答案 0 :(得分:5)

  

更新:自4.6.0 Gradle插件版本com.jfrog.artifactory发布多模块项目中的发布工件   不行。从个人经验来看,你最好放弃这个   插件并使用Java库的标准maven-publish插件   模块和Android的digital.wup.android-maven-publish插件   图书馆模块。

所以我终于把这一切都搞定了!特别感谢@RaGe帮助我一路走来。需要注意的关键点是artifactory块需要位于项目的根级build.gradle文件中,而不是位于各个模块的build.gradle文件中。此外,您需要将artifactoryPublish.skip=true添加到项目的根级build.gradle文件中。请参阅此GitHub仓库以获取完整的,尽可能最小的示例:

https://github.com/adil-hussain-84/SO-35851251-Multiproject-Artifactory-Publish

如果链接停止工作,我也会粘贴build.gradle文件的内容。首先,项目的根级build.gradle文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.0.1'
    }
}

allprojects {
    apply plugin: 'com.jfrog.artifactory'

    repositories {
        jcenter()
    }

    group = "${projectGroupName}"
    version = "${projectVersionName}"
}

artifactoryPublish.skip=true

artifactory {
    contextUrl = "${artifactory_url}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_username}"
            password = "${artifactory_password}"
        }
        defaults {
        publications('SomePublication')
            publishArtifacts = true
            properties = ['qa.level': 'basic', 'dev.team': 'core']
            publishPom = true
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.8'
}

其次,build.gradle模块的Android文件:

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 23
        versionCode Integer.parseInt("${projectVersionCode}")
        versionName "${projectVersionName}"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(':SharedCode')
    compile 'com.android.support:appcompat-v7:23.2.1'

    testCompile 'junit:junit:4.12'
}

publishing {
    publications {
        SomePublication(MavenPublication) {
            artifact "$buildDir/outputs/aar/Android-release.aar"

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.compile.allDependencies.each {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }
        }
    }
}

第三个也是最后一个build.gradle(Java)模块的SharedCode文件:

apply plugin: 'java'
apply plugin: 'maven-publish'

dependencies {
    compile 'com.google.guava:guava:18.0'
}

publishing {
    publications {
        SomePublication(MavenPublication) {
            from components.java
        }
    }
}

就是这样!

答案 1 :(得分:3)

Gradle Artifactory插件版本4.2.0于上周发布,并添加了多个Artifactory存储库部署。现在,您只需为项目的不同模块定义具有不同存储库的工件闭包。

答案 2 :(得分:2)

通过github repo上的神器多项目示例,似乎只有根项目需要有一个artifactory{...}配置部分,而不是像你所做的那样在每个子项目中。

此外,当您在根项目中声明publications('SharedCode')时,artifactory插件似乎在每个子项目中寻找名为sharedCode的发布。

我会尝试:

  • 从android build.gradle

  • 中删除artifactory{...}部分
  • 将android出版物重命名为sharedCode(或两个项目中更通用的内容)