ชนิดงานและการดำเนินการ
สำรวจชนิดงานต่าง ๆ กำหนดการดำเนินการของงาน และจัดการอินพุตกับเอาต์พุตของงาน
ชนิดงานและการดำเนินการ เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ชนิดงานและการดำเนินการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดงานแบบกำหนดเอง
- ชนิดงานและการดำเนินการ
- คุณสมบัติและการกำหนดค่างาน
- งานแบบเพิ่มทีละน้อยและการตรวจสอบความเป็นปัจจุบัน