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

时间:2018-08-23 15:07:36

标签: spring gradle intellij-idea dependencies dependency-management

我在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"
            }
     }
}

如果我添加

dependency 'org.apache.hadoop:hadoop-tools:2.6.0-mr1-cdh5.14.4' 

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'
    }
}

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

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'
    }
}

为什么会这样?

1 个答案:

答案 0 :(得分:2)

要了解此行为,您需要了解Spring DependencyManagement插件的工作方式(请参见官方文档中的this section):

  • dependencyManagement {} 块用于配置将应用于依赖项(使用的版本,传递性依赖项的排除等)的约束,但是该块不会< / strong>将这些依赖项自动应用于您的项目,
  • 必须使用 dependencies {}
  • 配置项目的依赖项

在您的示例中:

  • 首先,您在根项目中配置了 dependencyManagement 块,并在“ hadoop-common”和“ hadoop-hdfs”模块上设置了约束,然后在“ hadoop-tools”上添加了约束(在 dependencyManagement 块中使用“ dependency”或“ dependencySet”):在此阶段,您尚未将任何依赖项显式添加到项目中,而仅配置了依赖项约束

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

  • 然后使用 dependencies 块在“ hadoop-tools”上添加了“ compile”依赖项 ,这是声明依赖关系的正确方法,这使“ 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/'
        }
    }
}

子项目脚本

dependencies{

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

}