Maven remote build cache setup with the Apache Build Cache Extension
+Share Maven build results across machines using Apache's Build Cache Extension and a self-hosted cache server. About ten minutes. The server side is built; its acceptance testing is still in progress, so treat Maven as newer than Gradle here.
+ +Who owns what
+The client is Apache's Maven Build Cache Extension. We maintain the server. If your hit rate looks low, that is usually the extension's own project settings (which files count as inputs, what to skip), not the server. Start with the extension's configuration guide.
+ +1. Add the extension
+In .mvn/extensions.xml (create it if it is missing):
<extensions>
+ <extension>
+ <groupId>org.apache.maven.extensions</groupId>
+ <artifactId>maven-build-cache-extension</artifactId>
+ <version>1.2.3</version>
+ </extension>
+</extensions>
+ Check Maven Central for the current version. Pin one rather than a range. The versions we test against are the ones in our Maven doc; a version outside that list is not something we claim to have tested.
+ +2. Point it at the server
+Create .mvn/maven-build-cache-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
+<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0">
+ <configuration>
+ <enabled>true</enabled>
+ </configuration>
+ <!-- FosterStack Cache — remote Maven build cache.
+ https://github.com/fosterstack/cache -->
+ <remote enabled="true" id="fosterstack-cache">
+ <url>https://cache.example.com/</url>
+ </remote>
+</cache>
+ Keep the comment: a cache setting that points at an unfamiliar host looks like data theft to a reviewer without it. Keep the trailing slash on the URL.
+ +3. Add the password, if the server has one
+In ~/.m2/settings.xml, or your CI system's equivalent. The id must match the id in step 2:
<settings>
+ <servers>
+ <server>
+ <id>fosterstack-cache</id>
+ <username>maven</username>
+ <password>your-password-or-CI-secret</password>
+ </server>
+ </servers>
+</settings>
+ If the server has no password set, skip this step. For laptops, the server's optional read-only login lets them read the cache without being able to write to it.
+ +4. Build, then build again
+mvn install
+ The first run fills the cache. A later run from a clean checkout, or after mvn clean install, should show cache hits in the log for modules that did not change. If not, check the id in step 3 matches, then read the extension's own settings.
Mixed Gradle and Maven shop? Both can use the same server. See Gradle and Maven setup. For CI runners, see Jenkins and CI build cache.
+ +