Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - plugin maven-publish: How to publish only specific publication to a repository

Tags:

We have two different artifacts that is going to be published to two different maven repositories.

  • "ProjectXMergedWar" should be published to "MyMavenRepo1" (snapshots)
  • "ProjectXJarRelease" should be published to "MyMavenRepo2" (release)
  • "ProjectXMergedWar" should never be published to "MyMavenRepo2" (release)
  • "ProjectXJarRelease" should never be published to "MyMavenRepo1" (snapshots)

We use the plugin maven-publish where you configure a set of publications and reposistories. The plugin then generates tasks for all combinations of publications and repositories (see tasks list at the bottom). Currently the tasks publish and publishRelease is doing what we want, but there are tasks we don't want.

Some solutions might be:

  • Can we remove the unwanted tasks**?
  • Can we configure maven-publish to only generate two publish tasks (the wanted tasks*)?
  • Can we call the correct classes directly (repo.publish(artifact) or something like that)?

I have looked at the source code of PublishToMavenRepository. It seems like the action I want to achive is located in protected void doPublish.

*Wanted tasks:

  • publishProjectXMergedWarPublicationToMyMavenRepo1Repository + generatePom
  • publishProjectXJarReleasePublicationToMyMavenRepo2Repository + generatePom

**Unwanted tasks:

  • publishProjectXMergedWarPublicationToMyMavenRepo2Repository
  • publishProjectXJarReleasePublicationToMyMavenRepo1Repository

Gradle file:

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' ]) 

Output from tasks:

$ ./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 (...) 
like image 209
HNygard Avatar asked Jan 29 '14 13:01

HNygard


2 Answers

You could disable and hide the "invalid" tasks like so:

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         }     } } 
like image 143
Knut Saua Mathiesen Avatar answered Oct 24 '22 13:10

Knut Saua Mathiesen


I've just started playing with gradle and it's other plugins and @knut-saua-mathiesen solution was really interresting, however it wasn't working for me.

Inside the 'AfterEvaluate' the task.publication wasn't set to it's correcte value but initialized to 'null'. So i tried a few other things and came up with this solution :

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                                                                                      }                                                                                                }                                                                                                }    
like image 36
Lifely Avatar answered Oct 24 '22 11:10

Lifely