Gradle resolutionStrategy不会按预期覆盖版本

时间:2017-04-21 10:12:21

标签: gradle dependencies

我在子项目中定义了一个依赖项,例如:

testCompile(
  "org.springframework:spring-test:4.3.7-RELEASE",
)

我的main build.gradle覆盖了resolutionStrategy:

subprojects {

  configurations {
    compileClasspath.transitive = false
    testCompileClasspath.transitive = false

    all*.resolutionStrategy {
      eachDependency { details ->
        def requested = details.requested

        switch (requested.group) {
           case 'org.springframework':
             details.useVersion '4.3.7-RELEASE'
             break
        }
      }
    }
  }
}

但依赖关系被解析为(片段):

+--- org.springframework:spring-test:4.3.7.RELEASE (*)
|    +--- org.springframework:spring-core:4.3.7.RELEASE
|    \--- org.springframework:spring-aop:4.3.7.RELEASE
|         +--- org.springframework:spring-beans:4.3.7.RELEASE
|         |    \--- org.springframework:spring-core:4.3.7.RELEASE
|         \--- org.springframework:spring-core:4.3.7.RELEASE
|    |    \--- org.springframework:spring-context:4.3.7.RELEASE
|    |         +--- org.springframework:spring-aop:4.3.7.RELEASE (*)
|    |         +--- org.springframework:spring-beans:4.3.7.RELEASE (*)
|    |         +--- org.springframework:spring-core:4.3.7.RELEASE
|    |         \--- org.springframework:spring-expression:4.3.7.RELEASE -> 4.3.0.RC2
|    |              \--- org.springframework:spring-core:4.3.0.RC2 -> 4.3.7.RELEASE

注意org.springframework:spring-expression:4.3.7.RELEASE -> 4.3.0.RC2

为什么会这样?

我正在使用Gradle 3.5

1 个答案:

答案 0 :(得分:0)

尝试一下:

all.resolutionStrategy {
  eachDependency { DependencyResolveDetails details ->
    if (details.requested.group == 'org.springframework') {
      details.useVersion '4.3.7-RELEASE'
    }
  }
}

取自https://docs.gradle.org/3.5/dsl/org.gradle.api.artifacts.ResolutionStrategy.html