Skip to content

gradle/dpeuni-maven-build-cache-deep-dive-caching-any-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DPE University Training

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

Maven Build Cache Deep Dive - Lab 03: Add Build Cache Support to any Maven Plugin

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 add build cache support to any Maven plugin.

Prerequisites

  • Finished going through the Caching for Other Plugins section in Maven Build Cache Deep Dive.
  • Java 11+

Steps

  1. View the pom.xml, pay attention to the exec-maven-plugin and the command executes a command similar to this:

    # Change directory to target/
    cd target/
    # read the contents of a file, assign it to shell variable `person`
    person=`cat ../name.txt`
    # write `hello $person` to the file `hello.txt`
    echo "hello $person" > hello.txt

    From the root directory, you can think of this script as having the input of name.txt and an output of target/hello.txt

  2. 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:

```shell
./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. Enable the build cache debug logging to view additional details:

    export MAVEN_OPTS="-Dorg.slf4j.simpleLogger.log.develocity.goal.cache=debug"
  2. Execute the build:

     ./mvnw clean verify
  3. Create a pluginManagement section in the pom.xml.

Note

The free build cache training course walks through the following steps in detail.

  1. Add the develocity-maven-extension to pluginManagement.

  2. Add a configuration entry for the exec-maven-plugin.

  3. Set a value for the cacheableBecause argument.

  4. Add the name.txt file as an input. This is similar to what you did in the missing inputs lab.

  5. Define the hello.txt file as an output.

  6. Run the build:

./mvnw clean verify
  1. Scroll back to the exec-maven-plugin in the console output, and note the log message, stating caching was not enabled for this goal (this message can also be viewed in the Build Scan).

  2. Add the missing parameters as inputs.

Note

If you are familiar with the plugin, you can walk through configuring each argument, adding or ignoring each one. In this lab we will iteratively run the build do demonstrate the types of error you may see when enabling build caching for other plugins.

  1. Run the build:
./mvnw clean verify
  1. Once again, scroll back in the console or view the Build Scan to see the reason why the build was not cached.

  2. Configure the extension to ignore the file parameters (workingDirectory, buildDirectory, and basedir) as they are not used in this use case (e.g. we are explicitly configuring each of the input and output files).

  3. Run the build:

./mvnw clean verify
  1. Again, scroll back in the console (or view the Build Scan). Note that one of the input types is unsupported. For this use case, the project, pluginDependencies, and session, can all be ignored (as we are concerned with only the script execution).

  2. Configure the skip property. Remove the skip property as an input, and define a skipIfTrue section in the configuration.

  3. Run the build multiple times:

./mvnw clean verify

The goal is now cachable! Open the Build Scan to view its details.

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: