Search code examples
kotlingradle-plugingradle-kotlin-dsl

Externalize plugin versions in kotlin gradle dsl?


I would like to externalize plugin versions.

I've tried

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

object DependencyVersions {
    const val SPRING_BOOT_VERSION = "2.1.6.RELEASE"
    const val DEPENDENCY_MANAGEMENT_VERSION = "1.0.8.RELEASE"
}

plugins {
    id("org.springframework.boot") version DependencyVersions.SPRING_BOOT_VERSION
    id("io.spring.dependency-management") version DependencyVersions.DEPENDENCY_MANAGEMENT_VERSION
}

but I get Unresolved reference: DependencyVersions


Solution

  • This was until very recently still true with the Gradle API but was recently fixed. See this issue covering version variables in the Plugin section. There is an open issue for fixing this for the Kotlin DSL.

    The only way to get around this is setting the version with a PluginResolutionStrategy as suggested in the same issue in your settings.gradle:

    pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.version == null) {
                def pluginName = requested.id.name.split('-').collect { it.capitalize() }.join().uncapitalize()
                def versionPropertyName = (requested.id.id == 'org.jetbrains.kotlin.jvm') ?
                        "kotlinPluginVersion" : "${pluginName}PluginVersion"
                logger.info("Checking for plugin version property '$versionPropertyName'.")
                if (gradle.rootProject.hasProperty(versionPropertyName)) {
                    def version = gradle.rootProject.properties[versionPropertyName]
                    logger.info("Setting '${requested.id.id}' plugin version to $version.")
                    useVersion version
                } else {
                    logger.warn("No version specified for plugin '${requested.id.id}' and property " +
                            "'$versionPropertyName' does not exist.")
                }
            }
        }
    }}
    

    And adding a property with the name of the plugin suffixed by "Version" to gradle.properties, e.g. "jacksonVersion".