Gradle: Override transitive dependency by version classifier

后端 未结 6 1339
囚心锁ツ
囚心锁ツ 2020-12-28 12:42

One of the dependencies declared in my project has a transitive dependency on \'com.google.guava:guava:15.0\'. But my application deployed on WAS/Weblogic doesn

相关标签:
6条回答
  • 2020-12-28 13:19

    Gradle 4.5.1 has the function DependencySubstitutions. Here an example to replace a dependency:

    configurations.each {
        c -> c.resolutionStrategy.dependencySubstitution {
            all { DependencySubstitution dependency ->
                if (dependency.requested.group == 'org.json') {
                    dependency.useTarget 'com.vaadin.external.google:android-json:0.0.20131108.vaadin1'
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-28 13:21
    implementation( group: 'commons-codec', name: 'commons-codec'){
          version{
            strictly "[1.15]"
          }
       }
    

    This works for me with gradle 6.6.1

    The documentation link for strictly can found here https://docs.gradle.org/current/userguide/rich_versions.html#rich-version-constraints

    0 讨论(0)
  • 2020-12-28 13:34

    Currently classifiers are not yet taken into account when it comes to resolutionStrategies. A workaround for you might excluding the transitive Guava library when declaring your dependencies and adding the Guava cdi1.0 version explicitly:

    dependencies {
        compile ("org.acme:someDependency:1.0"){
            exclude group: 'com.google.guava', module: 'guava'
        }       
        compile "com.google.guava:guava:15.0:cdi1.0"
    }
    
    0 讨论(0)
  • 2020-12-28 13:38

    I came across a more elegant approach which is simply:

    compile ("com.google.guava:guava:15.0:cdi1.0") {
      force = true
    }
    

    Explanation

    Setting force = true for a dependency tells gradle to use the specified version in case of a version conflict

    0 讨论(0)
  • 2020-12-28 13:38

    Since force = true is deprecated, relevant solution is to use strictly(...) version, e.g.:

    dependencies {
        // no need to exclude transitive spring-data-relational from this dependency
        implementation("org.springframework.data", "spring-data-r2dbc", "1.1.0.RC1")
    
        implementation("org.springframework.data", "spring-data-relational").version {
            strictly("2.0.0.RC1")
        }
    }
    

    P.S. tested on Gradle 6.3

    0 讨论(0)
  • 2020-12-28 13:46

    This will not work if the same dependency is pointed by some other jar. Sureshot way to exclude the dependency

    configurations {
     all*.exclude group: 'com.google.guava', module:'guava-jdk5'
    }
    
    0 讨论(0)
提交回复
热议问题