Groovy & Gradle: JVM Automation and Build Engineering · Ders

Özel Görevler Tanımlama

Belirli derleme adımlarını otomatikleştirmek için Groovy veya Kotlin DSL kullanarak kendi Gradle görevlerinizi yazın.

1. ders / 410 adım

Özel Görevler Tanımlama, CoddyKit'te ücretsiz bir Groovy & Gradle: JVM Automation and Build Engineering dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Groovy & Gradle: JVM Automation and Build Engineering öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Groovy & Gradle: JVM Automation and Build Engineering kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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 myGreeting

Task 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 MyGreetingTask class itself, which extends DefaultTask. 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 doFirst and doLast.
  • Custom Task Classes: Extending DefaultTask for 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!

Başlamak ücretsiz

Yapay zeka eğitmeniyle Groovy öğren — ücretsiz

Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.

Kurslar
12
Dersler
48

Sıkça Sorulan Sorular

“Özel Görevler Tanımlama” dersi ücretsiz mi?

Evet — “Özel Görevler Tanımlama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Groovy & Gradle: JVM Automation and Build Engineering kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Groovy & Gradle: JVM Automation and Build Engineering kursu toplamda 4 dersten oluşur.

“Özel Görevler Tanımlama” dersinde ne öğreneceğim?

Belirli derleme adımlarını otomatikleştirmek için Groovy veya Kotlin DSL kullanarak kendi Gradle görevlerinizi yazın. Groovy & Gradle: JVM Automation and Build Engineering ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Groovy & Gradle: JVM Automation and Build Engineering öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Groovy & Gradle: JVM Automation and Build Engineering, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“Özel Görevler Tanımlama” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Groovy & Gradle: JVM Automation and Build Engineering dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Groovy & Gradle: JVM Automation and Build Engineering dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Özel Görevler Tanımlama
  2. Görev Türleri ve Eylemler
  3. Görev Özellikleri ve Yapılandırma
  4. Artımlı Görevler ve Güncellik Denetimleri
← Groovy & Gradle: JVM Automation and Build Engineering Sayfasına Dön