Developer Productivity Engineering Blog

Speed up Apache Maven builds with a build cache

A build cache is one of the most effective tools for boosting productivity by reducing build times and shortening feedback cycles. However, many developers are unaware of what a build cache is and how it can help. This blog post will introduce you to a build cache and then walk you through using a build cache for Apache Maven projects.

What is a build cache?

A build cache is a mechanism that saves the output of a build step and reuses it in later builds. Instead of constantly rebuilding a whole project from scratch every time, the build cache recognizes previously executed goals with the same inputs and reuses their outputs. This reuse can significantly reduce build times, especially in large projects that contain a lot of computationally heavy work, such as compilation and tests. Therefore, a build cache can speed up local builds (e.g., builds on your laptop) and CI builds. For large development teams, the reduction in build time adds up quickly. For example, a top-three global investment bank used a build cache to cut their Maven build and test times by over 60%, giving 20,000 developers back two productive hours each day.

Using a build cache with Apache Maven

Apache Maven does not include a built-in build cache; however, you can add build caching to your project using a core extension. Maven extensions are similar to regular Maven plugins, but unlike plugins or “build extensions,” “core extensions” are loaded before your project’s pom file is read, which allows for the customization of Maven’s default behavior. Core extensions cannot be defined in your pom.xml file; they are commonly defined in a .mvn/extensions.xml file.

NOTE: There are currently two different Maven extensions you can use to enable build caching in your project: the Develocity Maven Extension and the Apache Maven Build Cache Extension. Both of these extensions work with Develocity, but this post will only cover the Develocity Maven Extension. 

You can use the Develocity Maven Extension for free to add local build caching and Build Scan to your project, whereas build caching for remote and CI builds is provided with a Develocity subscription. If you want us to write a similar blog post for the Apache Maven Build Cache Extension, message us via your favorite social channel!

Prerequisites

Setting up Develocity Build Cache for Maven

As mentioned above, to use a build cache with a Maven project, you must add an extension to your build. The Develocity Maven Extension provides a goal that will automate this for you in one command from your favorite terminal application. In your Maven project’s root directory, run:

mvn com.gradle:develocity-maven-extension:${ext-version}:init

Where ${ext-version} is the latest version of the Develocity Maven Extension. At the time of this writing, the latest version is 1.21.1, so the command would be:

mvn com.gradle:develocity-maven-extension:1.21.1:init

The above command adds the Develocity Maven Extention to your project, enabling build caching and publishing a Build Scan® for each build. By default, Build Scans will be published to scans.gradle.com. If you want to use a different Develocity server, add an additional argument to the command:

-Ddevelocity.url=https://your-develocity.example.com

See the documentation for more details.

NOTE: If you’re using your own Develocity server, you may also need to authenticate from your terminal run:

mvn com.gradle:develocity-maven-extension:${ext-version}:provision-access-key

That’s it—now just run your Maven build!

Run your Maven build

You’ll need to run your build twice to see the benefits of the build cache. The first build will populate the build cache, and future builds will benefit from it. Build cache entries are only created for “clean” builds, so make sure you add that to your Maven command. For example, for the Apache Commons Collections project, I’ll run:

mvn clean verify

This project takes about 27 seconds on my laptop. If I rerun the build, it will be much faster!

mvn clean verify

This time, the build took about 7 seconds—that’s roughly a 75% improvement!

Learn more about Develocity Build Cache

In my example above, the Apache Commons Collection build was already fast, but you probably would have lost interest if I’d used an example project that took an hour to build. However, larger multi-module Maven projects have the potential for even greater gains from using a build cache. This is because the chances of cache hits increase as work is commonly done in outer leaf modules.

We’ve only scratched the surface of build caching in this post. I didn’t cover topics like how your CI server can populate a remote build cache and speed up the build for your whole team, or what to do if you see cache misses. If you want to learn more about build caching with Maven, you can take our free Maven Build Cache course from DPE University.

Interested in more ways to speed up feedback cycle time by leveraging the latest tools and technologies? Check out these posts to learn more about Developer Productivity Engineering:

For more great content, follow us on Twitter and LinkedIn or subscribe to our YouTube channel.