Checkout all the free Maven, Gradle, and DPE courses at the DPE University!
This is a hands-on exercise to go along with the Maven Build Cache Deep Dive training module. In this exercise you will go over the following:
- How to deal with cache misses caused by outputs that overwrite inputs of a previously run goal.
- Finished going through the Troubleshooting Build Cache Misses section in Maven Build Cache Deep Dive.
- Java 11+
- If you have not completed the previous labs, authenticate Maven with the Develocity server.
Note
As part of taking this free course, you have access to a training instance of Develocity located at:
https://dpeuniversity-develocity.gradle.com/
Sign in to this Develocity server using the same account you use for the DPE University.
This server is configured so users can only access the Build Scan® and Build Cache entries they publish.
Run the following command and follow the instructions in your terminal:
./mvnw com.gradle:develocity-maven-extension:provision-access-key
Note
For more ways to authenticate, see the authentication guide to see how to provide credentials.
-
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.This directory has already been added as an input, see the
pom.xml
. -
Run the build multiple times:
./mvnw clean install
-
Notice how the test goal was not cached (the tests run on every build). Open the Build Scan to see more details.
Open
HelloWorldTest.java
and look at thewriteToResourcesDir
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. -
Fix the test.
While it is possible to configure Develocity to ignore a whole file, 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).- // resolve a file in the data dir (data/timing.txt) - Path timingFile = Paths.get("data/timing.txt"); + // resolve a file relative to the `target` directory + Path timingFile = Paths.get("./target/test-output/timing.txt"); + Files.createDirectories(timingFile.getParent());
-
Run the build multiple times:
./mvnw clean install
After the first run the test goal will have a cache hit!
To see the solution to the lab, check out the solution
branch of this project.
Be sure to check out our other free courses and labs!
Related courses:
- Maven - Build Cache Deep Dive
- Maven - Maintaining an Optimized Build Cache
- Develocity - How to Use Build Scans
Related labs: