0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · درس

كتابة مهام مخصصة باستخدام Kotlin DSL

أنشئ مهام Gradle مخصصة قابلة لإعادة الاستخدام وآمنة الأنواع باستخدام Kotlin DSL، مع خصائص مكتوبة وتهيئة كسولة وأنماط تسجيل واضحة.

كتابة مهام مخصصة باستخدام Kotlin DSL درس مجاني في 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 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Custom Tasks?

Built-in tasks cover common work, but real builds need project-specific automation: code generation, asset processing, custom checks. The Kotlin DSL makes these typesafe and readable.

Ad-hoc Task Registration

The simplest task is registered inline with an action block.

tasks.register("hello") {
    doLast {
        println("Hello from Gradle")
    }
}

register vs create

Prefer register over create: registration is lazy, so the task is only configured if it is actually needed, improving configuration time.

Typed Task Classes

For reusable logic, define a class extending DefaultTask with an annotated action.

abstract class GreetTask : DefaultTask() {
    @TaskAction
    fun run() {
        println("Hi")
    }
}

Typed Properties

Use Gradle Property types for lazy, configurable inputs that integrate with the build model.

abstract class GreetTask : DefaultTask() {
    @get:Input
    abstract val name: Property<String>
}

Registering a Typed Task

Register the class and configure its properties in the Kotlin DSL with full type checking.

tasks.register<GreetTask>("greet") {
    name.set("Ada")
}

Inputs and Outputs

Annotate properties so Gradle can make the task incremental and cacheable.

@get:OutputFile
abstract val report: RegularFileProperty

Lazy Configuration

Lazy Provider values defer computation until needed, avoiding work during configuration and enabling correct task dependencies.

val versionProvider = providers.gradleProperty("appVersion")

Wiring Task Dependencies

Express order with dependsOn, or better, wire outputs of one task into the inputs of another so Gradle infers the order.

tasks.register("publishDocs") {
    dependsOn("generateDocs")
}

Reusing Across Modules

Move the task class into buildSrc or a convention plugin so every module can register it without duplication.

Kotlin DSL Advantages

Compared to Groovy, the Kotlin DSL gives:

  • IDE autocompletion and refactoring
  • Compile-time error detection
  • Easier navigation to task sources

Quick Check

Test your understanding of custom tasks.

Recap

You learned custom tasks in Kotlin DSL:

  • Use lazy register over create
  • Define typed tasks extending DefaultTask
  • Use Property and annotations for typed inputs/outputs
  • Reuse via buildSrc or convention plugins

الأسئلة الشائعة

هل درس «كتابة مهام مخصصة باستخدام Kotlin DSL» مجاني؟

نعم — نص درس «كتابة مهام مخصصة باستخدام Kotlin DSL» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.

ماذا ستتعلم في «كتابة مهام مخصصة باستخدام Kotlin DSL»؟

أنشئ مهام Gradle مخصصة قابلة لإعادة الاستخدام وآمنة الأنواع باستخدام Kotlin 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.

كم من الوقت يستغرق درس «كتابة مهام مخصصة باستخدام Kotlin DSL»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟

نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. Kotlin DSL لـ Gradle
  2. بناء المشاريع متعددة اللغات
  3. تكامل IDE وأدوات التطوير
  4. كتابة مهام مخصصة باستخدام Kotlin DSL
← العودة إلى Groovy & Gradle: JVM Automation and Build Engineering