dpeuni-gradle-remote-caching

Hands-on exercise for DPE University

OTHER License

Stars
4
Committers
2

DPE University Training

Gradle Remote Caching Exercise

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

Prerequisites

  • Finished going through the relevant sections in the training course
  • Completed the incremental build and local caching exercise

Develocity Authentication

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:


Enable Remote Cache

  1. Edit the gradle.properties file and add org.gradle.caching=true to it. The
    contents of the file now look like:
org.gradle.console=verbose
org.gradle.caching=true
  1. Open the settings.gradle.kts file. Notice the com.gradle.enterprise plugin applied and the gradleEnterprise 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
        }
    }
}
  1. In the settings.gradle.kts file add a buildCache configuration which disables the local cache and uses the gradleEnterprise configuration for the remote cache:
buildCache {
    local {
        isEnabled = false
    }
    remote(gradleEnterprise.buildCache) {
        isEnabled = true
        isPush = true
    }
}
  1. 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
  1. 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
  1. Open the build scan, go to the Timeline (in the left menu) and expand the
    compileJava task and inspect the cache details.
  1. 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
  1. 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.
  1. Select your two recent scans to compare and click on the "Compare" button on
    the bottom right.
  1. Expand the file properties to see which files were different in the inputs
    which caused the cache miss.

Solution Reference

If you get stuck you can refer to the solution branch of this repository.

Related Projects