Search code examples
gradlenetflix-nebula-plugins

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


One of my build scripts imports that nebula plugin:

plugins {
  id 'nebula.ospackage' version '3.5.0'
}

I've been moving all of my version info into a separate file that all projects have access to and am wondering what is the correct syntax to convert to something like:

plugins {
  id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}

When I try running the above with "gradle clean build", I get the following error:

build file 'build.gradle': 2: argument list must be exactly 1 literal non empty string

See https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

@ line 2, column 33. id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"

The linked article shows how I could use the "buildscript" block, which works, but it seems like there must be a way to make this work in a single line?


Solution

  • 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