简体   繁体   English

编写自定义kotlin-dsl时如何忽略显式导入诸如gradle kotlin脚本的类

[英]How to ignore explicitly importing classes like gradle kotlin script when writing custom kotlin-dsl

In the gradle's kotlin build script, we don't need explicitly import classes or functions like plugins , repositories or dependencies in build script build.gradle.kts . 在gradle的kotlin构建脚本中,我们不需要在构建脚本build.gradle.kts中显式导入类或函数,例如pluginsrepositoriesdependencies

plugins {
    val kotlinVersion = "1.3.10"
    val springBootVersion = "2.1.0.RELEASE"
    val detektVersion = "1.0.0-RC10"

    id("org.springframework.boot") version springBootVersion
    id("org.jetbrains.kotlin.jvm") version kotlinVersion
    id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
    id("io.spring.dependency-management") version "1.0.6.RELEASE"
    id("io.gitlab.arturbosch.detekt") version detektVersion
}


repositories {
    mavenLocal()

    mavenCentral()
    maven(url = uri("https://dl.bintray.com/s1m0nw1/KtsRunner"))
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-script-runtime")
    implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
    implementation("org.jetbrains.kotlin:kotlin-script-util")

    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("cn.pilipa:pilipa-spring-boot-starter-logging:2.0.10")

    implementation("de.swirtz:ktsRunner:0.0.5")

    testImplementation("org.springframework.boot:spring-boot-starter-test"){
        exclude(module = "junit")
    }
    testImplementation("io.projectreactor:reactor-test")
    testImplementation("org.springframework.cloud:spring-cloud-stream-test-support")
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntime("org.junit.jupiter:junit-jupiter-engine")
    testCompile("io.kotlintest:kotlintest-runner-junit5:${kotlinTestVersion}")
    testCompile("io.kotlintest:kotlintest-extensions-spring:${kotlinTestVersion}")

    detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:${detektVersion}")
}

How to implement this similar feature in the custom kotlin-dsl script to implicitly import classes in kotlin-dsl script? 如何在自定义kotlin-dsl脚本中实现此类似功能,以隐式导入kotlin-dsl脚本中的类?

Gradle defines a list of implicit imports that has no mechanism for extending this list. Gradle定义了一个隐式导入列表,该列表没有扩展此列表的机制。 This is the same as for build.gradle and Groovy versions as it is for the Kotlin versions. 这与build.gradle和Groovy版本相同,与Kotlin版本相同。

See also: Automatic imports in Gradle plugin 另请参阅: Gradle插件中的自动导入

Which still holds true as of today. 到今天仍然适用。 There is a TODO remaining in the Kotlin Gradle Script source code ( master branch as-of Nov 22, 2018 ) related to this: 与以下内容相关的Kotlin Gradle脚本源代码截至2018年11月22日为主分支)中还有一个待办事项:

// TODO: let this be contributed by :plugins // TODO:由:plugins贡献

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM