1

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?

Kane
  • 8,035
  • 7
  • 46
  • 75

1 Answers1

2

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

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • Thanks for your comprehensive answer. It's a bit difference with what I asked. The question is not relative to writing gradle kotlin dsl script. I would like to implicitly import classes for self-created kotlin dsl. Looks like gradle is good example as reference for how to implicitly importing classes in kotlin dsl script. – Kane Dec 05 '18 at 07:19
  • You just want Kotlin-script implied imports? That is supported by Kotlin script and is documented. "Kotlin-dsl script" isn't something that exists, other than the Kotlin DSL for Gradle (using that terminology). So you want any Kotlin Script to have implied imports? – Jayson Minard Dec 06 '18 at 02:15
  • Yes. I'm creating my own dsl script not writing gradle script. I would like to implicitly imports in my dsl script. – Kane Dec 06 '18 at 09:16