Gradle - 插件maven-publish:如何仅将特定发布发布到存储库

时间:2014-01-29 13:59:23

标签: gradle maven-plugin

我们有两个不同的工件将发布到两个不同的maven存储库。

  • ProjectXMergedWar ”应发布到“ MyMavenRepo1 ”(快照)
  • ProjectXJarRelease ”应发布到“ MyMavenRepo2 ”(发布)
  • “ProjectXMergedWar”永远不应发布到“MyMavenRepo2”(发布)
  • “ProjectXJarRelease”永远不应发布到“MyMavenRepo1”(快照)

我们使用插件maven-publish来配置一组发布和reposistories。然后,插件为发布和存储库的所有组合生成任务(请参阅底部的任务列表)。目前,publishpublishRelease正在执行我们想要的任务,但我们不需要执行任务。

有些解决方案可能是:

  • 我们可以删除不需要的任务**?
  • 我们可以将maven-publish配置为只生成两个发布任务(想要的任务*)吗?
  • 我们可以直接调用正确的类repo.publish(artifact)或类似的东西)吗?

我查看了PublishToMavenRepository的源代码。我想要获得的动作似乎位于protected void doPublish

* 通缉任务:

  • publishProjectXMergedWarPublicationToMyMavenRepo1Repository + generatePom
  • publishProjectXJarReleasePublicationToMyMavenRepo2Repository + generatePom

* *不需要的任务:

  • publishProjectXMergedWarPublicationToMyMavenRepo2Repository
  • publishProjectXJarReleasePublicationToMyMavenRepo1Repository

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
(...)

3 个答案:

答案 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