تعريف المهام المخصصة
اكتب مهام Gradle الخاصة بكم باستخدام Groovy أو Kotlin DSL لأتمتة خطوات بناء محددة
تعريف المهام المخصصة درس مجاني في Groovy & Gradle: JVM Automation and Build Engineering على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Groovy & Gradle: JVM Automation and Build Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Custom Tasks: Why & How?
Why define your own Gradle tasks? They let you automate unique steps not covered by standard plugins. Think of custom tasks as recipes for specific build actions.
This lesson will show you how to craft them to extend Gradle's capabilities!
Your First Custom Task
The simplest way to create a custom task is directly in your build.gradle file. Use the task keyword followed by your task's name.
Let's create a task named helloCustom:
// build.gradle
task helloCustom {
doLast {
println 'Hello from a custom task!'
}
}Task Actions: doLast & doFirst
Tasks can have multiple actions. You can define them using doFirst and doLast blocks. These blocks specify code to run before or after the task's main actions.
// build.gradle
task orderedActions {
doFirst {
println 'This runs first!'
}
doLast {
println 'This runs last!'
}
doLast { // Can have multiple doLast/doFirst
println 'This runs after the previous doLast!'
}
}Beyond Inline: Custom Task Classes
For more complex logic or reusability across projects, you'll want to define a custom task as a class. This allows for better organization, testing, and property management.
- Encapsulation: Keep task logic in one place.
- Reusability: Share tasks between different projects.
- Configuration: Define properties for customization.
Writing a Custom Task Class
Custom task classes extend Gradle's DefaultTask. You define the task's actions within a method, often annotated with @TaskAction.
Create a file named src/main/groovy/com/coddykit/tasks/MyGreetingTask.groovy:
package com.coddykit.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyGreetingTask extends DefaultTask {
@TaskAction
def greet() {
println "Hello from MyGreetingTask!"
}
}Using Your Custom Task Class
To use your custom task class, you need to tell Gradle where to find it and then register an instance of it. First, add the Groovy plugin to compile your task class.
Update your build.gradle:
// build.gradle
plugins {
id 'groovy' // To compile Groovy task classes
}
repositories {
mavenCentral()
}
dependencies {
// Required for DefaultTask
implementation gradleApi()
// If your task uses Groovy
implementation 'org.codehaus.groovy:groovy-all:3.0.10'
}
// Register our custom task
tasks.register('myGreeting', com.coddykit.tasks.MyGreetingTask)Executing the Class-Based Task
Now that your custom task class is defined and registered in build.gradle, you can execute it just like any other Gradle task from your terminal.
Run the command:
gradle myGreetingTask Types vs. Definitions
It's important to distinguish between defining a task type (the class) and defining a task instance (registering it).
- Task Type: The
MyGreetingTaskclass itself, which extendsDefaultTask. It defines what the task can do. - Task Definition:
tasks.register('myGreeting', MyGreetingTask). This creates a specific task named 'myGreeting' of that type.
Custom Task Quiz
You've learned how to create custom tasks. Let's test your understanding!
Defining Custom Tasks Recap
Great job! You've learned how to define custom Gradle tasks:
- Inline Tasks: Using
task name { doLast { ... } }for simple, script-local actions. - Task Actions: Controlling execution order with
doFirstanddoLast. - Custom Task Classes: Extending
DefaultTaskfor reusable, organized, and configurable task logic. - Registering Tasks: Using
tasks.register()to create instances of your custom task classes.
Next, we'll dive deeper into task types and how to configure properties for more dynamic tasks!
تعلم Groovy مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «تعريف المهام المخصصة» مجاني؟
نعم — نص درس «تعريف المهام المخصصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Groovy & Gradle: JVM Automation and Build Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة Groovy & Gradle: JVM Automation and Build Engineering 4 دروس في المجموع.
ماذا ستتعلم في «تعريف المهام المخصصة»؟
اكتب مهام Gradle الخاصة بكم باستخدام Groovy أو 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 منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعريف المهام المخصصة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Groovy & Gradle: JVM Automation and Build Engineering هذا؟
نعم. كل درس في Groovy & Gradle: JVM Automation and Build Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف المهام المخصصة
- أنواع المهام وإجراءاتها
- خصائص المهام وإعدادها
- المهام التزايدية وفحوصات حداثة النتائج