mavenmaven-3

Customize maven build cache configuration for submodule


Is there a way to overwrite the maven build cache configuration for a submodule in a multi-module project? If we assume a project with structure

then the build cache configuration would normally reside in the .mvn directory of the main directory (say ~/cool-software-main/.mvn/maven-build-cache-config.xml). Now, one submodule might need to have a different configuration for the build cache extension than the others. Is there a way to modify the configuration just for cool-software-module-A ? Simply placing a maven-build-cache-config.xml into a .mvn directory in the submodule does not seem to work.


Solution

  • As it turns out, yes some configurations, crucially including which files to consider as input for a module's hash function, can be overwritten by adding specifically named properties in a module's pom file.

    This is documented (imho a bit hidden since not mentioned in the main Usage tutorials and under command line parameters) on the main documentation pages under the Parameters section: https://maven.apache.org/extensions/maven-build-cache-extension/parameters.html#project-level-properties

    In essence

    <maven.build.cache.exclude.value.1>src/main/java/package-info.java<maven.build.cache.exclude.value.1>
    

    would exclude the src/main/java/package-info.java file from consideration for the hash. Note that the name of the property is fixed, you cannot change any part of it. You can have multiple such properties by increasing the number at the end, but they all need to follow this exact naming pattern. You can combine path exclusions with glob restrictions by using the same number in the property name, e.g.

    <maven.build.cache.exclude.value.2>src/main/resources</maven.build.cache.exclude.value.2>
    <maven.build.cache.exclude.glob.2>*.md</maven.build.cache.exclude.glob.2>
    

    results in an exclusion rule like this

     <exclude glob="*.md">src/main/resources</exclude>
    

    because both properties are tied together via the 2 in their name.