Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

536
Vistas
How do I use the Gradle Kotlin DSL inside a plugin?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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!"
                )
            }
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda