0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lektion

Benutzerdefinierte Tasks definieren

Schreiben Sie eigene Gradle-Tasks mit der Groovy- oder Kotlin-DSL, um bestimmte Build-Schritte zu automatisieren.

Benutzerdefinierte Tasks definieren ist eine kostenlose Groovy & Gradle: JVM Automation and Build Engineering-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Groovy & Gradle: JVM Automation and Build Engineering-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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!

Häufig gestellte Fragen

Ist die Lektion „Benutzerdefinierte Tasks definieren“ kostenlos?

Ja — der vollständige Text von „Benutzerdefinierte Tasks definieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Groovy & Gradle: JVM Automation and Build Engineering-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Benutzerdefinierte Tasks definieren“?

Schreiben Sie eigene Gradle-Tasks mit der Groovy- oder Kotlin-DSL, um bestimmte Build-Schritte zu automatisieren. Du übst Groovy & Gradle: JVM Automation and Build Engineering mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Groovy & Gradle: JVM Automation and Build Engineering zu starten?

Keine Vorkenntnisse erforderlich. Groovy & Gradle: JVM Automation and Build Engineering auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Benutzerdefinierte Tasks definieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Groovy & Gradle: JVM Automation and Build Engineering-Lektion Code schreiben und ausführen?

Ja. Jede Groovy & Gradle: JVM Automation and Build Engineering-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Benutzerdefinierte Tasks definieren
  2. Task-Typen und Aktionen
  3. Task-Eigenschaften und Konfiguration
  4. Inkrementelle Tasks und Up-to-Date-Prüfungen
← Zurück zu Groovy & Gradle: JVM Automation and Build Engineering