在Gradle中,如何在单个位置声明公共依赖关系?

在Gradle中,如何在单个位置声明公共依赖关系?,gradle,dependency-management,Gradle,Dependency Management,在Maven中,有一个非常有用的特性,您可以在父POM的部分中定义依赖项,并从子模块引用该依赖项,而无需指定版本或范围或任何内容 Gradle中有哪些替代方案?您可以在父脚本中声明公共依赖项: ext.libraries=[//Groovy映射文字 spring_核心:“org.springframework:spring核心:3.1”, junit:“junit:junit:4.10” ] 然后,您可以从子脚本使用依赖项声明,如下所示: 依赖项{ 编译libraries.spring_cor

在Maven中,有一个非常有用的特性,您可以在父POM的
部分中定义依赖项,并从子模块引用该依赖项,而无需指定版本或范围或任何内容


Gradle中有哪些替代方案?

您可以在父脚本中声明公共依赖项:

ext.libraries=[//Groovy映射文字
spring_核心:“org.springframework:spring核心:3.1”,
junit:“junit:junit:4.10”
]
然后,您可以从子脚本使用依赖项声明,如下所示:

依赖项{
编译libraries.spring_core
testcompilelibraries.junit
}
要与高级配置选项共享依赖项声明,可以使用
DependencyHandler.create

库=[
spring\u core:dependencies.create(“org.springframework:springcore:3.1”){
排除模块:“公用记录”
力=真
}
]
可以使用相同的名称共享多个依赖项:

库=[
spring:[//Groovy列表文字
“org.springframework:spring核心:3.1”,
“org.springframework:springjdbc:3.1”
]
]
依赖项{compile libraries.spring}
将同时添加这两个依赖项


您不能以这种方式共享的一条信息是应该为依赖项分配什么配置(Maven术语中的范围)。然而,根据我的经验,最好还是明确地说明这一点。

这是一个迟来的答复,但您可能还想看看: 它提供了导入maven“bom”并重用“bom”中定义的定义的可能性。
当逐渐从maven迁移到gradle时,这无疑是一个很好的帮助!现在就享受它。

io.spring.gradle:依赖关系管理插件插件在新的gradle 3.x系列中存在问题,但在2.x系列中稳定。有关参考,请参阅错误报告

在弹簧()的情况下,您可以以以下内容结束:

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
    }
}

repositories {
    mavenLocal()
    jcenter()
}

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:Athens-SR3'
    }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'

    testCompile 'org.springframework.boot:spring-boot-starter-test'
}
请注意,
io.spring.platform:platform bom
org.springframework.boot:spring boot starter父项作为父项,因此它与spring boot兼容

您可以通过以下方式验证实际依赖项解决方案:

$ gradle dependencies
$ gradle dependencies --configuration compile
$ gradle dependencies -p $SUBPROJ

$ gradle buildEnvironment
$ gradle buildEnvironment -p $SUBPROJ
或使用任务:

task showMeCache {
    configurations.compile.each { println it }
}

阅读Soring官方博客文章,了解引入
io.spring.gradle:dependency management plugin

的原因。您可以使用以下代码集中化依赖项:

gradle.properties

COMPILE_SDK_VERSION=26
BUILD_TOOLS_VERSION=26.0.1
TARGET_SDK_VERSION=26
MIN_SDK_VERSION=14

ANDROID_SUPPORT_VERSION=26.0.2
在每个模块中,添加到build.gradle

android {
    compileSdkVersion COMPILE_SDK_VERSION as int
    buildToolsVersion BUILD_TOOLS_VERSION as String

    defaultConfig {
        minSdkVersion MIN_SDK_VERSION as int
        targetSdkVersion TARGET_SDK_VERSION as int
        versionCode 1
        versionName "1.0"

    }

}

dependencies {
 compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_VERSION}"
 compile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}"
 compile "com.android.support:support-annotations:${ANDROID_SUPPORT_VERSION}"
 compile "com.android.support:support-vector-drawable:${ANDROID_SUPPORT_VERSION}"
 compile "com.android.support:design:${ANDROID_SUPPORT_VERSION}"
}

这篇博文建议将依赖项和组作为配置进行管理:

我自己没有试过,但它看起来很有趣

