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

ปลั๊กอินตามแบบแผนและตรรกะการสร้าง

พัฒนาและใช้ปลั๊กอินตามแบบแผน เพื่อบังคับใช้การกำหนดค่าและมาตรฐานการสร้างที่สอดคล้องกันทั่วทั้งองค์กร

บทเรียน 3 จาก 411 ขั้นตอน

ปลั๊กอินตามแบบแผนและตรรกะการสร้าง เป็นบทเรียน 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 Convention Plugins?

In large Gradle multi-project builds, maintaining consistent configurations across many subprojects can become a challenge. This is where Convention Plugins come in handy.

A convention plugin is essentially a way to package and reuse common build logic and configurations specific to your organization or project. They help enforce standards and reduce boilerplate.

The Problem: Inconsistent Build Logic

Imagine you have several Java subprojects. Each needs to apply the java-library plugin, set a specific Java toolchain, and configure common repositories. Without convention plugins, you might end up with repetitive and slightly different configurations in each build.gradle file.

/* project-a/build.gradle */
plugins {
    id 'java-library'
}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
repositories {
    mavenCentral()
}

/* project-b/build.gradle */
plugins {
    id 'java-library'
}
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}
repositories {
    mavenCentral()
}

Centralizing Build Logic with `buildSrc`

To solve the problem of repetition, Gradle offers a special directory: buildSrc. When present in your project root, Gradle automatically compiles any code (Groovy, Kotlin, Java) found within buildSrc and adds it to the classpath of your build scripts.

This makes buildSrc the perfect place to define internal build logic, custom tasks, and especially, convention plugins.

Crafting Your First Convention Plugin

Let's create a simple convention plugin to standardize our Java library setup. We'll define it in buildSrc/src/main/groovy/my.java-conventions.groovy. This plugin will apply the java-library plugin, set Java 17 as the toolchain, and add mavenCentral().

// buildSrc/src/main/groovy/my.java-conventions.groovy
plugins {
    id 'java-library'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

Applying Your Convention Plugin

Once you've defined your convention plugin in buildSrc, you can apply it to any subproject's build.gradle file using its ID. Notice how much cleaner and more concise the subproject's build file becomes!

// subproject-a/build.gradle
plugins {
    id 'my.java-conventions'
}

// All Java library, Java 17, and Maven Central settings
// are now applied automatically by the convention plugin!

Expanding Conventions: Dependencies & Tests

Convention plugins can do much more than just basic settings. You can enforce common dependencies, configure testing frameworks, or standardize other build aspects. Here, we add JUnit Jupiter dependencies and a common test logging configuration.

// buildSrc/src/main/groovy/my.java-conventions.groovy (updated)
plugins {
    id 'java-library'
}
// ... (previous Java toolchain and repositories config)

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

Convention Plugins with Kotlin DSL

Just like regular build scripts, convention plugins can also be written using the Kotlin DSL (.gradle.kts files). This provides compile-time safety and better IDE support, which can be very valuable for complex build logic.

The structure remains similar, just with Kotlin syntax.

// buildSrc/src/main/kotlin/my.kotlin-conventions.gradle.kts
plugins {
    java
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

Applying to the Root Project

While convention plugins are often used for subprojects, they can also be applied to the root project's build.gradle. This is useful for defining build-wide properties or configurations that should apply to the entire project, like common dependency versions.

// build.gradle (root project)
plugins {
    id 'my.root-conventions'
}

// buildSrc/src/main/groovy/my.root-conventions.groovy
ext {
    // Define properties accessible throughout the build
    springBootVersion = '3.2.0'
    kotlinVersion = '1.9.0'
}

Benefits and Best Practices

Convention plugins are a cornerstone of maintainable, large-scale Gradle builds:

  • Consistency: Ensures all projects adhere to organizational standards.
  • Reduced Duplication: Centralizes common build logic, eliminating copy-pasting.
  • Easier Maintenance: Update standards in one place; all projects instantly benefit.
  • Improved Readability: Subproject build files become shorter and more focused on project-specific details.

When creating them, keep plugins focused on a single concern, use clear IDs, and leverage Kotlin DSL for type safety.

Convention Checkpoint

Convention plugins are a powerful way to standardize build logic and enforce organizational standards. Let's test your understanding.

Recap: Mastering Convention Plugins

You've now learned about Gradle Convention Plugins! They are an essential tool for managing complex, multi-project builds.

  • Convention plugins centralize common build logic.
  • They are typically defined in the buildSrc directory.
  • They help enforce consistent configurations (Java versions, dependencies, test setups).
  • They drastically reduce boilerplate and improve maintainability.

By leveraging convention plugins, you can build more robust and scalable Gradle projects.

เริ่มต้นได้ฟรี

เรียนรู้ 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 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ปลั๊กอินตามแบบแผนและตรรกะการสร้าง”

พัฒนาและใช้ปลั๊กอินตามแบบแผน เพื่อบังคับใช้การกำหนดค่าและมาตรฐานการสร้างที่สอดคล้องกันทั่วทั้งองค์กร คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การสแกนการสร้างและข้อมูลเชิงลึกของ Gradle
  2. การจัดการความปลอดภัยและข้อมูลรับรอง
  3. ปลั๊กอินตามแบบแผนและตรรกะการสร้าง
  4. แค็ตตาล็อกรุ่นส่วนพึ่งพาและแพลตฟอร์ม
← กลับไปที่ Groovy & Gradle: JVM Automation and Build Engineering