MartinTeeVarga
MartinTeeVarga

Reputation: 10898

How to get the latest version of a dependency from maven and use it as project's own version in Gradle

I have a suite of cucumber specifications that need to be running against the latest jar of a project. When the project is released with a specific version (e.g. 1.0.4), then I manually change the cucumber's gradle project version to match the jar version. I then use the project version as dependency version:

testCompile(group: 'groupId', name: 'theJar', version: version)

Is there any way I could get the latest version of the dependency in the repository and then use this version as cucumber gradle's project version?

version = getTheLatestVersionOfTheJar()

I would like to avoid creating a cucumber subproject in the main project, because of historical reasons.

Or if this is not possible, is there a way how to do this in TeamCity? That would be a workaround that I can live with. I.e. TeamCity would run a build. The build would produce a Jar and somehow store the version. The cucumber build would then be triggered with the version passed in.

Upvotes: 1

Views: 106

Answers (1)

Rene Groeschke
Rene Groeschke

Reputation: 28653

Why do you want to avoid the cucumber subproject. It sounds like both are so closely coupled that it would make a lot of sense and ease the pain of maintenance? Anyway put a method like below in your build script to dynamically resolve the latest version of a dependency from your repositories. But be aware of the costs of this method as does a dependency lookup every time it is invoked.

String getTheLatestVersionOfTheJar(){
    def versionResolveConfig = configurations.detachedConfiguration(dependencies.create("junit:junit:+"))
    versionResolveConfig.transitive = false
    return versionResolveConfig.resolvedConfiguration.firstLevelModuleDependencies[0].moduleVersion
} 

Upvotes: 1

Related Questions