Gradle - getting the latest release version of a dependency

JavaMavenDependenciesIvyGradle

Java Problem Overview


What would be the easiest way to tell Gradle the following:

> Retrieve 'junit' dependency and take its latest 'release' version.

Managing Maven and Ivy repositories is sort of new to me. I tried the following steps and they result in Could not resolve dependency ... error:

  • Write compile "junit:junit:latest.release" with repositories set to only mavenCentral() (however, it works if I say "junit:junit:4.10").

  • Write compile "junit:junit:latest.release" with repository set the following way:

      ivy {
          // I also tried 'http://maven.org' and other possible variants.           
          url "http://repo1.maven.org" 
          layout "maven"
      }
    
  • Attempted to use Spring Source Ivy repository:

      ivy {
          artifactPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
          ivyPattern "http://repository.springsource.com/ivy/libraries/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"
      }
    

Maybe I misunderstand something. Why would getting the latest version of the dependency be such a hard task?

Java Solutions


Solution 1 - Java

It can be quite useful sometimes to get the latest release - if for example you release often your own dependencies.

You can get the latest version like

compile "junit:junit:+"

or better specify at least the major version like

compile "junit:junit:4.+"

Solution 2 - Java

Gradle currently does not support Maven's RELEASE (which is rarely used and deprecated) but it does support Ivy's latest.release (and for snapshots latest.integration). However, the general recommendation is to build against exact versions. Otherwise, the build can become a lottery.

Solution 3 - Java

Check out the Gradle-Versions-Plugin. It does exactly what you want: https://github.com/ben-manes/gradle-versions-plugin

For the installation, see the github page. Basically you need to add these two lines to your build.gradle - project file:

apply plugin: 'com.github.ben-manes.versions'

buildscript {
    [...]
    dependencies {
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.8'
        [...]
    }
}
[...]

Then you can use the plugin, by running this command in terminal in your project dir:

./gradlew dependencyUpdates -Drevision=release

And it will show you which dependencies are outdated!

Solution 4 - Java

Latest Gradle User Guide mentions and explains plus sign in versions:

From 7.2. Declaring your dependencies: > dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' testCompile group: 'junit', name: 'junit', version: '4.+' } ... The build script also states that any junit >= 4.0 is required to compile the project's tests.

From 23.7. How dependency resolution works: > If the dependency is declared as a dynamic version (like 1.+), Gradle will resolve this to the newest available static version (like 1.2) in the repository. For Maven repositories, this is done using the maven-metadata.xml file, while for Ivy repositories this is done by directory listing.

Solution 5 - Java

In Android Studio:

If you're using + for the version, and want to know which version is actually being used, select Project in the sidebar, and then under External Libraries you will see the actual version number in use.

Solution 6 - Java

Another similar notation for Kotlin DSL (build.gradle.kts):

dependencies {
    implementation("or.jsoup", "jsoup") {
        version {
            require("1.14.+")
        }
    }
    // OR simply
    // implementation("or.jsoup:jsoup:1.14.+")
}

Read more about this in Gradle documentations.

An excerpt from the docs:

> A dynamic version can be either a version range (e.g. 2.+) or it can be a placeholder for the latest version available e.g. latest.integration.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYippie-Ki-YayView Question on Stackoverflow
Solution 1 - JavajmrucView Answer on Stackoverflow
Solution 2 - JavaPeter NiederwieserView Answer on Stackoverflow
Solution 3 - Javaelectronix384128View Answer on Stackoverflow
Solution 4 - JavaRunninglVlanView Answer on Stackoverflow
Solution 5 - JavalenoohView Answer on Stackoverflow
Solution 6 - JavaMahozadView Answer on Stackoverflow