任务类型与操作
探索不同的任务类型,定义任务操作,并管理任务输入和输出。
任务类型与操作 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Tasks: Types & Actions Intro
In Gradle, tasks are the core units of work. While Lesson 1 showed how to define basic tasks, this lesson dives deeper into task types and actions.
We'll learn how different task types offer specific functionalities, define what a task actually does using actions, and understand how to manage task inputs and outputs for efficient builds.
What are Task Actions?
A task action is a piece of code that a task executes. Think of it as the 'what to do' part of your task.
You can define actions directly within a task block or using special methods like doLast and doFirst.
- Directly: Code inside
doLast { ... }or the task block itself. doLast: Executes the action at the very end of the task's execution.doFirst: Executes the action at the very beginning of the task's execution.
Simple Task Action Example
Let's create a simple Gradle task with an action using doLast. This action will print a message to the console.
Save this as build.gradle and run gradle helloAction.
task helloAction {
doLast {
println 'Hello from a Gradle action!'
}
}doFirst vs doLast Explained
When a task has multiple actions, their order matters. doFirst actions run before any other actions, while doLast actions run after.
If you define actions directly in the task configuration block (without doFirst/doLast), they are implicitly added as doLast actions.
doFirst & doLast in Action
Observe the execution order in this example. The println outside of doFirst/doLast runs during the configuration phase.
Run gradle orderedActions to see the output.
task orderedActions {
doLast { println 'Action C: This is the main action (doLast).' }
doFirst { println 'Action B: This runs before everything.' }
doLast { println 'Action D: Another action at the end.' }
println 'Action A: This runs during configuration phase.'
}Built-in Task Types
Gradle isn't just for custom scripts! It provides many powerful built-in task types for common operations like compiling Java code, running tests, or copying files.
These types are classes that extend Gradle's DefaultTask and come with pre-defined properties and behaviors. You configure them instead of writing all the logic from scratch.
Example: The `Copy` Task Type
The Copy task type is used to copy files and directories. It's much more robust than a simple script for file operations.
To run this, first create an empty file named my_document.txt in your project root. Then execute gradle copyMyFile.
task copyMyFile(type: Copy) {
from '.' // Source directory (project root)
include 'my_document.txt' // File to copy
into 'build/backup' // Destination directory
}Example: The `JavaExec` Task Type
The JavaExec task type is designed to execute a Java application in a separate JVM process.
For this to run, you'd typically have a Java file (e.g., src/main/java/MyApp.java) with a main method. Here's how you'd define the task in build.gradle:
// In build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
task runMyJavaApp(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
mainClass = 'MyApp' // Your main class name
args 'Hello', 'World' // Optional arguments
}
// A sample src/main/java/MyApp.java file:
// public class MyApp {
// public static void main(String[] args) {
// System.out.println("Args: " + String.join(", ", args));
// }
// }Task Inputs: The 'What It Needs'
Task inputs are any values, files, or directories that a task uses to perform its work. Gradle tracks these inputs.
If the inputs haven't changed since the last build, Gradle knows it can skip executing the task (making your builds faster!). This is crucial for incremental builds.
For custom task classes (covered in the next lesson), you mark properties with annotations like @Input, @InputFile, or @InputDirectory.
Task Outputs: The 'What It Produces'
Task outputs are any files or directories a task creates or modifies. Gradle also tracks these.
By declaring outputs (e.g., with @OutputDirectory or @OutputFile for custom task classes), Gradle can cache task results, share them across builds, and properly manage task dependencies.
Understanding inputs and outputs is key to leveraging Gradle's build caching and parallel execution features.
Quick Check: Action Order
Consider a Gradle task definition:
task myTask {
doLast { println 'Action C' }
doFirst { println 'Action B' }
doLast { println 'Action D' }
println 'Action A'
}What is the correct order of output when gradle myTask is run?
Recap: Master Your Tasks!
Great job! You've explored deeper into Gradle tasks.
- We defined task actions using
doFirstanddoLastto control execution order. - You saw how to leverage powerful built-in task types like
CopyandJavaExec. - We also touched upon the importance of declaring task inputs and outputs for incremental builds and caching.
Next, we'll learn how to add properties to custom tasks and configure them for even more flexibility!
常见问题解答
「任务类型与操作」课时是免费的吗?
是的 — 「任务类型与操作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「任务类型与操作」这节课中我会学到什么?
探索不同的任务类型,定义任务操作,并管理任务输入和输出。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「任务类型与操作」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义自定义任务
- 任务类型与操作
- 任务属性与配置
- 增量任务与最新状态检查