|
| 1 | +# DPE University Training |
| 2 | + |
| 3 | +[<img width="10%" height="10%" src="https://user-images.githubusercontent.com/120980/174325546-8558160b-7f16-42cb-af0f-511849f22ebc.png">](https://dpeuniversity.gradle.com/) |
| 4 | +Checkout all the **free** Maven, Gradle, and DPE courses at the [DPE University][dpe-university]! |
| 5 | + |
| 6 | +## Maven Build Cache Deep Dive - Lab 05: Outputs Overwrite Inputs |
| 7 | + |
| 8 | +This is a hands-on exercise to go along with the [Maven Build Cache Deep Dive][course-url] training module. In this exercise you will go over the following: |
| 9 | + |
| 10 | +- How to deal with cache misses caused by outputs that overwrite inputs of a previously run goal. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Finished going through the _Troubleshooting Build Cache Misses_ section in Maven Build Cache Deep Dive. |
| 15 | +- Java 11+ |
| 16 | + |
| 17 | +Steps |
| 18 | +----- |
| 19 | + |
| 20 | +1. If you have not completed the previous labs, authenticate Maven with the Develocity server. |
| 21 | + |
| 22 | +> [!NOTE] |
| 23 | +> As part of taking this **free** course, you have access to a training instance of Develocity located at: |
| 24 | +> ``` |
| 25 | +> https://dpeuniversity-develocity.gradle.com/ |
| 26 | +> ``` |
| 27 | +> [Sign in][develocity-url] to this Develocity server using the same account you use for the DPE University. |
| 28 | +> |
| 29 | +> This server is configured so users can only access the Build Scan® and Build Cache entries they publish. |
| 30 | +
|
| 31 | +Run the following command and follow the instructions in your terminal: |
| 32 | +
|
| 33 | + ```shell |
| 34 | + ./mvnw com.gradle:develocity-maven-extension:provision-access-key |
| 35 | + ``` |
| 36 | +> [!NOTE] |
| 37 | +> For more ways to authenticate, see the [authentication guide](https://docs.gradle.com/enterprise/maven-extension/#authenticating_with_gradle_enterprise) to see how to provide credentials. |
| 38 | +
|
| 39 | +2. In this example the `data` directory represents content used by the build that _may_ be in another repository, a external application, or other resources needed by the build. |
| 40 | + |
| 41 | + This directory has already been added as an input, see the `pom.xml`. |
| 42 | + |
| 43 | +3. Run the build **multiple times**: |
| 44 | + |
| 45 | + ```shell |
| 46 | + ./mvnw clean install |
| 47 | + ``` |
| 48 | + |
| 49 | +4. Notice how the test goal was not cached (the tests run on every build). Open the Build Scan to see more details. |
| 50 | + |
| 51 | + Open `HelloWorldTest.java` and look at the `writeToResourcesDir` test method, this test writes to a directory that is used as an input. This changed input will cause a cache miss on the next build. |
| 52 | + |
| 53 | +5. Fix the test. |
| 54 | + |
| 55 | + While it is possible to configure Develocity to [ignore a whole file](https://docs.gradle.com/enterprise/maven-extension/#ignoring_arbitrary_files), when possible it's best to fix the cause of the problem. In this case, update the test to write to a different location (a temp directory or the `target/` dir). |
| 56 | + |
| 57 | + ```diff |
| 58 | + - // resolve a file in the data dir (data/timing.txt) |
| 59 | + - Path timingFile = Paths.get("data/timing.txt"); |
| 60 | + + // resolve a file relative to the `target` directory |
| 61 | + + Path timingFile = Paths.get("./target/test-output/timing.txt"); |
| 62 | + + Files.createDirectories(timingFile.getParent()); |
| 63 | + ``` |
| 64 | +6. Run the build **multiple times**: |
| 65 | + |
| 66 | + ```shell |
| 67 | + ./mvnw clean install |
| 68 | + ``` |
| 69 | + |
| 70 | + After the first run the test goal will have a cache hit! |
| 71 | + |
| 72 | +## Solution Reference |
| 73 | + |
| 74 | +To see the solution to the lab, check out the [`solution`](https://github.com/gradle/outputs-overwrite-inputs-maven-build-cache-lab/commit/solution) branch of this project. |
| 75 | + |
| 76 | +## More Free Labs & Courses |
| 77 | + |
| 78 | +Be sure to check out our other **free** [courses][dpe-university] and [labs](https://github.com/gradle?q=dpe-university)! |
| 79 | + |
| 80 | +**Related courses:** |
| 81 | +- [Maven - Build Cache Deep Dive][course-url] |
| 82 | +- [Maven - Maintaining an Optimized Build Cache](https://dpeuniversity.gradle.com/c/42cf9d626302011526c4a0536b26af929b5bef58) |
| 83 | +- [Develocity - How to Use Build Scans](https://dpeuniversity.gradle.com/c/0b0b3e4a8d21709ff39074e9962eee6ca4276dc1) |
| 84 | + |
| 85 | +**Related labs:** |
| 86 | +- [Lab 01 - Using the local build cache](https://github.com/gradle/getting-started-maven-build-cache-lab) |
| 87 | +- [Lab 02 - Missing Inputs With Build Caching](https://github.com/gradle/missing-inputs-maven-build-cache-lab) |
| 88 | +- [Lab 03 - Add Build Cache Support to any Maven Plugin](https://github.com/gradle/caching-any-plugin-maven-build-cache-lab) |
| 89 | +- [Lab 04 - Handling Cache Misses with Normalization](https://github.com/gradle/cache-misses-maven-build-cache-lab) |
| 90 | +- [Lab 05 - Outputs Overwrite Inputs](https://github.com/gradle/outputs-overwrite-inputs-maven-build-cache-lab) |
| 91 | +- [Lab 06 - Maintaining an Optimized Build Cache](https://github.com/gradle/maintaining-optimized-cache-maven-build-cache-lab) |
| 92 | + |
| 93 | +[course-url]: https://dpeuniversity.gradle.com/c/47262fea1e74b719afb590d8cb3f8280bf2af732 |
| 94 | +[dpe-university]: https://dpeuniversity.gradle.com/ |
| 95 | +[develocity-url]: https://dpeuniversity-develocity.gradle.com/ |
0 commit comments