【问题标题】:How do I define a variable for the dependency version in Gradle如何在 Gradle 中为依赖版本定义变量
【发布时间】:2016-11-01 13:45:45
【问题描述】:

我目前正在尝试将Maven 项目迁移到Gradle

在我的 Maven 版本中,我的 parent pom 中列出了所有依赖项版本:

<properties>
    <spring.version>4.2.3.RELEASE</spring.version>
    <spring.boot.version>1.3.3.RELEASE</spring.boot.version>
    ...
</properties>

然后我可以在我的任何子模块中定义这样的依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>

我试图在Gradle 上做同样的事情,因为我的一些模块共享这样的依赖版本,如果我想升级 Spring 或做类似的操作,我不想修改多个地方.

我最接近它的是:

dependencies {
    ext.springVersion = '4.2.3.RELEASE'
    compile "org.springframework:spring-jdbc:$springVersion"
}

但这仍然行不通。在 Gradle 中实现这一目标的推荐方法是什么?还是 Gradle 对此有不同的处理方式?也许我对 Maven 的想法仍然太多,无法看到其他解决方案。

请记住,对 Gradle 的尝试还不是我想要的完全。我希望能够在单独的文件中定义依赖关系,而不是直接在将使用它的文件上。

【问题讨论】:

  • 我见过带有“ext”的例子。在依赖项块之前分配。当您说“那仍然行不通”时,究竟会发生什么?
  • IDE指出错误说找不到变量并且没有导入库...但是话又说回来,也许这与IntelliJ IDEA有关,然后是gradle本身

标签: gradle dependency-management


【解决方案1】:

build.gradle 文件中的以下配置适用于 gradle version 4.5,将其发布在此处以供将来参考 -

ext {
    springVersion = '5.0.3.RELEASE'
}

dependencies {
    compile  "org.springframework:spring-context:$springVersion"
}

【讨论】:

  • 请注意, 中的单引号不起作用,应该是双引号。
  • implementation 替换compile 后,我可以确认它与gradle version 5.6gradle version 6.2 一起工作
【解决方案2】:

使用双引号对我有用。

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
        hazelCastVersion = '3.10.4'
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile "com.hazelcast:hazelcast-spring:${hazelCastVersion}"
    compile group: 'com.hazelcast', name: 'hazelcast', version: "${hazelCastVersion}" }

【讨论】:

  • 感谢您在此答案中包含“编译组”语法糖。
  • extbuildscript.ext有什么区别?
【解决方案3】:

您提供依赖版本的方式是正确的,应该可以工作。 IntelliJ Idea 在 gradle 构建脚本中的变量存在一些问题,所以您不必依赖它,只需尝试运行您的构建并检查,是否下载了依赖项以及您的项目是否正确构建。

如果您希望将项目依赖项存储在单独的文件中,那么最常见的方法是在根 build.script 中定义它们,通过 subprojects 关闭所有子项目或通过 allprojects 如果您希望指定您所有项目的依赖项都包括根。这将如下所示:

subprojects{
    dependencies{
        compile ...
    }
}

通过这种方式,您可以在根构建脚本中为子项目提供任何通用配置。阅读它the user guide

或者您可以在根构建脚本中声明一个变量,将其初始化为具有您需要的所有依赖项规范的映射或具有单个依赖项列表的数组,并在您的子项目中使用此变量,如果您需要它们。像这样的东西在根:

ext.commomLibs = [
  'testlibs' : 'junit:junit:4.8'
]

注意,您可以将映射的值定义为 lib 规范的数组,以使其指向所有这些依赖项。然后你可以在你的子项目中使用它:

dependencies{
    testCompile commomLibs.testlibs
}

另一种选择是使用带有依赖项的单独构建脚本并将其应用于您需要的构建脚本,就像这样

apply from: 'dependencies.script'

然后您可以将您的依赖项放入依赖项脚本中,并将其应用到任何其他构建脚本中,您需要此依赖项和/或可能是一些常见的逻辑。

