This is a hands-on exercise to go along with the Incremental Builds and Build Caching training module. In this exercise you will go over the following:
- Enable and use remote caching
- Compare build scans to identify cause of cache misses
- Finished going through the relevant sections in the training course
- Completed the incremental build and local caching exercise
We will use the DPE University Develocity instance as the remote cache. If you haven't already done so, you can authenticate with the Develocity service by running:
./gradlew provisionGradleEnterpriseAccessKey
The output of the task will indicate a browser window will come up from which you can complete the authentication:
Once the browser window comes up you can enter a title for the access key that will be created or go with the suggested title:
Once confirmed you will see the following message and you can close the browser window and return to the editor:
- Edit the
gradle.properties
file and addorg.gradle.caching=true
to it. The contents of the file now look like:
org.gradle.console=verbose
org.gradle.caching=true
- Open the
settings.gradle.kts
file. Notice thecom.gradle.enterprise
plugin applied and thegradleEnterprise
configuration:
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
id("com.gradle.enterprise") version "3.16.2"
}
gradleEnterprise {
server = "https://dpeuniversity-develocity.gradle.com"
buildScan {
capture {
isTaskInputFiles = true
}
}
}
- In the
settings.gradle.kts
file add abuildCache
configuration which disables the local cache and uses thegradleEnterprise
configuration for the remote cache:
buildCache {
local {
isEnabled = false
}
remote(gradleEnterprise.buildCache) {
isEnabled = true
isPush = true
}
}
- Now run a clean followed by the tests. We will see no task output was fetched from the remote cache.
$ ./gradlew :app:clean :app:test
# Task :app:clean
# Task :app:generateLocalUniqueValue UP-TO-DATE
- Task :app:compileJava
# Task :app:processResources NO-SOURCE
- Task :app:classes
- Task :app:compileTestJava
# Task :app:processTestResources NO-SOURCE
- Task :app:testClasses
- Task :app:test
- Now run the clean and tests again and you will see the remote cache being used.
Also pass the
--scan
flag which will generate a build scan that we can explore.
$ ./gradlew :app:clean :app:test --scan
# Task :app:clean
# Task :app:generateLocalUniqueValue UP-TO-DATE
! Task :app:compileJava FROM-CACHE
# Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
! Task :app:compileTestJava FROM-CACHE
# Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
! Task :app:test FROM-CACHE
- Open the build scan, go to the Timeline (in the left menu) and expand the compileJava task and inspect the cache details.
- Now edit the string in
app/src/main/java/com/gradle/lab/App.java
to something different and run the clean and tests with the--scan
flag.
$ ./gradlew :app:clean :app:test --scan
# Task :app:clean
> Task :app:generateLocalUniqueValue UP-TO-DATE
- Task :app:compileJava
# Task :app:processResources NO-SOURCE
- Task :app:classes
! Task :app:compileTestJava FROM-CACHE
# Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
- Task :app:test
- You can see the cache misses. Now open the build scan, and in the top right click on the "Build Scans" link to view all the scans.
- Select your two recent scans to compare and click on the "Compare" button on the bottom right.
- Expand the file properties to see which files were different in the inputs which caused the cache miss.
If you get stuck you can refer to the solution
branch of this repository.