Ability to change maven-publish's artifacts to publish at runtime


(Marcin Zajączkowski) #1

I tried to combine maven-publish plugin with release plugin. When maven-publish plugin is configured it reads version property which is set to 1.0.0-SNAPSHOT. Later a release plugin change it to 1.0.0, but maven-publish is already configured (and tried to push not existing foo-1.0.0-SNAPSHOT.jar to a repo). I would like to change at runtime.

I tried to add my task as a dependent of PublishToMavenRepository to modify artifacts to upload and a repository, but with no luck. It is possible to achieve (with maven-publish or old maven publishing plugin)?

Marcin

Update: In the end I was able to change my artifacts with:

tasks.withType(PublishToMavenRepository).all {task ->

task.doFirst { ctask ->

tasks.withType(Jar).all { jtask ->

ctask.publication.setArtifacts(configurations.archives.artifacts)

}

}

but then another problems appear (with wrong version in MavenProjectIdentity, wrong pom.xml, etc.). It would be much better to just postpone initialization of MavenPublication (or reinitialize it) until the moment when a project is already in a state changed by a release plugin. How can I do that?


#2

When does the release plugin update the version number? The publishing plugin is configured late during configuration, but before ‘afterEvaluate’.

You should be able to utilise the (very) new support for modifying publication coordinates for a MavenPublication (see the DSL docs for the latest nightly). You’ll need to modify the coordinates at the same time the release plugin updates the project version. (Doing it in the PublishToMavenRepository task won’t work, because the GenerateMavenPom task uses this info as well).


(Scott Palmer) #3

I am having similar issues, and I really wish someone from the Gradle team would take over the release plugin (same version as above) or at least use it internally so they see the many pain points there are.

What I finally ended up using is actually quite simple:

apply plugin: 'java'
apply plugin: 'maven-publish'
  // more project specific stuff goes in here
  task sourceJar(type: Jar) {
 classifier 'sources'
 from sourceSets.main.allJava
}
  task javadocJar(type: Jar, dependsOn: javadoc) {
 classifier = 'javadoc'
 from javadoc.destinationDir
}
  publishing {
 publications {
  mavenJava(MavenPublication) {
   from components.java
   artifact sourceJar
   artifact javadocJar
  }
 }
 repositories {
  maven {
   url { "http://${repoHost}/libs-${project.version.endsWith('-SNAPSHOT') ? 'snapshot' : 'release' }-local" }
  }
 }
}
  apply plugin: 'release'
createReleaseTag.dependsOn publish
  confirmReleaseVersion << {
 publishing.publications.mavenJava.version = project.version
 jar.version = project.version
 javadocJar.version = project.version
 sourceJar.version = project.version
 publishing.publications.mavenJava.setArtifacts([jar, sourceJar, javadocJar])
}

The release plugin changes the version number when executing an “unSnapshotVersion” or “confirmReleaseVersion” task by this time though the publishing task has already decided that the artifact it will publish is what the jar task would have produced with the previous version. So at the time it tries to publish, it can’t find an artifact with the old version number and so it fails.

Notice I also used a closure to configure the repo URL so that it get’s the right destination path for snapshots vs. proper releases. (I’m publishing to Artifactory.)

This concept of doing a release that bumps the version number, publishes the artifacts, and tags the source code is a very basic and necessary part of a large percentage of build processes. It would be great if Gradle supported it without resorting to a third-party plugin. I suggest rolling the townsfolk release plugin into the core Gradle plugins as a starting point. Let others make the version control plugins to handle tagging for various SCMs and such.