一些gradle依赖项如何在没有提供版本的情况下工作

Nei*_*ker 19 gradle

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

compile('com.google.guava:guava')
Run Code Online (Sandbox Code Playgroud)

它必须(作为一个例子):

compile('com.google.guava:guava:21.0')
Run Code Online (Sandbox Code Playgroud)

但是,我正在学习Spring,它有以下几点:

compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
Run Code Online (Sandbox Code Playgroud)

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

是因为以下内容,但我认为只有我的插件'org.springframework.boot'需要这些行:

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

hak*_*iri 17

值得一提的是,招被称为BOM(物料清单)和实际版本可以在相关检查POM内部文件弹簧引导的依赖包.这在Spring Boot官方文档中提到:Build Systems.

Spring提供的另一种方式(对于非Boot项目)是通过Spring Platform BOM实际提供以下依赖项的版本.

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'
    }
}
Run Code Online (Sandbox Code Playgroud)


Opa*_*pal 9

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

一个spring boot插件,应用了以下代码:

apply plugin: 'spring-boot'
Run Code Online (Sandbox Code Playgroud)

处理没有版本列出的依赖项.这个逻辑在这个类中实现,它将它委托给这里.这里DependencyManagementPluginFeatures适用.


小智 7

春天的Gradle启动插件文件规定如下:

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


wak*_*eer 7

Spring Boot 依赖管理插件不是必需的。
您可以使用内置 Gradle BOM 支持代替Spring Boot 依赖管理插件

例如:

    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'
    }
Run Code Online (Sandbox Code Playgroud)

对于多模块项目:在根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'
}
Run Code Online (Sandbox Code Playgroud)

并在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'
}
Run Code Online (Sandbox Code Playgroud)