【问题标题】:Gradle - plugin maven-publish: How to publish only specific publication to a repositoryGradle - 插件 maven-publish:如何仅将特定出版物发布到存储库
【发布时间】:2014-01-29 13:59:23
【问题描述】:

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

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

我们使用插件maven-publish,您可以在其中配置一组发布和存储库。然后,该插件会为出版物和存储库的所有组合生成任务(请参阅底部的任务列表)。目前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
(...)

【问题讨论】:

    标签: gradle maven-plugin


    【解决方案1】:

    您可以像这样禁用和隐藏“无效”任务:

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

    【讨论】:

    • 克努特,你的回答似乎不错,但由于某种原因它不再适合我了,如果你可以看一下,我已经添加了一个新帖子,其中包含我的解决方案。
    【解决方案2】:

    我刚开始玩 gradle 和其他插件,@knut-saua-mathiesen 解决方案真的很有趣,但它对我不起作用。

    在“AfterEvaluate”中,task.publication 没有设置为正确的值,而是初始化为“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                                                                                 
        }                                                                                             
      }                                                                                               
    }   
    

    【讨论】:

      【解决方案3】:

      可能在提出问题时这并不存在,但Gradle documentation 仅描述了如何实现所需的有条件的发布。

      使用 accepted answer 中的方法 tasks.withType(),但随后也使用 onlyIf{} 块。

      “Gradle 允许您通过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
          }
      }
      

      他们定义publicationsrepositories如下:

      publishing {
          publications {
              binary(MavenPublication) {
                  from components.java
              }
              binaryAndSources(MavenPublication) {
                  from components.java
                  artifact sourcesJar
              }
          }
          repositories {
              // change URLs to point to your repos, e.g. http://my.org/repo
              maven {
                  name = 'external'
                  url = "$buildDir/repos/external"
              }
              maven {
                  name = 'internal'
                  url = "$buildDir/repos/internal"
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-28
        • 2017-11-01
        • 2020-04-09
        相关资源
        最近更新 更多