{"id":15147335,"url":"https://github.com/gradle/dpeuni-gradle-build-cache-deep-dive-runtime-api","last_synced_at":"2025-10-19T20:31:16.516Z","repository":{"id":236924286,"uuid":"793408816","full_name":"gradle/dpeuni-gradle-build-cache-deep-dive-runtime-api","owner":"gradle","description":"Hands-on exercise for DPE University","archived":false,"fork":false,"pushed_at":"2024-06-14T18:37:00.000Z","size":64,"stargazers_count":2,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-30T03:51:11.805Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gradle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-29T07:02:55.000Z","updated_at":"2024-08-24T10:34:25.000Z","dependencies_parsed_at":"2024-04-29T09:28:21.446Z","dependency_job_id":"85dc8a4e-fb75-45e8-b25a-41a89639e2fd","html_url":"https://github.com/gradle/dpeuni-gradle-build-cache-deep-dive-runtime-api","commit_stats":null,"previous_names":["gradle/dpeuni-gradle-build-cache-deep-dive-runtime-api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gradle","download_url":"https://codeload.github.com/gradle/dpeuni-gradle-build-cache-deep-dive-runtime-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237209745,"owners_count":19272497,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-26T12:40:51.802Z","updated_at":"2025-10-19T20:31:16.222Z","avatar_url":"https://github.com/gradle.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DPE University Training\n\n\u003cp align=\"left\"\u003e\n\u003cimg width=\"10%\" height=\"10%\" src=\"https://user-images.githubusercontent.com/120980/174325546-8558160b-7f16-42cb-af0f-511849f22ebc.png\"\u003e\n\u003c/p\u003e\n\n## Gradle Build Cache Deep Dive - Runtime API Exercise\n\nThis is a hands-on exercise to go along with the\n[Gradle Build Cache Deep Dive](https://dpeuniversity.gradle.com/app/courses/54469478-55ba-416d-9cef-3b5aa33dd2a5)\ntraining module. In this exercise you will go over the following:\n\n* Practice troubleshooting cache issues due to missing input and output declaration\n* Get familiar with using the build cache runtime API\n\n---\n## Prerequisites\n\n* Finished going through the build cache runtime API section in [Gradle Build Cache Deep Dive](https://dpeuniversity.gradle.com/app/courses/54469478-55ba-416d-9cef-3b5aa33dd2a5)\n\n---\n## Develocity Authentication\n\nIf you haven't already done so, you can authenticate with the training Develocity service by running:\n\n```shell\n./gradlew provisionGradleEnterpriseAccessKey\n```\n\nThe output of the task will indicate a browser window will come up from which you can complete the authentication:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/ccafa270-dbab-4c66-ba12-caabcd10399c\"\u003e\n\u003c/p\u003e\n\nOnce the browser window comes up you can enter a title for the access key that will be created or go with the suggested title:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/1aeef46a-2fb6-472a-8d87-82af31b20799\"\u003e\n\u003c/p\u003e\n\nOnce confirmed you will see the following message and you can close the browser window and return to the editor:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/1711c9db-814c-4df1-9d18-42fe5d1b82f8\"\u003e\n\u003c/p\u003e\n\n---\n## Scenario\n\nIn `app/build.gradle.kts` there is a task called `countSrc`, which counts the\nnumber of source files and puts the number in `app/build/src-stats/num-files.txt`.\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/6c80f0b5-3f77-4637-bcf6-59fc1ebda667\"\u003e\n\u003c/p\u003e\n\nThe task makes use of the `CountSrcFiles` task type which is in `buildSrc/src/main/kotlin/CountSrcFiles.kt`. For this exercise we will assume the task type is provided by a third-party plugin and we cannot modify the source.\n\nBoth incremental build and build caching does not work with the `countSrc` task. You will debug why and use the build cache runtime API to address this.\n\n---\n## Steps\n\n1. Open the Gradle project in this repository in an editor of your choice\n2. Run `./gradlew :app:countSrc` task. You will notice the file `app/build/src-stats/num-files.txt` gets created which contains the count:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/06f0e972-c5fd-41c0-819b-912d3461cee6\"\u003e\n\u003c/p\u003e\n\n3. Run the task again. You will notice the task is not `UP-TO-DATE`:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/9c570ddd-98a8-4c0c-8e26-d3da502c46e4\"\u003e\n\u003c/p\u003e\n\n4. Create a build scan, `./gradlew :app:countSrc --scan` , and look at the details in the timeline. Notice the message that indicates that no outputs were declared:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/ec165a2a-a513-45f8-80c1-981685fd36e4\"\u003e\n\u003c/p\u003e\n\n5. Open the task type in `buildSrc/src/main/kotlin/CountSrcFiles.kt`. We can see from the task type implementation, as well as the output observed earlier, we need to declare `app/build/src-stats` as an output directory. We could also be more specific and declare `app/build/src-stats/num-files.txt`, however in the future the task type may create additional files, so it's more future-proof to specify the directory.\n\n6. Use the [build cache runtime API documentation](https://docs.gradle.org/current/userguide/build_cache.html#using_the_runtime_api) to declare the output directory, call the property `outputDir`.\n\n7. Run the task twice, it should now be `UP-TO-DATE`:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/4b3369e7-fdd3-4f41-b83b-d01ff578a59e\"\u003e\n\u003c/p\u003e\n\n8. Now let's check if the task is working with the build cache. Do a clean, then run the task. We expect the `FROM-CACHE` outcome label, but instead there is no outcome label:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/9c570ddd-98a8-4c0c-8e26-d3da502c46e4\"\u003e\n\u003c/p\u003e\n\n9. Create a build scan and look at the task details in the timeline. Notice the message that indicates that build caching is not enabled for the task:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/fa38169c-275b-4cc9-a42e-3899745f5785\"\u003e\n\u003c/p\u003e\n\n10. Use the [build cache runtime API documentation](https://docs.gradle.org/current/userguide/build_cache.html#using_the_runtime_api) to enable build caching for the task.\n\n11. Now do a clean, run the task. This should populate the cache. Do another clean and run the task again. You should see the `FROM-CACHE` outcome label:\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/fc192fd7-bdda-411f-a86b-cc3b0a42cd14\"\u003e\n\u003c/p\u003e\n\n12. Now let's check the caching behavior. Do a clean first.\n\n13. Now add a new file under `app/src` anywhere. There are now 3 files under `app/src`, one `App.java`, one `AppTest.java`, and the new file.\n\n14. Run the task. Check the `app/build/src-stats/num-files.txt` file, you will see the value `2` still. Also there should have been no outcome label as the action should have executed, however we see the `FROM-CACHE` outcome label.\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"75%\" height=\"75%\" src=\"https://github.com/gradle/build-tool-training-exercises/assets/120980/fc192fd7-bdda-411f-a86b-cc3b0a42cd14\"\u003e\n\u003c/p\u003e\n\n15. We need to declare the `app/src` directory as an input directory. Refer to the documentation to do this. **Don't forget to specify the path sensitivity**.\n\n16. Now do a clean and run the task. There should be no outcome label and the value in the output file should be correct.\n\n---\n## Solution Reference\n\nIf you get stuck you can refer to the `solution` branch of this repository.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgradle%2Fdpeuni-gradle-build-cache-deep-dive-runtime-api/lists"}