使用 Kotlin DSL 编写自定义任务
使用 Kotlin DSL 创建可复用、类型安全的自定义 Gradle 任务,结合类型化属性、惰性配置和清晰的注册模式。
使用 Kotlin DSL 编写自定义任务 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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: RegularFilePropertyLazy 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
registerovercreate - Define typed tasks extending
DefaultTask - Use
Propertyand annotations for typed inputs/outputs - Reuse via
buildSrcor convention plugins
常见问题解答
「使用 Kotlin DSL 编写自定义任务」课时是免费的吗?
是的 — 「使用 Kotlin DSL 编写自定义任务」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「使用 Kotlin DSL 编写自定义任务」这节课中我会学到什么?
使用 Kotlin DSL 创建可复用、类型安全的自定义 Gradle 任务,结合类型化属性、惰性配置和清晰的注册模式。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 Kotlin DSL 编写自定义任务」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Gradle 的 Kotlin DSL
- 多语言项目构建
- IDE 集成与工具
- 使用 Kotlin DSL 编写自定义任务