Gradle Artifactory 插件 - 如何从项目中的多个模块发布工件?
Gradle Artifactory Plugin - How to publish artifacts from multiple modules in a project?
我有一个项目有一个 SharedCode
(Java) 模块,其次是一个 Android
(Android 库) 模块,它依赖于 SharedCode
模块。我想发布来自 SharedCode
模块的 jar
工件和来自 Android
模块的 aar
工件。我不知道如何编写我的 build.gradle
文件,以便在 artifactoryPublish
任务为 运行 时两个模块都发布到 Artifactory。目前只有 SharedCode
模块将其工件发布到 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
}
}
}
如果我 运行 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
工件。
如果我在 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
请注意,在这种情况下没有发布任何工件。
按照他们的 github 仓库中的人工制品多项目示例,似乎只有根项目需要有一个 artifactory{...}
配置部分,而不是像您那样在每个子项目中已经完成了。
此外,当您在根项目中声明 publications('SharedCode')
时,artifactory 插件似乎正在每个子项目中寻找名为 sharedCode
的发布。
我会尝试:
从 android build.gradle
中删除 artifactory{...}
部分
也将 android 出版物重命名为 sharedCode
(或两个项目中更通用的名称)
更新: 从 com.jfrog.artifactory Gradle plugin, publishing artifacts in a multi-module project just does not work. From personal experience, you're best just abandoning this plugin and using the standard maven-publish 插件的版本 4.6.0
开始,用于 Java 库模块和 Android 库模块.
---下面是我发布上述更新之前的原始答案---
所以我终于让这一切正常工作了!特别感谢@RaGe 一路帮助我。需要注意的关键点是 artifactory
块需要在项目的根级 build.gradle
文件中,而不是在各个模块的 build.gradle
文件中。此外,您需要将 artifactoryPublish.skip=true
添加到项目的根级 build.gradle
文件中。请参阅此 GitHub 存储库以获取完整但尽可能少的示例:
https://github.com/adil-hussain-84/SO-35851251-Multiproject-Artifactory-Publish
万一 link 停止工作,我也会在此处粘贴 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 存储库部署。现在您可以简单地为项目的不同模块定义一个具有不同存储库的工件闭包。
我有一个项目有一个 SharedCode
(Java) 模块,其次是一个 Android
(Android 库) 模块,它依赖于 SharedCode
模块。我想发布来自 SharedCode
模块的 jar
工件和来自 Android
模块的 aar
工件。我不知道如何编写我的 build.gradle
文件,以便在 artifactoryPublish
任务为 运行 时两个模块都发布到 Artifactory。目前只有 SharedCode
模块将其工件发布到 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
}
}
}
如果我 运行 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
工件。
如果我在 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
请注意,在这种情况下没有发布任何工件。
按照他们的 github 仓库中的人工制品多项目示例,似乎只有根项目需要有一个 artifactory{...}
配置部分,而不是像您那样在每个子项目中已经完成了。
此外,当您在根项目中声明 publications('SharedCode')
时,artifactory 插件似乎正在每个子项目中寻找名为 sharedCode
的发布。
我会尝试:
从 android build.gradle
中删除 也将 android 出版物重命名为
sharedCode
(或两个项目中更通用的名称)
artifactory{...}
部分
更新: 从 com.jfrog.artifactory Gradle plugin, publishing artifacts in a multi-module project just does not work. From personal experience, you're best just abandoning this plugin and using the standard maven-publish 插件的版本 4.6.0
开始,用于 Java 库模块和 Android 库模块.
---下面是我发布上述更新之前的原始答案---
所以我终于让这一切正常工作了!特别感谢@RaGe 一路帮助我。需要注意的关键点是 artifactory
块需要在项目的根级 build.gradle
文件中,而不是在各个模块的 build.gradle
文件中。此外,您需要将 artifactoryPublish.skip=true
添加到项目的根级 build.gradle
文件中。请参阅此 GitHub 存储库以获取完整但尽可能少的示例:
https://github.com/adil-hussain-84/SO-35851251-Multiproject-Artifactory-Publish
万一 link 停止工作,我也会在此处粘贴 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 存储库部署。现在您可以简单地为项目的不同模块定义一个具有不同存储库的工件闭包。