在Gradle中,如何在一个地方声明公共依赖项?

Sta*_*sev 102 dependency-management gradle

在Maven中有一个非常有用的功能,您可以<dependencyManagement>在父POM 的部分中定义依赖项,并在不指定版本或范围或其他任何内容的情况下从子模块引用该依赖项.

Gradle有哪些替代方案?

Pet*_*ser 169

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

ext.libraries = [ // Groovy map literal
    spring_core: "org.springframework:spring-core:3.1",
    junit: "junit:junit:4.10"
]
Run Code Online (Sandbox Code Playgroud)

从子脚本中,您可以使用依赖性声明,如下所示:

dependencies {
    compile libraries.spring_core
    testCompile libraries.junit
}
Run Code Online (Sandbox Code Playgroud)

要使用高级配置选项共享依赖性声明,您可以使用DependencyHandler.create:

libraries = [
    spring_core: dependencies.create("org.springframework:spring-core:3.1") {
        exclude module: "commons-logging"
        force = true
    }
]
Run Code Online (Sandbox Code Playgroud)

可以使用相同的名称共享多个依赖项:

libraries = [
    spring: [ // Groovy list literal
        "org.springframework:spring-core:3.1", 
        "org.springframework:spring-jdbc:3.1"
    ]
]
Run Code Online (Sandbox Code Playgroud)

dependencies { compile libraries.spring } 然后将立即添加两个依赖项.

您无法以这种方式共享的一条信息是应该为其分配依赖项的配置(Maven术语中的范围).但是,根据我的经验,最好还是要明确这一点.

  • Peter,ctapobep所说的是,在maven中,您可以在dependencyManagement部分的父(或聚合器)pom中声明与版本(和范围)的依赖关系.然后在"具体"pom中,你不需要重新声明版本; 只是artifact和groupId.基本上它告诉maven"我需要X:Y,但使用父配置的任何版本." (4认同)
  • 谢谢,这解决了我的问题,但仍然有一个问题..在Maven我们可以让版本为空,如果这是一个lib,它很方便,因为你可以在我们的应用程序中使用它并使dependencyManagement定义lib的版本它应该采取.你会如何对Gradle做同样的事情? (3认同)
  • 为了避免这种重复,我倾向于创建一个单独的`dependencies.gradle`脚本,我将所有依赖项定义为属性,例如:`ext.GROOVY ='org.codehaus.groovy:groovy-all:2.1.6' `.在根项目`build.gradle`中,我包含`allprojects {apply from:"$ rootDir/dependencies.gradle"}`.然后,所有依赖项都在一个文件中定义,而不是将它们分散开来,并且在依赖项配置中使用了更多"易于阅读"的常量. (2认同)

roo*_*msg 7

这是一个迟到的回复,但您可能还想查看:http://plugins.gradle.org/plugin/io.spring.dependency-management 它提供了导入maven'bom'的可能性,并重用了定义在'bom'中定义.当逐渐从maven迁移到gradle时,这肯定是一个很好的帮助!现在享受吧.

  • 虽然方便,但这个插件可能具有显着的性能足迹.对于具有200多个依赖关系的30个子项目,它将依赖解析阶段加1分钟.不过,对于小型项目来说,它就像一个魅力 (7认同)

Mah*_*zad 7

正如datta在他们的回答中所说,Gradle 现在有一个叫做版本目录的东西。\n这里是Kotlin DSL
(*.kts 文件)的示例。请注意,我使用的是 Gradle 7.4。

\n

在settings.gradle.kts中定义依赖项:

\n
// Configure dependencies aspects applied to all projects\ndependencyResolutionManagement {\n    // By default, repositories declared by a project will override the ones here.\n    // You can change this behavior with the repositoriesMode property.\n    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n\n    // Define repositories for all projects\n    repositories {\n        mavenCentral()\n        maven("https://jitpack.io")\n    }\n\n    versionCatalogs {\n        create("libs") {\n            // Versions are useful specially when you have libraries with the same\n            // group and version which are updated together with the same version\n            version("room", "2.4.1")\n            //       \xe2\x94\x82       \xe2\x94\x82\n            //       \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> The version notation\n            //       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Your desired name (alias)\n    \n            library("material", "com.google.android.material:material:1.4.0")\n            //       \xe2\x94\x82           \xe2\x94\x82\n            //       \xe2\x94\x82           \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> The dependency notation (coordinates)\n            //       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Your desired name (alias); only letters, digits and _ - .\n            //       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Note that _ - . will all be normalized to .\n    \n            // You can configure the version as you would in a regular build file\n            // Note that the group and module are separate parameters\n            library("junit5", "org.junit.jupiter", "junit-jupiter").version {\n                prefer("5.8.0")\n            }\n    \n            // Using the same version for multiple dependencies\n            library("room-ktx", "androidx.room", "room-ktx").versionRef("room")\n            library("room-runtime", "androidx.room", "room-runtime").versionRef("room")\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

build.gradle[.kts]中的用法:

\n
dependencies {\n    implementation(libs.material)\n    implementation(libs.room.ktx)\n    implementation(libs.room.runtime)\n    testImplementation(libs.junit5)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

正如您所看到的,您不仅可以声明依赖项,还可以在此处声明存储库(而不是allprojects在顶级构建脚本中使用 block 来为所有子项目定义存储库)。

\n

有关上述解决方案的常规语法以及有关版本目录和集中存储库和依赖项配置的更多信息,请参阅Gradle 官方指南

\n


Adr*_*ker 6

从Gradle 4.6开始,文档中建议使用依赖项约束来实现此目的。来自https://docs.gradle.org/current/userguide/declaring_dependencies.html#declaring_a_dependency_without_version

对于较大的项目,建议的做法是声明没有版本的依赖项,并将依赖项约束用于版本声明。优点是,依赖关系约束使您可以在一处管理所有依赖关系的版本,包括可传递的依赖关系。

在您的父build.gradle文件中:

allprojects {
  plugins.withType(JavaPlugin).whenPluginAdded {
    dependencies {
      constraints {
        implementation("com.google.guava:guava:27.0.1-jre")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

whenPluginAdded {不必严格检查Java插件(... )来包装依赖项块,但是它将处理将非Java项目添加到同一内部版本。

然后,在儿童gradle项目中,您可以简单地省略版本:

apply plugin: "java"

dependencies {
  implementation("com.google.guava:guava")
}
Run Code Online (Sandbox Code Playgroud)

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