Gradle中的库版本已降级

时间:2020-07-18 19:47:41

标签: gradle

执行时(在gradle 6.5中)

./gradlew dependencyInsight --dependency groovy-testng --configuration testRuntimeClasspath

我发现groovy-testng来自groovy-all库,该库已添加到我们的build.gradle中。我想更新groovy-testng的版本,所以我决定更新groovy-all,根据mvnrepository,它在版本3.0.4中包含groovy-testng,但仍然是groovy-testng的版本是旧版本,而gradle无法将其解析为最新版本:

org.codehaus.groovy:groovy-testng:2.5.12 (selected by rule)
   variant "runtime" [
      org.gradle.status              = release (not requested)
      org.gradle.usage               = java-runtime
      org.gradle.libraryelements     = jar
      org.gradle.category            = library

      Requested attributes not found in the selected variant:
         org.gradle.dependency.bundling = external
         org.gradle.jvm.version         = 11
   ]

org.codehaus.groovy:groovy-testng:3.0.4 -> 2.5.12
\--- org.codehaus.groovy:groovy-all:3.0.4
     \--- testRuntimeClasspath

我在项目中找到了行selected by rule,但找不到任何ResolutionStrategy,所以我开始注释掉,看看是什么原因引起的。原来是org.springframework.boot插件与io.spring.dependency-management一起导致该版本降级。为什么?而且为什么只有当两个都包括在内?我假设这些插件定义了一些ResolutionStrategy?找出ResolutionStrategy来自哪里的最简单方法是什么?

1 个答案:

答案 0 :(得分:1)

Spring Dependency Management插件相当繁重。如果您是使用--info-i构建项目的,那么您会看到很多这样的日志:

Applying dependency management to configuration 'bootArchives' in project 'demo'
Applying dependency management to configuration 'archives' in project 'demo'
Applying dependency management to configuration 'default' in project 'demo'
Applying dependency management to configuration 'compile' in project 'demo'
Applying dependency management to configuration 'implementation' in project 'demo'
Applying dependency management to configuration 'runtime' in project 'demo'
Applying dependency management to configuration 'compileOnly' in project 'demo'

根据我的经验,依赖管理插件将赢得/强迫自己赢得胜利。

我在您的代码段中看到您想要3.0.4的Groovy,但是Gradle解决了2.5.12。如果查看Spring Boot Dependencies BOM,则会看到2.5.12是Spring Boot 2.3.1的当前版本:https://github.com/spring-projects/spring-boot/blob/2.3.x/spring-boot-project/spring-boot-dependencies/build.gradle#L365..L371

Spring Boot Gradle插件检测是否存在Spring依赖性管理插件,如果存在,则配置该插件以导入Spring Boot依赖性BOM:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/DependencyManagementPluginAction.java

通过查看BOM表:https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.3.1.RELEASE/spring-boot-dependencies-2.3.1.RELEASE.pom

您应该能够像这样覆盖Groovy版本:

ext {
  set("groovy.version", "3.0.4")
}

Spring依赖管理插件应该接管并应用3.0.4

如果那不能解决您的问题,那么您需要在这里解决其他插件或配置。

我还建议观看Managing Dependencies for Spring Projects with Gradle,以了解Spring依赖管理插件和Gradle的本机依赖管理之间的区别。

相关问题