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

Estensioni dei plugin e DSL configurabili

Offra al suo plugin Gradle una DSL di configurazione chiara usando oggetti di estensione, proprietà e convenzioni, per un'esperienza utente curata.

Estensioni dei plugin e DSL configurabili è una lezione Groovy & Gradle: JVM Automation and Build Engineering gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Groovy & Gradle: JVM Automation and Build Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Estensioni dei plugin e DSL configurabili» è gratuita?

Sì — il testo completo di «Estensioni dei plugin e DSL configurabili» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Groovy & Gradle: JVM Automation and Build Engineering, passa a CoddyKit PRO. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Cosa imparerò in «Estensioni dei plugin e DSL configurabili»?

Offra al suo plugin Gradle una DSL di configurazione chiara usando oggetti di estensione, proprietà e convenzioni, per un'esperienza utente curata. Eserciti Groovy & Gradle: JVM Automation and Build Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Groovy & Gradle: JVM Automation and Build Engineering?

Non è richiesta alcuna esperienza precedente. Groovy & Gradle: JVM Automation and Build Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Estensioni dei plugin e DSL configurabili»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Groovy & Gradle: JVM Automation and Build Engineering?

Sì. Ogni lezione Groovy & Gradle: JVM Automation and Build Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Comprendere i plugin Gradle
  2. Creazione di plugin binari
  3. Applicazione e pubblicazione dei plugin
  4. Estensioni dei plugin e DSL configurabili
← Torna a Groovy & Gradle: JVM Automation and Build Engineering