ext in buildscript can not be recognised by Gradle Kotlin DSL

前端 未结 5 1790
走了就别回头了
走了就别回头了 2021-01-31 07:39

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

相关标签:
5条回答
  • 2021-01-31 08:18

    It is possible to use constants defined in .kt file in .gradle.kts files.

    create buildSrc folder in root folder of your project

    create buildSrc/build.gradle.kts file with the following content

    plugins {
        `kotlin-dsl`
    }
    
    repositories {
        mavenCentral()
    }
    

    create file buildSrc/src/main/kotlin/Constants.kt with the following content

    object Constants {
        const val kotlinVersion = "1.3.70"
        const val targetSdkVersion = 28
    }
    

    Synchronize. Now you may reference created constants in various .gradle.kts files like this

    ...
    classpath(kotlin("gradle-plugin", version = Constants.kotlinVersion))
    ...
    
    ...
    targetSdkVersion(Constants.targetSdkVersion)
    ...
    
    0 讨论(0)
  • 2021-01-31 08:23

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-31 08:27

    There is a new possibility with Kotlin we can use:

    object DependencyVersions {
        const val JETTY_VERSION = "9.4.12.v20180830"
    }
    
    dependencies{
        implementation("org.eclipse.jetty:jettyserver:${DependencyVersions.JETTY_VERSION}")
    }
    

    Here, DependencyVersions is a name I chose. You can choose another name, like "MyProjectVariables". This is a way to avoid using the extra or ext properties.

    0 讨论(0)
  • 2021-01-31 08:37

    Global properties in kotlin-gradle-dsl:
    https://stackoverflow.com/a/53594357/3557894


    Kotlin version is embedded into kotlin-gradle-dsl.
    You can use dependecies with embedded version as follows:

    implementation(embeddedKotlin("stdlib-jdk7"))
    
    classpath(embeddedKotlin("gradle-plugin"))
    
    0 讨论(0)
  • 2021-01-31 08:41

    What is working for me is using ext in allprojects instead of buildscript, so in your top-level build.gradle.kts

    allprojects {
      ext {
        set("supportLibraryVersion", "26.0.1")
      }
    }
    

    then you can use it in build.gradle.kts files in modules like this:

    val supportLibraryVersion = ext.get("supportLibraryVersion") as String
    
    0 讨论(0)
提交回复
热议问题