امتدادات الإضافات وDSLs القابلة للتهيئة
امنح إضافة Gradle الخاصة بك DSL نظيفًا للتهيئة باستخدام كائنات الامتداد والخصائص والاصطلاحات، لتوفير تجربة استخدام متقنة.
امتدادات الإضافات وDSLs القابلة للتهيئة درس مجاني في Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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
الأسئلة الشائعة
هل درس «امتدادات الإضافات وDSLs القابلة للتهيئة» مجاني؟
نعم — نص درس «امتدادات الإضافات وDSLs القابلة للتهيئة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
ماذا ستتعلم في «امتدادات الإضافات وDSLs القابلة للتهيئة»؟
امنح إضافة Gradle الخاصة بك DSL نظيفًا للتهيئة باستخدام كائنات الامتداد والخصائص والاصطلاحات، لتوفير تجربة استخدام متقنة. تتمرن على Groovy & Gradle: JVM Automation and Build Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Groovy & Gradle: JVM Automation and Build Engineering؟
لا تُشترط خبرة سابقة. Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «امتدادات الإضافات وDSLs القابلة للتهيئة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟
نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- فهم إضافات Gradle
- بناء الإضافات الثنائية
- تطبيق الإضافات ونشرها
- امتدادات الإضافات وDSLs القابلة للتهيئة