Capture unique version of an artifact published to Maven


(Ryan Nelson) #1

I would like to capture the unique version of an artifact that Gradle creates when it publishes to a Maven repository. I’m using the old uploadArchives task as follows:

uploadArchives {
        repositories {
            mavenDeployer {
                repository(url: nexusUrl) {
                    authentication(userName: nexusUsername, password: nexusPassword )
                      pom.version = "1.0-SNAPSHOT"
                    pom.artifactId = "mything"
                    pom.groupId = "com.example"
                }
            }
        }
    }
}

This yields output like the following:

[ant:null] Deploying to https://mynexus.example.com/nexus-deps/content/repositories/snapshots
[INFO] Retrieving previous build number from remote
Uploading: com/example/mything/1.0-SNAPSHOT/mything-1.0-20140528.000250-425.jar to repository remote at https://mynexus.example.com/nexus-deps/content/repositories/snapshots

I’d like to capture “1.0-20140528.000250-425” for use in a CI build (Jenkins). I have been through the docs and the source code up and down, and can’t figure out how to do this. I’m open to any method: setting an environment variable, writing to disk, etc.

Any suggestions?


(Peter Niederwieser) #2

I think the unique snapshot version is generated by the Maven library leveraged by the ‘Upload’ task, and I’m not aware of a way to access it.


(Ryan Nelson) #3

Thanks, Peter. Yes, the logic seems buried inside CustomDeployTask which is used by the BaseMavenDeployer. I was hoping there was some way to get to it that I’d missed.

I’ve turned off unique versioning using “uniqueVersion = false”, but I can’t seem to set the version it uploads with. No matter what I try, it always publishes with “1.0-SNAPSHOT”. It’s extremely frustrating. Here’s what I’ve got so far:

def artifactVersion = "1.0-" + new SimpleDateFormat("yyyyMMdd.HHmmss").format(new Date())
  task zip(type: Zip) {
       from file("${buildDir}/unpacked/dist")
    baseName = 'mything-' + artifactVersion
}
  artifacts {
    archives zip
}
  version = artifactVersion
  uploadArchives {
        dependsOn zip
          repositories {
            mavenDeployer {
                repository(url: nexusUrl) {
                    authentication(userName: nexusUsername, password: nexusPassword )
                    pom.version = artifactVersion
                    pom.artifactId = "mything"
                    pom.groupId = "com.example"
                }
                uniqueVersion = false
            }
        }
    }
}

No matter where I specify artifactVersion, Gradle always outputs:

Uploading: com/example/mything/1.0-SNAPSHOT/mything-1.0-SNAPSHOT.zip to repository remote at https://mynexus.example.com/nexus-deps/content/repositories/snapshots

What setting can I use to override this vexing default?


#4

If you set “uniqueVersion = false”, then you’re telling Maven that every snapshot version will be published the literal ‘SNAPSHOT’ instead of a unique version. You cannot specify a version yourself in this case.


(Ryan Nelson) #5

So I guess I’m not sure how I get control over the version number. I need to be able to (1) either set it myself or (2) programmatically be able to access the one generated by Gradle and/or the underlying plugin. I think what I’m understanding from this thread is that this isn’t possible?


(Ryan Nelson) #6

I tried using the newer maven-publish plugin instead. My configuration looks as follows:

publishing {
        publications {
        maven(MavenPublication) {
            groupId 'com.example'
            artifactId 'mything'
            version artifactVersion
              artifact zip
        }
    }
    repositories {
        maven {
            url nexusUrl
            credentials {
                username = "username"
                password = "password"
            }
        }
    }
}

Inexplicably (to me, at least), Gradle still ignores the version I’m giving it:

:publishMavenPublicationToMavenRepository
Uploading: com/example/mything/1.0-SNAPSHOT/mything-1.0-20140609.202841-1.zip to repository remote at https://mynexus.example.com/nexus-deps/content/repositories/snapshots
Transferring 72917K from remote
Uploaded 72917K
:publish

Notice it still uses 1.0-SNAPSHOT as the version, and the zip file name is a string I don’t even recognize. That is NOT the string generated for my variable “artifactVersion.”

I noticed through some experimentation that the problem arises when “artifactVersion” begins with “1.0-”. If I remove those characters from the start of the string, Gradle publishes normally with the version I specify and everything appears to succeed.

Is there some strange behavior Gradle and/or the Maven plugin default to if it detects “1.0” in the version?


(Ryan Nelson) #7

Here’s a little more detail on this. It appears that anytime the version number begins with some variation of a digit and a dash, then Gradle automatically substitutes a SNAPSHOT in the path. Here’s a self-contained runnable example (note: requires a local file named fake.txt):

import java.text.SimpleDateFormat
  apply plugin: 'maven-publish'
  publishing {
     publications {
        maven(MavenPublication) {
            groupId 'temp1'
            artifactId 'temp2'
            version new SimpleDateFormat("yyyyMMdd.HHmmss").format(new Date()) + "-0"
              artifact file("${projectDir}/fake.txt")
        }
    }
    repositories {
        maven {
            url nexusUrl
            credentials {
                username = nexusUsername
                password = nexusPassword
            }
        }
    }
}

The output of this run includes:

Uploading: temp1/temp2/20140609.153248-0/temp2-20140609.153248-0.txt repository remote at https://mynexus.example.com/nexus-deps/content/repositories/snapshots

Now I change the code to add a number and a dash:

import java.text.SimpleDateFormat
  apply plugin: 'maven-publish'
  publishing {
     publications {
        maven(MavenPublication) {
            groupId 'temp1'
            artifactId 'temp2'
            version "1-" + new SimpleDateFormat("yyyyMMdd.HHmmss").format(new Date()) + "-0"
              artifact file("${projectDir}/fake.txt")
        }
    }
    repositories {
        maven {
            url nexusUrl
            credentials {
                username = nexusUsername
                password = nexusPassword
            }
        }
    }
}

The output of this run includes:

Uploading: temp1/temp2/1-SNAPSHOT/temp2-1-20140609.223414-2.txt to repository remote at https://mynexus.example.com/nexus-deps/content/repositories/snapshots

Notice the path has been changed, and the version in the filename no longer uses my version algorithm (there’s no “-0” at the end). I don’t know if this is Nexus or Gradle, but it seems like it’s Gradle. What am I missing?

BTW, this is Gradle 1.12.