0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lección

Extensiones de plugins y DSL configurables

Proporcione a su plugin de Gradle un DSL de configuración claro mediante objetos de extensión, propiedades y convenciones, para ofrecer una experiencia de usuario cuidada.

Extensiones de plugins y DSL configurables es una lección gratuita de Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Groovy & Gradle: JVM Automation and Build Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Configuring a Plugin

A good plugin lets users customize its behaviour through a readable block in their build script. Gradle calls this an extension — it is how plugins like the Java plugin expose settings.

What Is an Extension?

An extension is a plain object your plugin registers under a name. Users set its properties in a matching DSL block, and your tasks read those values.

Defining an Extension Class

Create a class holding the configurable properties.

class GreetingExtension {
    String message = "Hello"
    String recipient = "World"
}

Registering the Extension

In the plugin apply method, register the extension under a DSL name.

void apply(Project project) {
    project.extensions.create('greeting', GreetingExtension)
}

Configuring in build.gradle

Users now configure the plugin with a clean block matching the extension name.

greeting {
    message = 'Hi'
    recipient = 'Gradle'
}

Reading Extension Values in a Task

Wire the extension values into a task so configuration drives behaviour at execution time.

def ext = project.extensions.greeting
project.tasks.register('greet') {
    doLast { println "${ext.message}, ${ext.recipient}!" }
}

Lazy Properties

Modern plugins use the Provider API (Property<String>) so values are evaluated lazily, after the user has finished configuring.

abstract class Ext {
    abstract Property<String> getMessage()
}

Setting Conventions

Give sensible defaults with convention so the plugin works out of the box but stays overridable.

message.convention('Hello')

Nested Extensions

Complex plugins nest extensions, e.g. a publishing block containing a repositories sub-block, by registering child objects.

Configuring Tasks from the Extension

The robust pattern is to register tasks and wire their lazy properties from the extension inside project.afterEvaluate or via providers, so user configuration is fully applied before values are read.

project.afterEvaluate {
    project.tasks.greet.configure { message = project.extensions.greeting.message }
}

Why Extensions Matter

Extensions separate what the user wants from how the plugin works. They make plugins discoverable, type-safe, and pleasant to configure — the hallmark of a reusable plugin.

Quick Check

Test your plugin extension knowledge.

Recap

You learned to make plugins configurable:

  • Extensions expose a named DSL block to users
  • Register with project.extensions.create
  • Tasks read extension values at execution time
  • Use the Provider API and convention for lazy defaults
  • Extensions separate user intent from plugin mechanics

Preguntas frecuentes

¿La lección «Extensiones de plugins y DSL configurables» es gratis?

Sí — el texto completo de «Extensiones de plugins y DSL configurables» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Groovy & Gradle: JVM Automation and Build Engineering, actualiza a CoddyKit PRO. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.

¿Qué aprenderé en «Extensiones de plugins y DSL configurables»?

Proporcione a su plugin de Gradle un DSL de configuración claro mediante objetos de extensión, propiedades y convenciones, para ofrecer una experiencia de usuario cuidada. Practicas Groovy & Gradle: JVM Automation and Build Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Groovy & Gradle: JVM Automation and Build Engineering?

No se requiere experiencia previa. Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Extensiones de plugins y DSL configurables»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Groovy & Gradle: JVM Automation and Build Engineering?

Sí. Cada lección de Groovy & Gradle: JVM Automation and Build Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Comprensión de los plugins de Gradle
  2. Construcción de plugins binarios
  3. Aplicación y publicación de plugins
  4. Extensiones de plugins y DSL configurables
← Volver a Groovy & Gradle: JVM Automation and Build Engineering