0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Aula

Extensões de Plugins e DSLs Configuráveis

Dê ao seu plugin Gradle uma DSL de configuração clara usando objetos de extensão, propriedades e convenções, para uma experiência de utilização aprimorada.

Extensões de Plugins e DSLs Configuráveis é uma aula grátis de Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Groovy & Gradle: JVM Automation and Build Engineering, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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

Perguntas Frequentes

A aula “Extensões de Plugins e DSLs Configuráveis” é grátis?

Sim — o texto completo de “Extensões de Plugins e DSLs Configuráveis” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Groovy & Gradle: JVM Automation and Build Engineering, atualize para CoddyKit PRO. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.

O que vou aprender em “Extensões de Plugins e DSLs Configuráveis”?

Dê ao seu plugin Gradle uma DSL de configuração clara usando objetos de extensão, propriedades e convenções, para uma experiência de utilização aprimorada. Você pratica Groovy & Gradle: JVM Automation and Build Engineering com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Groovy & Gradle: JVM Automation and Build Engineering?

Nenhuma experiência prévia é necessária. Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Extensões de Plugins e DSLs Configuráveis”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Groovy & Gradle: JVM Automation and Build Engineering?

Sim. Cada aula de Groovy & Gradle: JVM Automation and Build Engineering inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Compreendendo os plugins do Gradle
  2. Construindo plugins binários
  3. Aplicando e publicando plugins
  4. Extensões de Plugins e DSLs Configuráveis
← Voltar para Groovy & Gradle: JVM Automation and Build Engineering