งานและวงจรชีวิตการสร้าง
กำหนดและจัดการงานของ Gradle ทำความเข้าใจการพึ่งพาระหว่างงาน และทำความเข้าใจระยะต่าง ๆ ของวงจรชีวิตการสร้าง
งานและวงจรชีวิตการสร้าง เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are Gradle Tasks?
Welcome! In this lesson, we'll dive into Gradle Tasks, the fundamental units of work in any Gradle build.
- A task represents a single, atomic piece of work that Gradle performs.
- Think of tasks as actions like 'compile code', 'run tests', 'clean build directory', or 'deploy application'.
- They are the building blocks that make up your entire build process.
Discovering Built-in Tasks
Gradle comes with many built-in tasks. You can list all available tasks for your project using the command line:
gradle tasks
This command shows a categorized list of tasks, including those provided by plugins and any custom tasks you define. It's a great way to explore what your build can do!
Defining Your First Task
Creating your own custom task is straightforward in your build.gradle file. Let's define a simple task:
The task keyword is used, followed by the task's name. The curly braces {} define the task's actions.
task helloWorld {
println "Hello from CoddyKit Gradle!"
}Running Custom Tasks
To execute a custom task, you simply run Gradle from your terminal, specifying the task name:
gradle helloWorld
When you run the example from the previous scene, you'll see "Hello from CoddyKit Gradle!" printed in the console.
Task Actions: doLast & doFirst
Tasks can have multiple actions. You can define these actions using doLast and doFirst blocks.
doLast: Adds an action to be executed at the end of the task.doFirst: Adds an action to be executed at the beginning of the task.
These are useful for organizing complex task logic.
task myGreet {
doFirst {
println "Preparing to greet..."
}
doLast {
println "Hello, CoddyKit!"
}
}Chaining Tasks with dependsOn
Often, one task must complete before another can start. Gradle handles this with task dependencies using the dependsOn keyword.
If task 'B' depends on task 'A', then 'A' will always run before 'B'. This ensures operations happen in the correct order.
task prepare {
doLast {
println "Preparation complete."
}
}
task build(dependsOn: prepare) {
doLast {
println "Build finished."
}
}Understanding the Build Lifecycle
Every time you run a Gradle command, it goes through a distinct build lifecycle, consisting of three main phases:
- Initialization: Sets up the build environment.
- Configuration: Evaluates build scripts and configures tasks.
- Execution: Runs the configured tasks.
Understanding these phases is key to writing effective Gradle builds.
Phase 1: Initialization
The Initialization Phase is where Gradle determines which projects are participating in the build.
- It evaluates the
settings.gradlefile (if present). - This file defines the project hierarchy for multi-project builds.
- Gradle creates a
Projectinstance for each project involved.
This phase essentially sets the stage for the build.
Phase 2: Configuration
During the Configuration Phase, all build.gradle scripts for the projects are evaluated.
- Tasks are created and configured.
- Important: Any code directly in a task block (not inside
doLastordoFirst) runs during this phase, regardless of whether the task will actually be executed.
This is where tasks' properties and dependencies are set up.
task configExample {
println "This message runs during Configuration!"
doLast {
println "This message runs during Execution!"
}
}Phase 3: Execution
The final phase is the Execution Phase. This is where the actual work happens!
- Gradle determines which tasks need to run based on your command and task dependencies.
- It then executes the
doFirstanddoLastactions (and any other actions) of those selected tasks. - Tasks are executed in the correct order, respecting their dependencies.
This is when your code compiles, tests run, and artifacts are built.
Lifecycle Check
Let's test your understanding of Gradle's build lifecycle!
Tasks & Lifecycle Recap
Great job! You've learned the core concepts of Gradle tasks and the build lifecycle:
- Tasks are the fundamental units of work.
- You can define custom tasks and chain them with
dependsOn. - The Gradle build proceeds through three phases: Initialization, Configuration, and Execution.
- Code directly in a task block runs during Configuration, while
doFirst/doLastactions run during Execution.
Next, we'll explore how Gradle manages external dependencies!
เรียนรู้ Groovy ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “งานและวงจรชีวิตการสร้าง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “งานและวงจรชีวิตการสร้าง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “งานและวงจรชีวิตการสร้าง”
กำหนดและจัดการงานของ Gradle ทำความเข้าใจการพึ่งพาระหว่างงาน และทำความเข้าใจระยะต่าง ๆ ของวงจรชีวิตการสร้าง คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “งานและวงจรชีวิตการสร้าง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้ง Gradle และ CLI
- โครงสร้างโครงการ Gradle
- งานและวงจรชีวิตการสร้าง
- การสร้างหลายโครงการและไฟล์การตั้งค่า