Bilbo Baggins
Bilbo Baggins

Reputation: 3019

Gradle upgrades versions of dependency automatically

I am new to Gradle. I am facing the following problem.

  +--- org.springframework:spring-tx:5.0.5.RELEASE
|         |    +--- org.springframework:spring-beans:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         |    \--- org.springframework:spring-core:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         +--- org.springframework:spring-context:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         +--- org.springframework:spring-beans:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         +--- org.springframework:spring-core:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         +--- org.springframework:spring-expression:5.0.5.RELEASE -> 5.1.2.RELEASE (*)
|         +--- org.springframework.data:spring-data-commons:2.0.6.RELEASE -> 2.0.11.RELEASE
|         |    +--- org.springframework:spring-core:5.0.10.RELEASE -> 5.1.2.RELEASE (*)
|         |    +--- org.springframework:spring-beans:5.0.10.RELEASE -> 5.1.2.RELEASE (*)

As shown above, all of my dependencies are upgraded to the latest compatible versions. This is causing a compilation error in one of my test cases.

How can I ask Gradle to not to do this?

Using gradle version 3.0, if more details required I can post.

Please find the build.gradle file below:

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.abc.studentmanagement'
version = '1.0.0-SNAPSHOT'

description = """"""

project.sourceCompatibility = 1.8
project.targetCompatibility = 1.8

buildscript {

    ext {
        springBootVersion = '2.0.1.RELEASE'
        springRetryVersion = '1.2.2.RELEASE'
        springCloudStarterVersion = '2.0.0.RC1'
        springfoxSwaggerVersion = '2.9.2'
        guavaVersion = '20.0'
        mongoDBVersion = '3.10.1'
        lombokVersion = '1.16.20'
        swaggerRequestValidatorVersion = '1.3.9'
        referenceArchVersion = '5.3.2'
        junitVersion = '4.12'
        karateJUnitVersion = '0.9.4'
        refarchGradleCodegenPluginVersion = '1.0.2'

        springVersion = "5.0.6.RELEASE"
    }
    repositories {
        mavenLocal()
    }
}

repositories {
    mavenLocal()
}

dependencies {
    compile (group: 'com.abc.refarch', name: 'ref-arch-rest-api-base', version: referenceArchVersion){
        exclude(group: 'org.springframework')
    }
    compile (group: 'com.abc.refarch', name: 'ref-arch-rest-api-security', version: referenceArchVersion) {
        exclude(group: 'org.springframework')
    }
    compile (roup: 'com.abc.refarch', name: 'ref-arch-logging-web', version: referenceArchVersion) {
        exclude(group: 'org.springframework')
    }
    compile(group: 'com.abc.apie.restclient', name: 'restclient-sdk', version: '3.2.8') {
        exclude(module: 'json-smart')
    }
    compile group: 'com.abc.apie.restclient', name: 'restclient-sdk', version: '1.10.3'
    compile group: 'com.abc.apie.restclient', name: 'int-sdk', version: '1.4.3'
    compile group: 'io.swagger', name: 'swagger-parser', version: '1.0.23'
    compile group: 'commons-io', name: 'commons-io', version: '2.6'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    compile group: 'org.apache.camel', name: 'camel-jsonpath', version: '2.22.0'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: springBootVersion
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-vault-config', version: springCloudStarterVersion
    compile group: 'com.google.gdata', name: 'core', version: '1.47.1'
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.8.10'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
    compile group: 'io.pivotal.spring.cloud', name: 'spring-cloud-services-starter-config-client', version: springBootVersion
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: springBootVersion
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '2.0.0.RC1'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version: springBootVersion
    compile group: 'org.springframework.retry', name: 'spring-retry', version: springRetryVersion
    compile group: 'io.springfox', name: 'springfox-swagger2', version: springfoxSwaggerVersion
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: springfoxSwaggerVersion
    compile group: 'com.google.guava', name: 'guava', version: guavaVersion
    compile group: 'com.atlassian.oai', name: 'swagger-request-validator-core', version: swaggerRequestValidatorVersion
    compile group: 'org.mongodb', name: 'mongodb-driver', version: mongoDBVersion
    compile group: 'org.mongodb', name: 'mongodb-driver-core', version: mongoDBVersion
    compile group: 'org.mongodb', name: 'bson', version: mongoDBVersion
    compile group: 'com.abc.consumer.exception', name: 'exception-handler', version: '1.3.3-SNAPSHOT'
    compile(group: 'com.abc.apie.alter', name: 'alter-sdk', version: '2.0.5') {
        exclude(module: 'hibernate-validator')
    }
    compile(group: 'com.abc.refarch', name: 'ref-arch-messaging-base', version: '5.3.2') {
        exclude(module: 'alter-sdk')
        exclude(module: 'hibernate-validator')
    }
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
    testCompile group: 'com.abc.refarch', name: 'ref-arch-unit-test-jar', version: '5.3.2'
    testCompile(group: 'com.intuit.karate', name: 'karate-apache', version: karateJUnitVersion) {
        exclude(module: 'logback-classic')
    }
    testCompile group: 'com.intuit.karate', name: 'karate-junit4', version: karateJUnitVersion
    testCompile group: 'net.masterthought', name: 'cucumber-reporting', version: '3.20.0'
    compile(group: 'org.projectlombok', name: 'lombok', version: lombokVersion) {
        /* This dependency was originally in the Maven provided scope, but the project was not of type war.
        This behavior is not yet supported by Gradle, so this dependency has been converted to a compile dependency.
        Please review and delete this closure when resolved. */
    }
}

NOTE: I Have used gradle init to convert from maven to gradle project.

UPDATE-1 I noticed that this ref-arch-rest-api-base dependency has this version 5.1.2.RELEASE of spring.

No luck yet, can anyone help on this?

Upvotes: 2

Views: 1575

Answers (2)

Bilbo Baggins
Bilbo Baggins

Reputation: 3019

Okay, I finally found a work around, I still don't know why gradle was upgrading the version automatically. My work around goes as mentioned below.

  1. Imporated BOM as shown below.

dependencyManagement { imports { mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion" } } 2. And then include the dependencies, as and when required, these dependencies are defined in the dependency block. By doing this the version is not upgraded by Gradle automatically.

Just posting this as a reference to others.

Upvotes: 0

Louis Jacomet
Louis Jacomet

Reputation: 14500

In order to understand why a specific version of a dependency is used, run the dependencyInsight task, so something like:

./gradlew dependencyInsight --configuration compileClasspath --dependency spring-core

This will give you a reversed view compared to the one at the top of your question and you will be able to understand which dependency path causes the version upgrade.

However given your comment on the dependencies of ref-arch-rest-api-base, that's most likely the reason of the upgrade.

By default, Gradle will use all the versions it sees in the dependency graph and resolve conflicts on version by upgrading to the highest, which is usually safe.

You might need to downgrade ref-arch-rest-api-base to a version that works with Spring 5.0 if you strictly require that version.

Upvotes: 1

Related Questions