Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

ext in buildscript can not be recognised by Gradle Kotlin DSL

In these days, I am trying to write some codes to experience the Spring reactive features and kotlin extension in Spring 5, and I also prepared a gradle Kotlin DSL build.gradle.kt to configure the gradle build.

The build.gradle.kt is converted from Spring Boot template codes generated by http://start.spring.io.

But the ext in the buildscript can not be detected by Gradle.

buildscript {
  ext { }
}

The ext will cause Gradle build error.

To make the variables in classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") and compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion") work, I added the variables in the hard way.

val kotlinVersion = "1.1.4"
val springBootVersion = "2.0.0.M3"

But I have to declare them in global top location and duplicate them in the buildscript.

Code: https://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/build.gradle.kts

Is there a graceful approach to make ext work?

Update: There are some ugly approaches:

  1. From Gradle Kotlin DSL example, https://github.com/gradle/kotlin-dsl/tree/master/samples/project-properties, declares the properties in gradel.properties.

    kotlinVersion = 1.1.4
    springBootVersion = 2.0.0.M3
    

    And use it in build.gradle.kts.

    buildScript{
       val kotlinVersion by project
    
    }
     val kotlinVersion by project //another declare out of buildscript block.
    
  2. Similar with above declare them in buildScript block:

    buildScript{
       extra["kotlinVersion"] = "1.1.4"
       extra["springBootVersion"] = "2.0.0.M3"
       val kotlinVersion: String by extra
    
    }
     val kotlinVersion: String by extra//another declare out of buildscript block.
    

How can I avoid the duplication of val kotlinVersion: String by extra?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With Kotlin DSL ext has been changed to extra and it can be used under buildscript.

Eg :-

buildscript {
    // Define versions in a single place
    extra.apply{
        set("minSdkVersion", 26)
        set("targetSdkVersion", 27)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...