定义自定义任务
使用 Groovy 或 Kotlin DSL 编写自己的 Gradle 任务,自动执行特定构建步骤。
定义自定义任务 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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!
常见问题解答
「定义自定义任务」课时是免费的吗?
是的 — 「定义自定义任务」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「定义自定义任务」这节课中我会学到什么?
使用 Groovy 或 Kotlin DSL 编写自己的 Gradle 任务,自动执行特定构建步骤。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「定义自定义任务」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义自定义任务
- 任务类型与操作
- 任务属性与配置
- 增量任务与最新状态检查