title: Gradle - plugin maven-publish: How to publish only specific publication to a repository
body: <p>We have two different artifacts that is going to be published to two different maven repositories.</p>
  
  <ul>
  <li>"<strong>ProjectXMergedWar</strong>" should be published to "<strong>MyMavenRepo1</strong>" (snapshots)</li>
  <li>"<strong>ProjectXJarRelease</strong>" should be published to "<strong>MyMavenRepo2</strong>" (release)</li>
  <li>"ProjectXMergedWar" should never be published to "MyMavenRepo2" (release)</li>
  <li>"ProjectXJarRelease" should never be published to "MyMavenRepo1" (snapshots)</li>
  </ul>
  
  <p>We use the plugin <a href="http://www.gradle.org/docs/current/userguide/publishing_maven.html">maven-publish</a> 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 <code>publish</code> and <code>publishRelease</code> is doing what we want, but there are tasks we don't want.</p>
  
  <p>Some solutions might be:</p>
  
  <ul>
  <li>Can we <strong>remove the unwanted tasks**?</strong></li>
  <li>Can we configure maven-publish to <strong>only generate two publish tasks</strong> (the wanted tasks*)?</li>
  <li>Can we call the <strong>correct classes directly</strong> (<code>repo.publish(artifact)</code> or something like that)?</li>
  </ul>
  
  <p>I have looked at the source code of <code>PublishToMavenRepository</code>. It seems like the action I want to achive is located in <code>protected void doPublish</code>.</p>
  
  <p>*<strong>Wanted tasks:</strong></p>
  
  <ul>
  <li>publishProjectXMergedWarPublicationToMyMavenRepo1Repository + generatePom</li>
  <li>publishProjectXJarReleasePublicationToMyMavenRepo2Repository + generatePom</li>
  </ul>
  
  <p>*<strong>*Unwanted tasks:</strong></p>
  
  <ul>
  <li>publishProjectXMergedWarPublicationToMyMavenRepo2Repository</li>
  <li>publishProjectXJarReleasePublicationToMyMavenRepo1Repository</li>
  </ul>
  
  <p>Gradle file:</p>
  
  <pre><code>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'
  ])
  </code></pre>
  
  <p>Output from tasks:</p>
  
  <pre><code>$ ./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
  (...)
  </code></pre>