如何在build.gradle.kts中将ext。*变量插入到plugins块中

时间:2017-09-05 11:11:00

标签: gradle kotlin gradle-kotlin-dsl

我的构建文件如下所示:

val nexusBaseUri: String by extra
val gradle_version: String by extra
val kotlin_version: String by extra

buildscript {
    val nexusBaseUri by extra { "https://mynexusserver/nexus" }
    val gradle_version by extra { "4.1" }
    val kotlin_version by extra { "1.1.4-3" }
    val springBoot_version by extra { "2.0.0.M3" }

    repositories {
        maven { url = uri("$nexusBaseUri/repository/public") }
        jcenter()
        maven { url = uri("http://repo.spring.io/snapshot") }
        maven { url = uri("http://repo.spring.io/milestone") }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
    }
}

plugins {
    application
    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    // the following line causes a problem
    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    kotlin("jvm", kotlin_version)
}

apply {
    plugin("kotlin-spring")
    plugin("org.springframework.boot")
    plugin("io.spring.dependency-management")
}

application {
    mainClassName = "eqip.fid.FdmlInterpreterDeveloperAppKt"
}

repositories {
    maven { url = uri("$nexusBaseUri/content/groups/public") }
    jcenter()
    maven { url = uri("http://repo.spring.io/snapshot") }
    maven { url = uri("http://repo.spring.io/milestone") }
}

dependencies {
    compile(kotlin("stdlib"))
    compile("org.springframework.boot:spring-boot-starter-web")
}

tasks {
    "wrapper"(Wrapper::class) {
        gradleVersion = gradle_version
    }
}

我在IntelliJ IDEA中遇到的错误是

  

' val kotlin_version:String'在这种情况下,隐式接收器不能调用它。如有必要,请使用明确的

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

您可以在plugins中定义版本,然后在博客之外访问此版本,例如在dependencies部分。

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

//This is necessary to make the version accessible in other places
val kotlinVersion: String? by extra {
    buildscript.configurations["classpath"]
            .resolvedConfiguration.firstLevelModuleDependencies
            .find { it.moduleName == "kotlin-gradle-plugin" }?.moduleVersion
}

dependencies {
    compile(kotlin("stdlib", kotlinVersion))
}

对于版本1.2+,您必须使用kotlin-gradle-plugin org.jetbrains.kotlin.jvm.gradle.plugin

替换

答案 1 :(得分:0)

我一直在围绕定义项目buildscript块内部和外部的属性进行一些测试,但这似乎不起作用。如果您想访问与依赖关系相关的属性,可行的建议的解决方案是定义一个buildSrc模块(check this github repo),然后添加具有所需属性(版本,字符串...)的Kotlin文件/类。像这样:

object Dependency {

    object Foundation {

        const val KOTLIN = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.Global.KOTLIN}"
        const val RANDOM_VERSION = "1.2.3"
    }

您可以访问在buildSrc模块中声明的任何内容,因为如果我没有记错的话,在其中初始化的阶段中可以解析其中声明的组件。用法示例如下:

plugins {
    val kotlin = Dependency.Foundation.KOTLIN
    kotlin("kapt").version(Dependency.Foundation.RANDOM_VERSION)
}

更多信息here

答案 2 :(得分:-1)

UPD 无效,抱歉!

原始答案:

-

另一种解决方法是使用const val编译时常量:

object Defs {
    const val kotlinVersion = "1.2.60"
}

buildscript {
    val kotlinVersion:String by extra { Defs.kotlinVersion }
}

plugins {
    kotlin("jvm") version Defs.kotlinVersion
}