Gradle Kotlin DSL 无法识别 buildscript 中的 ext [英] ext in buildscript can not be recognised by Gradle Kotlin DSL

查看:55
本文介绍了Gradle Kotlin DSL 无法识别 buildscript 中的 ext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几天在尝试写一些代码来体验一下Spring 5中的Spring反应式特性和kotlin扩展,还准备了一个gradle Kotlin DSL build.gradle.kt来配置gradle build.

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.

build.gradle.kthttp://生成的Spring Boot模板代码转换而来start.spring.io.

但是Gradle检测不到buildscript中的ext.

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

buildscript {
  ext { }
}

ext 会导致 Gradle 构建错误.

The ext will cause Gradle build error.

classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion") 工作,我以困难的方式添加了变量.

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"

但我必须在全局顶级位置声明它们并在buildscript中复制它们.

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

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

是否有一种优雅的方法可以让 ext 工作?

Is there a graceful approach to make ext work?

更新:有一些丑陋的方法:

  1. 来自 Gradle Kotlin DSL 示例,https://github.com/gradle/kotlin-dsl/tree/master/samples/project-properties,在gradel.properties中声明属性.

  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

并在 build.gradle.kts 中使用它.

And use it in build.gradle.kts.

buildScript{
   val kotlinVersion by project

}
 val kotlinVersion by project //another declare out of buildscript block.

  • 与上面类似,在 buildScript 块中声明它们:

  • 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.
    

  • 如何避免重复val kotlinVersion: String by extra?

    推荐答案

    With Kotlin DSL ext 已经改为 extra 可以在 buildscript 下使用了.

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

    例如:-

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

    这篇关于Gradle Kotlin DSL 无法识别 buildscript 中的 ext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