プラグイン拡張と設定可能なDSL
拡張オブジェクト、プロパティ、規約を使ってGradleプラグインに洗練された設定DSLを提供し、使いやすい開発者体験を実現します。
「プラグイン拡張と設定可能なDSL」はCoddyKit上の無料Groovy & Gradle: JVM Automation and Build Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGroovy & Gradle: JVM Automation and Build Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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
conventionfor lazy defaults - Extensions separate user intent from plugin mechanics
よくある質問
「プラグイン拡張と設定可能なDSL」レッスンは無料ですか?
はい。「プラグイン拡張と設定可能なDSL」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Groovy & Gradle: JVM Automation and Build Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
「プラグイン拡張と設定可能なDSL」で何を学びますか?
拡張オブジェクト、プロパティ、規約を使ってGradleプラグインに洗練された設定DSLを提供し、使いやすい開発者体験を実現します。 ブラウザで直接実行するハンズオンコードでGroovy & Gradle: JVM Automation and Build Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Groovy & Gradle: JVM Automation and Build Engineeringを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGroovy & Gradle: JVM Automation and Build Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「プラグイン拡張と設定可能なDSL」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGroovy & Gradle: JVM Automation and Build Engineeringレッスンでコードを書いて実行できますか?
はい。すべてのGroovy & Gradle: JVM Automation and Build Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Gradleプラグインを理解する
- バイナリプラグインの構築
- プラグインの適用と公開
- プラグイン拡張と設定可能なDSL