/dpeuni-maven-build-cache-deep-dive-outputs-overwrite-inputs

DPE University Lab: Learn how to handle cases where the outputs of one goal overwrite the inputs of a previous goal, causing build cache misses

Primary LanguageJava

DPE University Training

Checkout all the free Maven, Gradle, and DPE courses at the DPE University!

Maven Build Cache Deep Dive - Lab 05: Outputs Overwrite Inputs

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.

Prerequisites

  • Finished going through the Troubleshooting Build Cache Misses section in Maven Build Cache Deep Dive.
  • Java 11+

Steps

  1. 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.

  1. 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.

  2. Run the build multiple times:

    ./mvnw clean install
  3. 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 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.

  4. 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());
  5. Run the build multiple times:

    ./mvnw clean install

    After the first run the test goal will have a cache hit!

Solution Reference

To see the solution to the lab, check out the solution branch of this project.

More Free Labs & Courses

Be sure to check out our other free courses and labs!

Related courses:

Related labs: