spring 依赖管理 gradle 插件不下载依赖项

r.s*_*r.s 3 spring dependencies intellij-idea dependency-management gradle

我在 IntelliJ 上使用 spring 依赖管理 gradle 插件。我有一个根模块如下

apply plugin: "io.spring.dependency-management"

dependencyManagement {
    dependencies {
      dependencySet(group: "org.apache.hadoop", version: "2.6.0-cdh5.14.4") {
                entry "hadoop-common"
                entry "hadoop-hdfs"
            }
     }
}
Run Code Online (Sandbox Code Playgroud)

如果我添加

dependency 'org.apache.hadoop:hadoop-tools:2.6.0-mr1-cdh5.14.4' 
Run Code Online (Sandbox Code Playgroud)

或者

dependencySet(group: "org.apache.hadoop", version: "2.6.0-mr1-cdh5.14.4") {
    entry ("hadoop-tools") {
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }
}
Run Code Online (Sandbox Code Playgroud)

在根模块中,它不会下载hadoop-toolsjar。仅当我在子模块中添加以下内容时,它才会下载此依赖项。

plugins {
    id "com.github.johnrengelman.shadow" version "2.0.4"
}

dependencies {
    compile ("org.apache.hadoop:hadoop-tools:2.6.0-mr1-cdh5.14.4") {
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }
}
Run Code Online (Sandbox Code Playgroud)

行为为何?

M.R*_*uti 5

为了理解这种行为,您需要了解 Spring DependencyManagement 插件的工作原理(请参阅 官方文档中的本节):

  • dependencyManagement { }块用于配置适用于依赖项的约束(要使用的版本、传递依赖项的排除等),但此块不会将这些依赖项本身应用到您的项目,
  • 项目的依赖项必须使用依赖项{}块进行配置

在你的例子中:

  • 首先,您在根项目中配置了dependencyManagement块,并对“hadoop-common”和“hadoop-hdfs”模块进行了约束,然后添加了对“hadoop-tools”的约束(在 dependencyManagement 块中使用“dependency”或“dependencySet ) :在这个阶段,您还没有显式向项目添加任何依赖项,而只是配置了依赖项约束

    ==> 这解释了为什么“hadoop-tools”依赖项没有添加/下载到您的项目中。

  • 然后,您使用依赖项块添加了对“hadoop-tools”的“编译”依赖项,这是声明依赖项的正确方法,这使得“hadoop-tools”库在您的项目中可用。

如果我很好地理解您的要求,根据您在问题中提供的源代码:您可以按如下方式配置您的项目:

根项目的脚本

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

// configure plugins to be applied and dependencies contraints for all subprojects
subprojects {
    apply plugin: 'java'
    apply plugin: io.spring.gradle.dependencymanagement.DependencyManagementPlugin

    dependencyManagement {
        dependencies {
            // set version for hadoop-common & hadoop-hdfs to "2.6.0-cdh5.14.4"
            dependencySet(group: "org.apache.hadoop", version: "2.6.0-cdh5.14.4") {
                entry "hadoop-common"
                entry "hadoop-hdfs"
            }
            // set version "2.6.0-mr1-cdh5.14.4" for hadoop-tool, 
            //   and exclude slf4j-log4j12 module from transitive dependencies
            dependency (group: "org.apache.hadoop" , name: "hadoop-tools", version : "2.6.0-mr1-cdh5.14.4") {
                exclude 'org.slf4j:slf4j-log4j12'
            }
        }
    }
    repositories {
        jcenter()
        maven {
            url = 'https://repository.cloudera.com/content/repositories/releases/'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

子项目脚本

dependencies{

    // hadoop-tools module version is defined (constrained) 
    //   by dependencyManagement in root project build script
    compile 'org.apache.hadoop:hadoop-tools'

}
Run Code Online (Sandbox Code Playgroud)