is there a trick to use the kotlin DSL when I am w...
# gradle
w
is there a trick to use the kotlin DSL when I am writing a gradle plugin? my build.gradle looks like
Copy code
plugins {
    id 'java-gradle-plugin'
    id 'org.jetbrains.kotlin.jvm'
}

dependencies {
    compileOnly gradleApi()
    implementation "org.scalastyle:scalastyle_2.12:1.0.0"

    testCompileOnly gradleApi()
    testCompile 'junit:junit:4.13.1'
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
    testImplementation("org.junit.jupiter:junit-jupiter-params:$junit5Version")
    testImplementation "org.assertj:assertj-core:$assertJVersion"
}
and i cant seem to access the
project.tasks.register<TaskType>(name: String)
among other things
c
There’s a good guide in the Gradle documentation for migrating scripts from Kotlin to Gradle. Creating and configuring tasks, in particular, is quite different from Groovy https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html#configuring-tasks
t
AFAIK no - as Kotlin DSL is generated by Gradle on first run. You need to introduce similar functions in your plugin codebase
v
Both answers are wrong or misleading, so here again for reference what I wrote in the duplicate question in the Gradle Slack: Not so much a trick, just a missing dependency:
compileOnly gradleKotlinDsl()
But actually you usually want to apply the
kotlin-dsl
or
kotlin-dsl.base
plugins to for example also get the SAM conversion for Gradle interfaces.
kotlin-dsl
applies
java-gradle-plugin
,
kotlin-dsl.base
, and
kotlin-dsl.precompiled-script-plugins
plugins
kotlin-dsl.base
plugin applies
embedded-kotlin
, adds
gradleKotlinDsl()
to the dependencies of
compileOnly
and
testImplementation
configurations, and configures the Kotlin DSL compiler plugins for example for proper SAM conversion for
Action
and similar.
👍 3
What is generated and only for build scripts and precompiled script plugins, are the accessors for extensions that were added by plugins applied using the
plugins
block. But all the standard things like
register
with type parameter and similar is available in that dependency.