Groovy & Gradle: JVM Automation and Build Engineering · บทเรียน

การเขียนงานที่กำหนดเองด้วย Kotlin DSL

สร้างงาน Gradle ที่กำหนดเองและนำกลับมาใช้ซ้ำได้อย่างปลอดภัยด้านชนิดข้อมูลด้วย Kotlin DSL พร้อมพร็อพเพอร์ตีแบบมีชนิด การกำหนดค่าแบบขี้เกียจ และรูปแบบการลงทะเบียนที่เป็นระเบียบ

บทเรียน 4 จาก 413 ขั้นตอน

การเขียนงานที่กำหนดเองด้วย Kotlin DSL เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Custom Tasks?

Built-in tasks cover common work, but real builds need project-specific automation: code generation, asset processing, custom checks. The Kotlin DSL makes these typesafe and readable.

Ad-hoc Task Registration

The simplest task is registered inline with an action block.

tasks.register("hello") {
    doLast {
        println("Hello from Gradle")
    }
}

register vs create

Prefer register over create: registration is lazy, so the task is only configured if it is actually needed, improving configuration time.

Typed Task Classes

For reusable logic, define a class extending DefaultTask with an annotated action.

abstract class GreetTask : DefaultTask() {
    @TaskAction
    fun run() {
        println("Hi")
    }
}

Typed Properties

Use Gradle Property types for lazy, configurable inputs that integrate with the build model.

abstract class GreetTask : DefaultTask() {
    @get:Input
    abstract val name: Property<String>
}

Registering a Typed Task

Register the class and configure its properties in the Kotlin DSL with full type checking.

tasks.register<GreetTask>("greet") {
    name.set("Ada")
}

Inputs and Outputs

Annotate properties so Gradle can make the task incremental and cacheable.

@get:OutputFile
abstract val report: RegularFileProperty

Lazy Configuration

Lazy Provider values defer computation until needed, avoiding work during configuration and enabling correct task dependencies.

val versionProvider = providers.gradleProperty("appVersion")

Wiring Task Dependencies

Express order with dependsOn, or better, wire outputs of one task into the inputs of another so Gradle infers the order.

tasks.register("publishDocs") {
    dependsOn("generateDocs")
}

Reusing Across Modules

Move the task class into buildSrc or a convention plugin so every module can register it without duplication.

Kotlin DSL Advantages

Compared to Groovy, the Kotlin DSL gives:

  • IDE autocompletion and refactoring
  • Compile-time error detection
  • Easier navigation to task sources

Quick Check

Test your understanding of custom tasks.

Recap

You learned custom tasks in Kotlin DSL:

  • Use lazy register over create
  • Define typed tasks extending DefaultTask
  • Use Property and annotations for typed inputs/outputs
  • Reuse via buildSrc or convention plugins
เริ่มต้นได้ฟรี

เรียนรู้ Groovy ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การเขียนงานที่กำหนดเองด้วย Kotlin DSL” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเขียนงานที่กำหนดเองด้วย Kotlin DSL” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเขียนงานที่กำหนดเองด้วย Kotlin DSL”

สร้างงาน Gradle ที่กำหนดเองและนำกลับมาใช้ซ้ำได้อย่างปลอดภัยด้านชนิดข้อมูลด้วย Kotlin DSL พร้อมพร็อพเพอร์ตีแบบมีชนิด การกำหนดค่าแบบขี้เกียจ และรูปแบบการลงทะเบียนที่เป็นระเบียบ คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเขียนงานที่กำหนดเองด้วย Kotlin DSL” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม

ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Kotlin DSL สำหรับ Gradle
  2. การสร้างโครงการหลายภาษา
  3. การผสานรวม IDE และเครื่องมือ
  4. การเขียนงานที่กำหนดเองด้วย Kotlin DSL
← กลับไปที่ Groovy & Gradle: JVM Automation and Build Engineering