Escrever Tarefas Personalizadas em Kotlin DSL
Crie tarefas Gradle personalizadas, reutilizáveis e com segurança de tipos usando Kotlin DSL, com propriedades tipadas, configuração preguiçosa e padrões de registo claros.
Escrever Tarefas Personalizadas em Kotlin DSL é uma aula grátis de Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Groovy & Gradle: JVM Automation and Build Engineering, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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
Perguntas Frequentes
A aula “Escrever Tarefas Personalizadas em Kotlin DSL” é grátis?
Sim — o texto completo de “Escrever Tarefas Personalizadas em Kotlin DSL” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Groovy & Gradle: JVM Automation and Build Engineering, atualize para CoddyKit PRO. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.
O que vou aprender em “Escrever Tarefas Personalizadas em Kotlin DSL”?
Crie tarefas Gradle personalizadas, reutilizáveis e com segurança de tipos usando Kotlin DSL, com propriedades tipadas, configuração preguiçosa e padrões de registo claros. Você pratica Groovy & Gradle: JVM Automation and Build Engineering com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Groovy & Gradle: JVM Automation and Build Engineering?
Nenhuma experiência prévia é necessária. Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Escrever Tarefas Personalizadas em Kotlin DSL”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Groovy & Gradle: JVM Automation and Build Engineering?
Sim. Cada aula de Groovy & Gradle: JVM Automation and Build Engineering inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Kotlin DSL para Gradle
- Compilações de projetos poliglotas
- Integração com IDEs e ferramentas
- Escrever Tarefas Personalizadas em Kotlin DSL