Michal Kordas
Michal Kordas

Reputation: 10925

Cannot parametrize version in Gradle plugins block

In Gradle when I declare plugin with hardcoded version it works well:

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
}

However, if I try to parametrize it then I get exception.

Contents of gradle.properties:

springBootVersion=2.1.4.RELEASE

Contents of build.gradle:

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

Why the following exception is happening?

Cause: startup failed:
build file 'build.gradle': 10: argument list must be exactly 1 literal non empty string

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

 @ line 10, column 5.
       id 'org.springframework.boot' version "$springBootVersion"
       ^

1 error

Upvotes: 1

Views: 1517

Answers (2)

Raul Lucaciu
Raul Lucaciu

Reputation: 162

You can parametrize the version by defying a variable in the project level build.gradle:

buildscript {
    ext.PLUGIN_VERSION = 'VERSION'
}

And then use it in the app level build.gradle:

plugins {
    id 'PLUGIN_NAME' version "$PLUGIN_VERSION"
}

Upvotes: 2

Louis Jacomet
Louis Jacomet

Reputation: 14500

What you are facing is a documented limitation of the plugins block. You cannot use parameterization in it.

What's possible is to delegate the configuration of the version of the plugin to the settings.gradle.

In gradle.properties:

springBootVersion=2.1.4.RELEASE

In settings.gradle:

// Must be the first statement of settings.gradle
pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "org.springframework.boot") {
                useModule("org.springframework.boot:org.springframework.boot.gradle.plugin:${springBootVersion}")
            }
        }
    }
}

In build.gradle:

plugins {
    id 'org.springframework.boot'
}

Upvotes: 3

Related Questions