I am authoring a Gradle plugin.
I am writing code like this calling the Groovy/Java Gradle APIs:
package com.example
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
class HelloPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.afterEvaluate {
project.tasks.register("hello", Exec::class.java) { task ->
task.commandLine = listOf(
"echo",
"Hello, world!"
)
}
}
}
}
I would prefer to write code like this:
package com.example
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
class HelloPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.afterEvaluate {
project.tasks.register<Exec>("hello") { task ->
task.commandLine = listOf(
"echo",
"Hello, world!"
)
}
}
}
}
After enabling the kotlin-dsl plugin in build.gradle.kts I get compiler errors in the original unchanged HelloPlugin.kt:
e: /home/example/Documents/gradle-com.example.hello-plugin/src/main/kotlin/com/example/HelloPlugin.kt: (11, 27): None of the following functions can be called with the arguments supplied:
public abstract fun register(p0: String, p1: Class, vararg p2: Any!): TaskProvider defined in org.gradle.api.tasks.TaskContainer
public abstract fun register(p0: String, p1: Class, p2: Action): TaskProvider defined in org.gradle.api.tasks.TaskContainer
e: /home/example/Documents/gradle-com.example.hello-plugin/src/main/kotlin/com/example/HelloPlugin.kt: (12, 22): Unresolved reference: commandLine
e: /home/example/Documents/gradle-com.example.hello-plugin/src/main/kotlin/com/verafin/aws/lambda/AbstractLambdaPlugin.kt: (76, 53): None of the following functions can be called with the arguments supplied:
public abstract fun register(p0: String, p1: Class, vararg p2: Any!): TaskProvider defined in org.gradle.api.tasks.TaskContainer
public abstract fun register(p0: String, p1: Class, p2: Action): TaskProvider defined in org.gradle.api.tasks.TaskContainer
Full runnable project is at: https://github.com/AlainODea/gradle-com.example.hello-plugin
How do I use the Gradle Kotlin DSL inside a plugin?
In build.gradle.kts, the Gradle Kotlin DSL is loaded with the plugin. In Kotlin classes in your plugin implementation, you need to import the Gradle Kotlin DSL explicitly:
import org.gradle.kotlin.dsl.*
Here's a full working example Gradle plugin Kotlin class using the Kotlin Gradle DSL:
package com.example
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.*
class HelloPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.afterEvaluate {
project.tasks.register<Exec>("hello") { task ->
task.commandLine = listOf(
"echo",
"Hello, world!"
)
}
}
}
}
With the Kotlin Gradle DSL, you can omit the explicitly named closure parameters and make it even cleaner:
package com.example
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.*
class HelloPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.afterEvaluate {
tasks.register<Exec>("hello") {
commandLine = listOf(
"echo",
"Hello, world!"
)
}
}
}
}