Overview

Cache configuration provides you additional control over the build-cache extension behavior. Follow it step-by-step to understand how it works, and figure out an optimal config

Minimal config

Minimal config

<?xml version="1.0" encoding="UTF-8" ?>
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0 https://maven.apache.org/xsd/build-cache-config-1.0.0.xsd">

    <configuration>
        <enabled>true</enabled>
        <hashAlgorithm>XX</hashAlgorithm>
    </configuration>

    <input>
        <global>
            <glob>{*.java,*.xml,*.properties}</glob>
        </global>
    </input>
</cache>

Enabling remote cache

Just add <remote> section under <configuration>

    <configuration>
        <enabled>true</enabled>
        <hashAlgorithm>XX</hashAlgorithm>
        <remote>
            <url>https://yourserver:port</url>
        </remote>
    </configuration>

Adding more file types to input

Add all the project-specific source code files in <glob>. Scala, in this case:

    <input>
        <global>
            <glob>{*.java,*.xml,*.properties,*.scala}</glob>
        </global>
    </input>

Adding source directory for bespoke project layouts

In most cases, the build-cache extension automatically recognizes directories by introspecting the build. When it is not enough, adding additional directories with <include> is possible. Also, you can filter out undesirable dirs and files by using exclude tag.

    <input>
        <global>
            <glob>{*.java,*.xml,*.properties,*.scala}</glob>
            <includes>
                <include>importantdir/</include>
            </includes>
            <excludes>
                <exclude>tempfile.out</exclude>
            </excludes>
        </global>
    </input>

Plugin property is environment-specific and yields different cache keys in different environments

Consider to exclude env specific properties:

    <input>
        <global>
            ...
        </global>
        <plugins>
            <plugin artifactId="maven-surefire-plugin">
                <effectivePom>
                    <excludeProperties>
                        <excludeProperty>argLine</excludeProperty>
                    </excludeProperties>
                </effectivePom>
            </plugin>
        </plugins>
    </input>

Implications - builds with different argLine will have an identical key. Validate that it is acceptable in terms of artifact equivalency.

Plugin property points to a directory where only a subset of files is relevant

If the plugin configuration property points to somedir, it will be scanned with the default glob. You can tweak it with custom processing rule

    <input>
        <global>
            ...
        </global>
        <plugins>
            <plugin artifactId="protoc-maven-plugin">
                <dirScan mode="auto">
                    <!--<protoBaseDirectory>${basedir}/..</protoBaseDirectory>-->
                    <tagScanConfigs>
                        <tagScanConfig tagName="protoBaseDirectory" recursive="false" glob="{*.proto}"/>
                    </tagScanConfigs>
                </dirScan>
            </plugin>
        </plugins>
    </input>

Local repository is not updated because the install phase is cached

Add executionControl/runAlways section:

    <executionControl>
        <runAlways>
            <plugins>
                <plugin artifactId="maven-failsafe-plugin"/>
            </plugins>
            <executions>
                <execution artifactId="maven-dependency-plugin">
                    <execIds>
                        <execId>unpack-autoupdate</execId>
                    </execIds>
                </execution>
            </executions>
            <goalsLists>
                <goalsList artifactId="maven-install-plugin">
                    <goals>
                        <goal>install</goal>
                    </goals>
                </goalsList>
            </goalsLists>
        </runAlways>
    </executionControl>

I occasionally cached build with -DskipTests=true, and tests do not run now

If you add command line flags to your build, they do not participate in effective pom - Maven defers the final value resolution to plugin runtime. To invalidate the build if the filed value is different in runtime, add a reconciliation section to executionControl:

<?xml version="1.0" encoding="UTF-8" ?>
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0 https://maven.apache.org/xsd/build-cache-config-1.0.0.xsd">
    <configuration>
        ...
    </configuration>
    <executionControl>
        <runAlways>
            ...
        </runAlways>
        <reconcile>
            <plugins>
                <plugin artifactId="maven-surefire-plugin" goal="test">
                    <reconciles>
                        <reconcile propertyName="skip" skipValue="true"/>
                        <reconcile propertyName="skipExec" skipValue="true"/>
                        <reconcile propertyName="skipTests" skipValue="true"/>
                        <reconcile propertyName="testFailureIgnore" skipValue="true"/>
                    </reconciles>
                </plugin>
            </plugins>
        </reconcile>
    </executionControl>
</cache>

Please notice the skipValue attribute. It captures the value that makes the plugin skip execution. Think of skipProperty as follows: if the build started with -DskipTests=true, restoring results from a build with completed tests is safe because the local run does not require completed tests. The same logic applies to any other plugin, not just Surefire.

How to renormalize line endings in working copy after committing .gitattributes (git 2.16+)

Ensure you've committed (and ideally pushed everything) - no changes in the working copy. After that:

# Rewrite objects and update the index
git add --renormalize .
# Commit changes
git commit -m "Normalizing line endings."
# Remove working copy paths from the git cache
git rm --cached -r .
# Refresh with new line endings
git reset --hard

I want to cache the interim build and override it later with the final version

Solution: set -Dmaven.build.cache.remote.save.final=true to nodes that produce final builds. Such builds will not be overridden and eventually will replace all interim builds