Kotlin-dsl-samples: How to read a val in plugins {}

Created on 30 Mar 2018  路  6Comments  路  Source: gradle/kotlin-dsl-samples

val kotlinVersion = "1.2.31"

plugins {
    kotlin("jvm")           version $kotlinVersion apply false
    kotlin("plugin.spring") version $kotlinVersion apply false
}

gradle report "Unresolved reference: kotlinVersion"

question duplicate

Most helpful comment

@ice1000
I bet you want to reuse the kotlin variable because you need it for the kotlin dependency, right?
In my current project I have the following setup:
(It is in Groovy, but you can archive the same with Kotlin I think)

buildSrc/src/main/kotlin/some/package/Version.kt:

object Versions {

    object Plugin {
        val kotlin = "1.2.31"
    }

    object Dependency {
        val kotlin = = Plugin.kotlin
    }
}

settings.gradle:

import some.package.Versions

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id.startsWith("org.jetbrains.kotlin.")) {
                it.useVersion(Versions.Plugin.kotlin)
            }
        }
    }
}

If you have setup this you can simply omit the version in the plugins block:

plugins {
  ...
  kotlin("jvm")
}

In your dependency block you can simply say:

import some.package.Versions.Dependency 

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$Dependency.kotlin"
}

All 6 comments

The pluginsblock is special, you can't read variables inside of it.
You can use the pluginResolution block in the settings file to use variables from properties and things like that.

Closing as duplicate of #158

So if I want to reuse the variable kotlinVersion which is outside of the plugins block, how can I achieve that?

var kotlinVersion: String by extra
kotlinVersion = if (isCI) "1.2.40-eap-61" else "1.2.31"

plugins {
    idea
    java
    application
    id("org.jetbrains.intellij") version "0.3.1"
    id("de.undercouch.download") version "3.4.2"
    kotlin("jvm") version "1.2.31" // I want to reuse the variable here
}

@ice1000
I bet you want to reuse the kotlin variable because you need it for the kotlin dependency, right?
In my current project I have the following setup:
(It is in Groovy, but you can archive the same with Kotlin I think)

buildSrc/src/main/kotlin/some/package/Version.kt:

object Versions {

    object Plugin {
        val kotlin = "1.2.31"
    }

    object Dependency {
        val kotlin = = Plugin.kotlin
    }
}

settings.gradle:

import some.package.Versions

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id.startsWith("org.jetbrains.kotlin.")) {
                it.useVersion(Versions.Plugin.kotlin)
            }
        }
    }
}

If you have setup this you can simply omit the version in the plugins block:

plugins {
  ...
  kotlin("jvm")
}

In your dependency block you can simply say:

import some.package.Versions.Dependency 

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$Dependency.kotlin"
}

Wow cool! I love this, countless thanks! @StefMa

Was this page helpful?
0 / 5 - 0 ratings