spring 依赖管理 gradle 插件不下载依赖
spring dependency-management gradle plugin doesn't download dependency
我在 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'
}
}
为什么会这样?
为了理解此行为,您需要了解 Spring DependencyManagement 插件的工作原理(请参阅官方文档中的 this section):
- dependencyManagement { } 块用于配置将应用于依赖项的约束(要使用的版本、传递依赖项的排除等),但此块 不会将这些依赖项应用到您的项目中,
- 您的项目的依赖项必须使用 dependencies{ } 块配置
在你的例子中:
首先,您在根项目中配置了 dependencyManagement 块,并对“hadoop-common”和“hadoop-hdfs”模块进行了约束,然后添加了约束关于“hadoop-tools”(在 dependencyManagement 块中使用“dependency”或“dependencySet”):在这个阶段,您还没有明确地向项目添加任何依赖项,而只是配置了依赖关系约束
==> 这解释了为什么“hadoop-tools”依赖性不是 added/downloaded 您的项目。
然后您使用 dependencies 块添加了对“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/'
}
}
}
子项目脚本
dependencies{
// hadoop-tools module version is defined (constrained)
// by dependencyManagement in root project build script
compile 'org.apache.hadoop:hadoop-tools'
}
我在 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'
}
}
为什么会这样?
为了理解此行为,您需要了解 Spring DependencyManagement 插件的工作原理(请参阅官方文档中的 this section):
- dependencyManagement { } 块用于配置将应用于依赖项的约束(要使用的版本、传递依赖项的排除等),但此块 不会将这些依赖项应用到您的项目中,
- 您的项目的依赖项必须使用 dependencies{ } 块配置
在你的例子中:
首先,您在根项目中配置了 dependencyManagement 块,并对“hadoop-common”和“hadoop-hdfs”模块进行了约束,然后添加了约束关于“hadoop-tools”(在 dependencyManagement 块中使用“dependency”或“dependencySet”):在这个阶段,您还没有明确地向项目添加任何依赖项,而只是配置了依赖关系约束
==> 这解释了为什么“hadoop-tools”依赖性不是 added/downloaded 您的项目。
然后您使用 dependencies 块添加了对“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/'
}
}
}
子项目脚本
dependencies{
// hadoop-tools module version is defined (constrained)
// by dependencyManagement in root project build script
compile 'org.apache.hadoop:hadoop-tools'
}