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

Rozszerzenia pluginów i konfigurowalne DSL-e

Zapewnij swojemu pluginowi Gradle przejrzysty DSL konfiguracji za pomocą obiektów rozszerzeń, właściwości i konwencji, oferując dopracowane doświadczenie użytkownika.

Rozszerzenia pluginów i konfigurowalne DSL-e to bezpłatna lekcja Groovy & Gradle: JVM Automation and Build Engineering na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Groovy & Gradle: JVM Automation and Build Engineering, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Rozszerzenia pluginów i konfigurowalne DSL-e” jest bezpłatna?

Tak — pełny tekst „Rozszerzenia pluginów i konfigurowalne DSL-e” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Groovy & Gradle: JVM Automation and Build Engineering, przejdź na CoddyKit PRO. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Co nauczysz się w „Rozszerzenia pluginów i konfigurowalne DSL-e”?

Zapewnij swojemu pluginowi Gradle przejrzysty DSL konfiguracji za pomocą obiektów rozszerzeń, właściwości i konwencji, oferując dopracowane doświadczenie użytkownika. Ćwiczysz Groovy & Gradle: JVM Automation and Build Engineering z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Groovy & Gradle: JVM Automation and Build Engineering?

Nie wymagamy żadnego doświadczenia. Groovy & Gradle: JVM Automation and Build Engineering w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Rozszerzenia pluginów i konfigurowalne DSL-e”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Groovy & Gradle: JVM Automation and Build Engineering?

Tak. Każda lekcja Groovy & Gradle: JVM Automation and Build Engineering zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zrozumienie wtyczek Gradle
  2. Tworzenie wtyczek binarnych
  3. Stosowanie i publikowanie wtyczek
  4. Rozszerzenia pluginów i konfigurowalne DSL-e
← Powrót do Groovy & Gradle: JVM Automation and Build Engineering