How can I grab the previous version of a project's jar?


(jim.holmes) #1

We are publishing artifacts to an Ivy repository in Artifactory. We have the requirement that new versions of jars are only published if they have changed from the previous version. Whether an artifact has changed is determined by a separate in-house tool with its own set of rules. The tool simply takes the previous and current jars and compares them.

I was assuming I could grab the previous jar by simply resolving the ‘latest.integration’ version of the current artifact from the repo since the new one hadn’t been published yet:

task difference {
    project.configurations {
        differencing
    }
      project.dependencies {
        differencing "$project.group:$project.name:latest.integration"
    }
      doLast {
        println project.configurations.differencing.singleFile
    }
}

When I run this task, the result of the configuration resolve always points to the output of the current project’s jar task (whether that jar exists yet or not). This is true no matter what version I request, so it appears Gradle is short-circuiting the resolve if the group and name match a current project of the build.

I understand why Gradle might do that, such as someone accidentally adding an artifact dependency on a subproject rather than an explicit project dependency. Is there a way i can bypass this behavior and resolve a previous version?


Fetching the previous version of a project's jar
(jim.holmes) #2

Any ideas? There’s got to be a way to do this…


(Nikolay Nachev) #3

You can try this:

println project.configurations.differencing.dependencies.findAll{ it.name=="${project.name}"}.collect{ it.version}

(jim.holmes) #4

Given my example above, this will just return a map containing ‘latest.integration’. I need the previous published artifact.


(jim.holmes) #5

I’m still struggling with this. I’ve investigated just about every API path off of a configuration and I can’t figure out how to resolve the previous published version of a project’s jar. The closest thing I’ve found is a ComponentSelectionRule:

configurations.differencing.resolutionStrategy {
    componentSelection {
        all { ComponentSelection selection ->
            println selection.candidate.version
        }
    }
}

This actually prints out the previous version of the jar. This is encouraging since it tells me that Gradle did in fact resolve against the Ivy repo to discover that version and obviously has that knowledge stored somewhere. However, the only thing I can do here is reject it. Assuming I did know the actual version string ahead of time, it doesn’t seem to help. Even resolutionStrategy.force() with the specific version will still result in the configuration resolving to the current project jar.

At this point, i’m open to any solution even if it involves internal APIs.


Fetching the previous version of a project's jar