Hello, probably a noob question. But how can I in ...
# plugin-development
m
Hello, probably a noob question. But how can I in a gradle plugin using Kotlin DSL define the configuration closure? I get the following error. I also can't do the usual
Copy code
project.dependencies {
    implementation("org.springframework.boot:spring-boot-gradle-plugin:3.3.4")
}
I must write
Copy code
project.dependencies.apply {
            this.add("compileOnly", "jakarta.servlet:jakarta.servlet-api")
        }
I wanted to use this
KotlinClosure0
but it seems not to be available.
Maybe it is not part of gradle api
t
Can you provide more details on what you mean by "in a gradle plugin using Kotlin DSL"? Is this a precompiled script plugin? A plugin written in Kotlin where you have a dependency on the Kotlin DSL (e.g. through using the
kotlin-dsl
plugin)? In the latter case, you have to
import
the Kotlin DSL extensions.
m
It's a plugin writen in kotlin, and I don't have a dependency to Kotlin DSL currently.
Should I add this to the build.gradle.kts of my plugin?
Copy code
implementation("org.gradle.kotlin:gradle-kotlin-dsl-plugins:5.1.2")
Ah found this `
Copy code
gradleKotlinDsl()
Despite adding the kotlin dsl plugin as a dependency to my project, I still can't access the extensions defined on DependencyHandler
Neither how to properly use closureOf
a
once you've added a dependency on
gradleKotlinDSl()
make sure to add the import
Copy code
import org.gradle.kotlin.dsl.*
v
Despite adding the kotlin dsl plugin as a dependency to my project, I still can't access the extensions defined on DependencyHandler
gradleKotlinDsl()
is not a dependency on the
kotlin-dsl
plugin. It is a dependency to on the Kotlin DSL. The
kotlin-dsl
plugin also adds this dependency to your project and additionally does some other things like configuring the assignment Kotlin compiler plugin which allows you to set `Property`s using
=
or the
sam-with-receiver
Kotlin compiler plugin.
I still can't access the extensions defined on DependencyHandler
implementation(...)
is not a fixed extension on DependencyHandler. In a Kotlin DSL build script where you applied some plugin in the
plugins { ... }
block that adds a configuration called
implementation
you have a generated accessor extension function that lets you do
implementation("...")
on the dependency handler. In a normal Kotlin file, you don't have such accessors. So you have to have a variable of type
Configuration
or
Provider<Configuration>
, and then you can use that syntax if you imported
org.gradle.kotlin.dsl.invoke
.