ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้
มอบ DSL การกำหนดค่าที่เป็นระเบียบให้ปลั๊กอิน Gradle ด้วยออบเจกต์ส่วนขยาย พร็อพเพอร์ตี และข้อตกลง เพื่อประสบการณ์ผู้ใช้ที่ราบรื่น
ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้ เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เรียนรู้ Groovy ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้”
มอบ DSL การกำหนดค่าที่เป็นระเบียบให้ปลั๊กอิน Gradle ด้วยออบเจกต์ส่วนขยาย พร็อพเพอร์ตี และข้อตกลง เพื่อประสบการณ์ผู้ใช้ที่ราบรื่น คุณปฏิบัติ Groovy & Gradle: JVM Automation and Build Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจปลั๊กอิน Gradle
- การสร้างปลั๊กอินไบนารี
- การใช้และเผยแพร่ปลั๊กอิน
- ส่วนขยายปลั๊กอินและ DSL ที่กำหนดค่าได้