Kotlin-dsl-samples: Property inaccessible in buildscript {} and plugins {} inner scopes

Created on 13 Oct 2016  路  2Comments  路  Source: gradle/kotlin-dsl-samples

Here's my failing build.gradle.kts:

val kotlinVersion by project("kotlinVersion") //Happens without parameters also

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //Property is not visible here
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Here's the gradle.properties:

kotlinVersion=1.0.4

And here is the whole output.

All 2 comments

The buildscript block and the remaining of the script are compiled and executed separately so variables and functions declared outside buildscript block are inaccessible by design.

Number of downvotes suggests that even if "by design" was a reasonable decision at the moment of implementing, it is definitely not what developers are expecting. Any chance you can reconsider something as basic as "I don't want to hardcode magical numbers"? If you do it in the code, any static analysis tool would raise an error for using magical number. Why should gradle scripts be special? I find gradle complicated where it shouldn't and inflexible where it should.

Edit: nevermind. I just saw that there is more active discussion of this problem in this ticket https://github.com/gradle/gradle/issues/1697 and after 2 years of going back and forth a half-measure was added.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xmlking picture xmlking  路  3Comments

AlexCzar picture AlexCzar  路  3Comments

iNikem picture iNikem  路  3Comments

jaredsburrows picture jaredsburrows  路  3Comments

jaredsburrows picture jaredsburrows  路  3Comments