maven-build-cache-extension icon indicating copy to clipboard operation
maven-build-cache-extension copied to clipboard

Maven Build Cache not used when executing an additional plugin goal

Open breun opened this issue 2 months ago • 3 comments

Affected version

1.2.0

Bug description

The Maven Build Cache Extension works when lifecycle phases are executed, but does not seem te be active when an additional plugin goal is executed (e.g. mvn verify dependency:tree).

Expected behavior

When running mvn verify dependency:tree on a project that previously stored its build result in the Maven build cache, I expect the cached result to be restored.

Actual behavior

The previously cached result doesn't seem to be restored.

Reproduction

Here are the steps to reproduce this issue:

  1. Create a simple Maven project called my-app.
❯ mvn archetype:generate \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.5 \
    -DgroupId=com.example \
    -DartifactId=my-app \
    -Dversion=1.0.0-SNAPSHOT \
    -DjavaCompilerVersion=21 \
    -DinteractiveMode=false && cd my-app
  1. Add the Maven Build Cache Extension.
❯ cat <<EOF > .mvn/extensions.xml
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.1.0 https://maven.apache.org/xsd/core-extensions-1.1.0.xsd">
  <extension>
    <groupId>org.apache.maven.extensions</groupId>
    <artifactId>maven-build-cache-extension</artifactId>
    <version>1.2.0</version>
  </extension>
</extensions>
EOF
  1. Build it for the first time, see the Maven Build Cache Extension storing the result in the local cache.
❯ mvn verify
(...)
[INFO] Local build was not found by checksum 0b97f1d6be2b14ce for com.example:my-app
(...)
[INFO] Saved Build to local file: /Users/nbreunese/.m2/build-cache/v1.1/com.example/my-app/0b97f1d6be2b14ce/local/buildinfo.xml
(...)
  1. Clean and build again, and see the cached artifacts getting restored:
❯ mvn clean verify
(...)
[INFO] Found cached build, restoring com.example:my-app from cache by checksum 0b97f1d6be2b14ce
(...)

Up to here everything works as expected.

But now, when you run verify again, but add dependency:tree as an additional plugin goal, the build cache doesn't seem to be used, because there are no messages about restoring anything, and the tests are ran again:

❯ mvn clean verify dependency:tree
[INFO] Cache configuration is not available at configured path /Users/nbreunese/Projects/tmp/my-app/.mvn/maven-build-cache-config.xml, cache is enabled with defaults
[INFO] Using XX hash algorithm for cache
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:my-app >-------------------------
[INFO] Building my-app 1.0.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ my-app ---
[INFO] skip non existing resourceDirectory /Users/nbreunese/Projects/tmp/my-app/src/main/resources
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ my-app ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ my-app ---
[INFO] skip non existing resourceDirectory /Users/nbreunese/Projects/tmp/my-app/src/test/resources
[INFO]
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ my-app ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.3.0:test (default-test) @ my-app ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.014 s -- in com.example.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.4.2:jar (default-jar) @ my-app ---
[INFO]
[INFO] --- dependency:3.7.0:tree (default-cli) @ my-app ---
[INFO] com.example:my-app:jar:1.0.0-SNAPSHOT
[INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.11.0:test
[INFO] |  +- org.opentest4j:opentest4j:jar:1.3.0:test
[INFO] |  +- org.junit.platform:junit-platform-commons:jar:1.11.0:test
[INFO] |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] \- org.junit.jupiter:junit-jupiter-params:jar:5.11.0:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.086 s
[INFO] Finished at: 2025-10-27T14:25:17+01:00
[INFO] ------------------------------------------------------------------------

breun avatar Oct 27 '25 13:10 breun

By design, the cache uses only phases.

olamy avatar Oct 28 '25 04:10 olamy

@olamy I understand, but then why isn't it used for the verify phase in that last command?

breun avatar Oct 28 '25 08:10 breun

By the way, I know that mvn verify and mvn dependency:tree could easily be run separately and then the build cache would be used, but my actual use case is executing mvn verify jib:build to build a container image, and jib:build can't be run standalone (unless I'd first execute install, which would unnecessarily grow our GitLab CI cache).

breun avatar Oct 28 '25 08:10 breun