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

我有一个项目,它有一个SharedCode (Java)模块,其次是一个依赖于SharedCode模块的Android (Android库)模块。 我想从SharedCode模块发布jar工件,并从Android模块发布aar工件。 我不知道如何build.gradle我的build.gradle文件,以便两个模块在artifactoryPublish任务运行时发布到Artifactory。 目前只有SharedCode模块将Artifact发布到Artifactory。

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

首先,我的SharedCode模块中的build.gradle文件如下所示:

 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 } } } 

其次,我的Android模块中的build.gradle文件如下:

 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 } } } 

如果我在根,项目级或者SharedCode模块级运行artifactoryPublish任务,那么我看到的输出如下:

 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工件。

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

 18:25:25: Executing external task 'artifactoryPublish'... Publication named 'SharedCode' does not exist for project ':Android' in task ':Android:artifactoryPublish'. :Android:artifactoryPublish 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/1457375127269 BUILD SUCCESSFUL 

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

Solutions Collecting From Web of "Gradle Artifactory插件 – 如何从项目中的多个模块发布工件?"

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

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

我会尝试:

  • 从android build.gradle中删除artifactory{...}部分

  • 将android发布重命名为sharedCode (或者两个项目中更通用)

所以我终于得到了这一切工作! 特别感谢@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' } 

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

 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) } } } } } 

第三,最后, SharedCode (Java)模块的build.gradle文件:

 apply plugin: 'java' apply plugin: 'maven-publish' dependencies { compile 'com.google.guava:guava:18.0' } publishing { publications { SomePublication(MavenPublication) { from components.java } } } 

而已!

Gradle Artifactory插件的4.2.0版已于上周发布,并添加了多个Artifactory存储库部署。 现在,您可以简单地为项目的不同模块定义一个artifactory闭包,并使用不同的存储库。