【问题标题】:How are some gradle dependencies working with no version supplied在没有提供版本的情况下,一些 gradle 依赖项如何工作
【发布时间】:2020-02-28 03:26:50
【问题描述】:

据我所知,gradle 在设置依赖项时需要版本号,但允许使用部分通配符。例如,如果我想要番石榴,我不能这样做,因为它失败了:

compile('com.google.guava:guava')

必须是(例如):

compile('com.google.guava:guava:21.0')

但是,我正在学习 Spring,它具有以下内容:

compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")

在没有提供版本的情况下,这些依赖项如何工作?

是不是因为以下原因,但我认为这些行仅对我的插件“org.springframework.boot”是必需的:

buildscript {
 repositories {
    mavenCentral()
 }
 dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
 }
}

【问题讨论】:

    标签: gradle


    【解决方案1】:

    值得一提的是,这个技巧被称为BOM材料清单),实际版本可以在spring-boot-dependencies内的相关POM文件中查看 包。此处的 Spring Boot 官方文档中提到了这一点:Build Systems

    Spring 提供此功能的另一种方式(对于非 Boot 项目)是通过 Spring Platform BOM,它实际上提供了 the following dependencies 的版本。

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
        }
    }
    
    apply plugin: 'io.spring.dependency-management'
    
    dependencyManagement {
        imports {
            mavenBom 'io.spring.platform:platform-bom:Athens-SR2'
        }
    }
    

    【讨论】:

    • 谢谢。实际上,我只是开始阅读引导参考指南来自学,但从未注意到这一点,我想我只是跳过了它,因为它可以正常工作:)
    • 你可以找到它使用的所有依赖版本here
    【解决方案2】:

    TL;DR - spring boot 使用自定义依赖解析器。

    使用以下代码应用的 spring boot 插件:

    apply plugin: 'spring-boot'
    

    处理列出的没有版本的依赖项。此逻辑在this 类中实现,该类将其委托给hereDependencyManagementPluginFeatures 被应用here

    【讨论】:

    • 这篇文章中的链接不再有效。仅供参考
    【解决方案3】:

    spring boot gradle plugin documentation 声明如下:

    您声明的 spring-boot gradle 插件的版本 确定 spring-boot-starter-parent bom 的版本是 导入(这确保构建始终是可重复的)。你应该 始终将 spring-boot gradle 插件的版本设置为实际版本 您希望使用的 Spring Boot 版本。

    【讨论】:

      【解决方案4】:

      Spring Boot Dependency Management Plugin 不是必需的。
      您可以使用build-in Gradle BOM support 代替Spring Boot Dependency Management Plugin

      例如:

          plugins {
              id 'java'
              id 'org.springframework.boot' version '2.1.0.RELEASE'
          }
          repositories {
              jcenter()
          }
          dependencies {
              implementation platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
      
          implementation 'org.springframework.boot:spring-boot-starter-web'
          implementation 'org.springframework.boot:spring-boot-starter-security'
          }
      

      对于多模块项目: 在根 build.gradle 中:

      plugins {
          id 'java-library'
          id 'org.springframework.boot' version '2.1.0.RELEASE'
      }
      
      
      allprojects {
          apply plugin: 'java-library'
      
          repositories {
              jcenter()
          }
      }
      
      dependencies {
          implementation project(':core')
          implementation 'org.springframework.boot:spring-boot-starter-web'
      }
      

      core/build.gradle

       dependencies {
          api platform('org.springframework.boot:spring-boot-dependencies:2.1.0.RELEASE')
          implementation 'org.springframework.boot:spring-boot-starter-web'
          testImplementation 'org.springframework.boot:spring-boot-starter-test'
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-10
        • 2017-06-16
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2017-06-22
        • 2014-06-25
        相关资源
        最近更新 更多