我们有两个不同的工件将发布到两个不同的maven存储库。
我们使用插件maven-publish来配置一组发布和reposistories。然后,插件为发布和存储库的所有组合生成任务(请参阅底部的任务列表)。目前,publish
和publishRelease
正在执行我们想要的任务,但我们不需要执行任务。
有些解决方案可能是:
repo.publish(artifact)
或类似的东西)吗?我查看了PublishToMavenRepository
的源代码。我想要获得的动作似乎位于protected void doPublish
。
* 通缉任务:
* *不需要的任务:
Gradle文件:
apply plugin: 'maven-publish'
publishing {
publications {
ProjectXMergedWar(MavenPublication) {
artifact mergeWar
artifactId = 'projectx'
}
ProjectXJarRelease(MavenPublication) {
artifact releaseJar
artifactId = 'projectx'
}
}
repositories {
maven {
name 'MyMavenRepo1'
url 'http://artifactory/url/our-snapshot-local'
credentials { (...) }
}
maven {
name 'MyMavenRepo2'
url 'http://artifactory/url/our-release-local'
credentials { (...) }
}
}
}
task publish(dependsOn: [
'generatePomFileForProjectXMergedWarPublication',
'publishProjectXMergedWarPublicationToMyMavenRepo1Repository'
], overwrite: true) {
// We override the normal publish which would have tried to publish all combinations of defined
// publications and repositories:
// - publishProjectXMergedWarPublicationToMyMavenRepo1Repository (we use this in normal snapshot publish)
// - publishProjectXMergedWarPublicationToMyMavenRepo2Repository (not to be used)
// - publishProjectXJarReleasePublicationToMyMavenRepo1Repository (not to be used)
// - publishProjectXJarReleasePublicationToMyMavenRepo2Repository (we use this one in publishRelease)
}
task publishRelease(dependsOn: [
'generatePomFileForProjectXJarReleasePublication',
'publishProjectXJarReleasePublicationToMyMavenRepo2Repository'
])
任务输出:
$ ./gradlew tasks
(...)
Publishing tasks
----------------
generatePomFileForProjectXJarReleasePublication - Generates the Maven POM file for publication 'ProjectXJarRelease'.
generatePomFileForProjectXMergedWarPublication - Generates the Maven POM file for publication 'ProjectXMergedWar'.
publishProjectXJarReleasePublicationToMavenLocal - Publishes Maven publication 'ProjectXJarRelease' to the local Maven repository.
publishProjectXJarReleasePublicationToMyMavenRepo1Repository - Publishes Maven publication 'ProjectXJarRelease' to Maven repository 'MyMavenRepo1'.
publishProjectXJarReleasePublicationToMyMavenRepo2Repository - Publishes Maven publication 'ProjectXJarRelease' to Maven repository 'MyMavenRepo2'.
publishProjectXMergedWarPublicationToMavenLocal - Publishes Maven publication 'ProjectXMergedWar' to the local Maven repository.
publishProjectXMergedWarPublicationToMyMavenRepo1Repository - Publishes Maven publication 'ProjectXMergedWar' to Maven repository 'MyMavenRepo1'.
publishProjectXMergedWarPublicationToMyMavenRepo2Repository - Publishes Maven publication 'ProjectXMergedWar' to Maven repository 'MyMavenRepo2'.
publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
(...)
Other tasks
-----------
(...)
publish
publishRelease
(...)
答案 0 :(得分:17)
您可以禁用和隐藏“无效”任务,如下所示:
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
name 'Dev'
url 'http://dev/'
credentials {
username 'username'
password 'password'
}
}
maven {
name 'Prod'
url 'http://prod/'
credentials {
username 'username'
password 'password'
}
}
}
publications {
// This will only be enabled on Dev
MyDevJar(MavenPublication) {
artifactId "test"
version "1.0"
groupId "org.example"
artifact file('abc')
ext.repo = 'Dev'
}
// This will only be enabled on prod
MyJar(MavenPublication) {
artifactId "test"
version "1.0"
groupId "org.example"
artifact file('abc')
ext.repo = 'Prod'
}
}
}
afterEvaluate {
tasks.withType(PublishToMavenRepository) { task ->
if (task.publication.hasProperty('repo') && task.publication.repo != task.repository.name) {
task.enabled = false
task.group = null
}
}
}
答案 1 :(得分:3)
我刚刚开始使用gradle和其他插件,@ knut-saua-mathiesen解决方案实际上是有趣的,但它不适用于我。
在'AfterEvaluate'中,task.publication未设置为其correcte值,但初始化为'null'。所以我尝试了其他一些方法并提出了这个解决方案:
afterEvaluate {
tasks.withType(PublishToMavenRepository).all { publishTask ->
publishTask.onlyIf { task ->
if (task.publication.hasProperty('repo') && task.publication.repo != task.repository.name) {
task.enabled = false
task.group = null
return false
}
return true
}
}
}
答案 2 :(得分:0)
问问题时可能并不存在,但是Gradle documentation仅描述了如何实现所需的有条件发布。
使用accepted answer中的方法<!-- Global site tag (gtag.js) - Google Ads: YYYYYYYYY -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-YYYYYYYYY"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-YYYYYYYYY');
</script>
<script>
gtag('config', 'AW-YYYYYYYYY/X_XXXXXXXXXXXXXXXXXX', {
'phone_conversion_number': '(XXX) XXX-XXXX'
});
</script>
,但同时也使用tasks.withType()
块。
“ Gradle允许您通过
onlyIf{}
方法根据条件跳过所需的任何任务。”
因此,他们的示例使用了存储库名称和发布类型的条件:
Task.onlyIf(org.gradle.api.specs.Spec)
他们对tasks.withType(PublishToMavenRepository) {
onlyIf {
(repository == publishing.repositories.external &&
publication == publishing.publications.binary) ||
(repository == publishing.repositories.internal &&
publication == publishing.publications.binaryAndSources)
}
}
tasks.withType(PublishToMavenLocal) {
onlyIf {
publication == publishing.publications.binaryAndSources
}
}
和publications
的定义如下:
repositories