约定插件与构建逻辑
开发并应用约定插件,在整个组织内强制执行一致的构建配置和标准。
约定插件与构建逻辑 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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
buildSrcdirectory. - 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.
常见问题解答
「约定插件与构建逻辑」课时是免费的吗?
是的 — 「约定插件与构建逻辑」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「约定插件与构建逻辑」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Gradle 构建扫描与洞察
- 安全与凭据管理
- 约定插件与构建逻辑
- 依赖版本目录与平台