Cache de compilação e Daemon
Aproveite o Gradle Build Cache e o Daemon para acelerar significativamente as compilações incrementais.
Cache de compilação e Daemon é uma aula grátis de Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit. Esta é a aula 1 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.
Boost Your Gradle Builds
Ever waited too long for a Gradle build to finish? In this lesson, we'll unlock two powerful features to speed up your development:
- Gradle Daemon: Keeps a JVM running in the background.
- Build Cache: Reuses outputs from previous builds.
These tools are essential for making your incremental builds much faster!
Meet the Gradle Daemon
The Gradle Daemon is a long-lived background process that hosts your Gradle builds. Think of it like a dedicated server for your builds!
- It eliminates the JVM startup overhead for each build.
- It keeps project information and caches in memory.
- This means subsequent builds are significantly faster!
Daemon in Action: First Run
Let's see the Daemon's effect. The first time you run a Gradle command, the Daemon might need to start up, taking a bit longer. Copy this into a build.gradle file:
task greet {
doLast {
println "Hello from Gradle!"
}
}Daemon in Action: Subsequent Runs
Now, run the task again using gradle greet. Notice how much faster it is? That's the Daemon at work!
You can check if the Daemon is running with gradle --status and stop it with gradle --stop.
Understanding the Build Cache
The Gradle Build Cache stores the outputs of tasks that have been executed. If a task's inputs haven't changed, Gradle can reuse its cached output instead of re-running it.
- It saves time by avoiding redundant work.
- Works across different machines if a remote cache is configured.
- Enabled by default for local caching.
Local Build Cache Explained
The local build cache is stored on your machine, typically in your Gradle user home directory (~/.gradle/caches/build-cache).
When a task runs, Gradle calculates a hash of its inputs. If that hash matches an entry in the cache, the cached output is used. If not, the task runs and its output is stored.
Cacheable Tasks
For a task to be cacheable, it must properly declare its inputs and outputs. Gradle needs to know what goes into a task and what comes out to determine if it can be cached.
- Inputs: Files, properties, directories used by the task.
- Outputs: Files or directories produced by the task.
Many built-in Gradle tasks (like compileJava) are cacheable by default!
Build Cache in Practice
Let's create a task that generates a file. Run gradle generateReport once, then run it again. Notice FROM-CACHE or UP-TO-DATE in the output!
task generateReport {
inputs.property 'version', '1.0'
outputs.file 'build/report.txt'
doLast {
def reportFile = file('build/report.txt')
reportFile.parentFile.mkdirs()
reportFile.text = "Report Version: ${inputs.properties.version}"
println "Generated report.txt"
}
}When Cache Isn't Used
Sometimes, the build cache might not be used, leading to a "cache miss". Common reasons include:
- Input Changes: Any change to task inputs (even a timestamp).
- Task Not Cacheable: Task doesn't properly declare inputs/outputs.
- Cache Disabled: Explicitly disabled with
--no-build-cache. - Local Cache Cleaned: Cache entries removed.
Quick Check: Build Speed
What is the primary benefit of the Gradle Daemon?
Recap: Faster Builds Ahead!
You've learned how the Gradle Daemon and Build Cache significantly improve build performance:
- The Daemon keeps a JVM running, making subsequent builds start faster.
- The Build Cache reuses previous task outputs, avoiding redundant work.
By leveraging these, you can enjoy much quicker incremental builds and a smoother development workflow. Next, we'll explore profiling and debugging your builds!
Perguntas Frequentes
A aula “Cache de compilação e Daemon” é grátis?
Sim — o texto completo de “Cache de compilação e Daemon” é 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 “Cache de compilação e Daemon”?
Aproveite o Gradle Build Cache e o Daemon para acelerar significativamente as compilações incrementais. 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 1 de 4.
Quanto tempo leva a aula “Cache de compilação e Daemon”?
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
- Cache de compilação e Daemon
- Analisando e depurando compilações
- Execução paralela e configuração
- Compilações Incrementais e Entradas e Saídas de Tarefas