【讨论】:

  • 你能给出使用apply from:的替代方法的说明吗?
  • 你能给 Gradle 7.1.1 的等价物吗?由于该线程中的任何答案似乎都不适用于该版本的 gradle build。
【解决方案4】:

您现在可以使用更接近 App Gradle Build 中的依赖项,如下所示:

dependencies {
    def daggerVersion = "2.24"
    implementation "com.google.dagger:dagger:$daggerVersion"
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
}

【讨论】:

    【解决方案5】:

    补充 Vikas Sachdeva 的回答:

    在我的子项目中以这种方式对我不起作用。 Gradle 抛出错误“找不到 org.springframework:spring-context:$springVersion”。最后,我通过添加加号解决了我的问题。

    例如:

    根 gradle.properties:

    springVersion = '5.0.3.RELEASE'
    

    root build.gradle

    dependencies {
        compile  "org.springframework:spring-context:${springVersion}"
    }
    

    子项目 build.gradle

    dependencies {
        compile  "org.springframework:spring-context:" + springVersion
    }
    

    注意:我的 gradle 版本是 5.4.1

    或者你可以使用插件管理你的依赖版本。

    io.spring.dependency-management

    参考:https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/

    【讨论】:

    • 可能在您的子项目中,您有单引号,而不是双引号。 Groovy/Gradle 在“${foo}”和“${foo}”之间有所不同。只有第一个会替代。另请注意 gradle.properties 不适合包含依赖版本。正常的方法是在你的 build.gradle 文件中使用额外的属性,比如“ext { ... }”。
    【解决方案6】:

    gradle v6.1.1上,最简单的是:

    dependencies {
        def jerseyVersion = '2.29.1'
        compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: jerseyVersion
    
        // ...
    }
    

    【讨论】:

      【解决方案7】:

      我有一个替代语法糖。我想这是编写依赖项的“漫长”方式。

      掌握“gradle.build”

      plugins {
          id 'java'
      }
      
      
      ext {
          slf4jApiVersion = '1.7.28'
          junitVersion = '4.12'
      }
      

      子模块/项目 gradle.build

      dependencies {
          testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
          compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jApiVersion}"
      }
      

      注意组、名称和版本的分隔... SINQLE 引号围绕组和名称值,双引号围绕版本值变量。

      【讨论】:

        【解决方案8】:

        另一种方法: 在 build.gradle app 的同一目录中创建一个文件。像这样命名这个新文件:dependencies.gradle 在你的 build.gradle app 在顶部添加这一行

        apply from: 'dependencies.gradle'
        

        像这样在 dependencies.gradle 中定义你的变量:

        ext {
        
        versions = [
            camerax_version : "1.1.0-alpha10",
            camerax_extra_version : "1.0.0-alpha24",
            constraintlayout : "2.0.3",
            appcompat : "1.2.0"
        ]
        
        camerax = [
                camerax_core : "androidx.camera:camera-core:${versions.camerax_version}",
                camerax_camera2 : "androidx.camera:camera-camera2:${versions.camerax_version}",
                camerax_lifecycle : "androidx.camera:camera-lifecycle:${versions.camerax_version}",
                camerax_extensions : "androidx.camera:camera-extensions:${versions.camerax_extra_version}",
                camerax_view : "androidx.camera:camera-view:${versions.camerax_extra_version}"
        ]
        
        androidx = [
                constraints_layout : "androidx.constraintlayout:constraintlayout:${versions.constraintlayout}",
                appcompat : 'androidx.appcompat:appcompat:${versions.appcompat}'
        ]}
        

        最后像这样在你的 build.gradle module 中使用这些变量

        implementation camerax.camerax_core
        implementation camerax.camerax_camera2
        implementation camerax.camerax_lifecycle
        implementation camerax.camerax_extensions
        implementation camerax.camerax_view
        

        【讨论】:

          猜你喜欢
          • 2017-08-21
          • 1970-01-01
          • 2019-03-30
          • 2017-07-05
          • 2018-06-02
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多