In gradle, how to use a variable for a plugin version?

You cannot use variable here:

Where «plugin version» and «plugin id» must be constant, literal, strings. No other statements are allowed; their presence will cause a compilation error.


This is an old post, but the bug is still open, and I found this post looking for workarounds.

It seems you were aware of this already, and actually have BoygeniusDexter's answer, but I think this may help others finding this post like I did. The following workaround is based on the Gradle docs and solved the problem for me:

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    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
    // ...
}

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

the plugins block in build.gradle:

plugins {
    id "org.springframework.boot" version "${springBootVersion}"
}

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