在gradle中,如何将变量用于插件版本?

cro*_*cha 35 gradle netflix-nebula-plugins

我的一个构建脚本导入了这个星云插件:

plugins {
  id 'nebula.ospackage' version '3.5.0'
}
Run Code Online (Sandbox Code Playgroud)

我一直在将所有版本信息移动到一个单独的文件中,所有项目都可以访问,并且想知道转换为以下内容的正确语法是什么:

plugins {
  id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用"gradle clean build"运行上面的操作时,出现以下错误:

构建文件'build.gradle':2:参数列表必须正好是1个非空字符串

有关 插件{}块的信息,请参阅 https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block

@第2行,第33列.id'nebula.ospackage'版本"$ versions.nebula_gradle_ospackage_plugin"

链接的文章展示了我如何使用"buildscript"块,它可以工作,但似乎必须有一种方法可以使这个工作在一行?

Boy*_*ter 36

你不能在这里使用变量:

«插件版本»和«插件ID»必须是常量,文字,字符串.不允许其他陈述; 它们的存在会导致编译错误.

  • 这是一个开放的错误:[允许插件DSL扩展属性作为版本#1697](https://github.com/gradle/gradle/issues/1697) (4认同)
  • 在以上问题中发布了可接受的解决方法:https://github.com/gradle/gradle/issues/1697#issuecomment-386824663 (2认同)

zho*_*uji 26

As of Gradle 5.6, you can declare your plugin versions in the gradle.properties file, and reference these properties in plugins block.

For example, the gradle.properties file?

springBootVersion=2.2.0.RELEASE
Run Code Online (Sandbox Code Playgroud)

the plugins block in build.gradle:

plugins {
    id "org.springframework.boot" version "${springBootVersion}"
}
Run Code Online (Sandbox Code Playgroud)

See: https://github.com/gradle/gradle/issues/1697#issuecomment-506910915.

See also:

  1. https://docs.gradle.org/5.6/release-notes.html#central-management-of-plugin-versions-with-settings-script
  2. https://docs.gradle.org/5.6/userguide/plugins.html#sec:plugin_management

  • 注意:这适用于 groovy build.gradle,但不适用于 kotlin DSL、build.gradle.kts,对 kotlin DSL 的支持尚未到来,https://github.com/gradle/gradle/issues/9830 (5认同)

Dmi*_*kiy 12

除了@zhouji comment之外,您不仅可以指定 中的版本gradle.properties,还可以通过编程方式定义它,如下所示:

buildscript {
    ext {
        kotlinVersion = "${JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32'}"
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
}
Run Code Online (Sandbox Code Playgroud)


aru*_*ruh 9

等级 7

Gradle 7 引入版本目录,用于声明依赖项并在项目/模块之间共享它们。

插件可以在目录中声明:

// settings.gradle

dependencyResolutionManagement {
    versionCatalogs {
        libs {
            version('android-plugins', '7.3.0')
            plugin('android-application', 'com.android.application').versionRef('android-plugins')

            // A library declaration using the same version.
            library('some-library', 'com.group', 'artifact').versionRef('android-plugins')
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该声明可以在块中引用plugins

// build.gradle

plugins {
    alias(libs.plugins.android.application) apply false
}
Run Code Online (Sandbox Code Playgroud)

该版本也可以在块中引用plugins

// build.gradle

plugins {
    id 'com.android.application' version libs.versions.android.plugins apply false
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅版本目录文档。

注意:在声明中,-用作依赖项名称中的分隔符。在消费端.则使用-groovy 语言的减号运算符,并且不能在属性名称中使用。因此,声明方和消费方的分隔符是不同的 - 这一开始让我感到困惑。


seu*_*abi 6

来自 Gradle 官方文档

插件版本管理

plugins {}内部的块允许pluginManagement {}在单个位置定义构建的所有插件版本。然后可以通过块通过id将插件应用到任何构建脚本plugins {}

以这种方式设置插件版本的好处之一是,它 pluginManagement.plugins {}不具有 与构建脚本块相同的约束语法plugins {}。这允许从 获取插件版本gradle.properties,或通过其他机制加载插件版本。


例子

// gradle.properties
springBootVersion=2.6.7
Run Code Online (Sandbox Code Playgroud)
// gradle.properties
springBootVersion=2.6.7
Run Code Online (Sandbox Code Playgroud)
// settings.gradle.kts
pluginManagement {
    val springBootVersion: String by settings

    plugins {
        id("org.springframework.boot") version springBootVersion
    }
}
Run Code Online (Sandbox Code Playgroud)


bar*_*uin 5

这是旧文章,但该错误仍未解决,我发现此文章正在寻找解决方法。

看来您已经意识到了这一点,并且实际上有了BoygeniusDexter的答案,但是我认为这可能会帮助其他人像我一样找到此帖子。以下变通办法基于Gradle文档,并为我解决了该问题:

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java'
    // and other plugins
    id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
// but the one with the variable version is applied the old way:
apply plugin: 'org.springframework.boot'

// We can use the variable in dependencies, too:
dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
    // ...
}
Run Code Online (Sandbox Code Playgroud)