根项目build.gradle

subprojects {
  configurations {
    commonsIo
  }

  dependencies {
    commonsIo 'commons-io:commons-io:2.5'
  }
}
configurations {
  compile.extendsFrom commonsIo
}
子项目build.gradle

subprojects {
  configurations {
    commonsIo
  }

  dependencies {
    commonsIo 'commons-io:commons-io:2.5'
  }
}
configurations {
  compile.extendsFrom commonsIo
}

从Gradle 4.6开始,文档中建议使用依赖约束作为实现此目的的方法。发件人:

对于大型项目,推荐的做法是声明没有版本的依赖项,并使用依赖项约束进行版本声明。其优点是依赖项约束允许您在一个位置管理所有依赖项的版本,包括可传递的依赖项

在父级
build.gradle
文件中:

allprojects {
  plugins.withType(JavaPlugin).whenPluginAdded {
    dependencies {
      constraints {
        implementation("com.google.guava:guava:27.0.1-jre")
      }
    }
  }
}
用检查Java插件(…
whenpluginaded{
)包装依赖项块并不是绝对必要的,但它将处理向同一版本添加非Java项目的问题

然后在child gradle项目中,您只需省略verison:

apply plugin: "java"

dependencies {
  implementation("com.google.guava:guava")
}

子版本仍然可以选择指定更高的版本。如果指定了更低的版本,它将自动升级到约束中的版本。

为了保持渐变文件干净,我们可以在数组中对依赖项进行分组,并在以后实现它们

  • 在依赖项块之外的build.gradle(应用程序级)中添加类似于此的库版本
  • //声明库的版本

    final RetrofitVersion = '2.3.0'
    final OkHttpVersion = '3.9.1'
    
  • 创建相关依赖项的数组,以便以后可以轻松找到。将其添加到依赖项块外部的build.gradle(应用程序级)
  • //在库中使用版本,并在访问时添加依赖项 名称(如改装(第一个))

  • 并且在依赖项块中:
  • //实现数组中的所有依赖项

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation networkDependencies.values()
    }
    

    因此,最终代码将如下所示:

    final RetrofitVersion = '2.3.0'
    final OkHttpVersion = '3.9.1'
    
    final networkDependencies = [
            retrofit             : "com.squareup.retrofit2:retrofit:${RetrofitVersion}",
            retrofitGsonConverter: "com.squareup.retrofit2:converter-gson:${RetrofitVersion}",
            retrofitRxJavaAdapter: "com.squareup.retrofit2:adapter-rxjava2:${RetrofitVersion}",
            okHttp3              : "com.squareup.okhttp3:okhttp:${OkHttpVersion}",
            okHttp3Logging       : "com.squareup.okhttp3:logging-interceptor:${OkHttpVersion}"
    ]
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation networkDependencies.values()
    }
    

    谢谢,这解决了我的问题,但仍然有一个问题。.在Maven中,我们可以将版本保留为空,如果这是一个库,这很方便,因为你可以在我们的应用程序中使用它,并进行dependencyManagement来定义它应该采用的库的版本。你会如何处理Gradle?我不理解这个问题。请提供一个ePeter,ctapobep所说的是,在maven中,您可以在dependencyManagement部分的父(或聚合器)pom中声明版本(和范围)的依赖项。然后在“具体”pom中,您不需要重新声明版本;只需要工件和groupId。基本上,它告诉maven“我需要X:Y,但使用父级配置的任何版本。“为了避免这种重复,我倾向于创建一个单独的
    dependencies.gradle
    脚本,在其中我将所有依赖项定义为属性,例如:
    ext.GROOVY='org.codehaus.GROOVY:GROOVY all:2.1.6'
    。在根项目
    build.gradle
    中,我包括
    所有项目{apply from:$rootDir/dependencies.gradle}
    。然后所有依赖项都定义在一个文件中,而不是分散在各个文件中,并且更“易于阅读”在依赖项配置中使用常量。这正是我在上面所做的。您不需要应用于
    所有项目
    ,因为子项目可以看到项目级的额外属性。当您希望在多个(多个)项目中共享相同的依赖项时,它甚至是必须的虽然很方便,但这个插件可能有重要的作用