Search code examples
kotlingradle-kotlin-dslkotlin-dsl

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.

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?


Solution

  • Gradle defines a list of implicit imports that has no mechanism for extending this list. This is the same as for build.gradle and Groovy versions as it is for the Kotlin versions.

    See also: Automatic imports in Gradle plugin

    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:

    // TODO: let this be contributed by :plugins