From 1308ad56ebb50e6c65a1840bcf9b2434552fb1e5 Mon Sep 17 00:00:00 2001 From: cowwoc Date: Tue, 6 Jan 2026 08:30:53 -0500 Subject: [PATCH 1/2] Fix IllegalStateException when cache is disabled via command line When running with -Dmaven.build.cache.enabled=false, the build would fail with IllegalStateException: "Cache is not initialized. Actual state: DISABLED" because stagePreExistingArtifacts() was being called unconditionally. This commit adds a check for cacheState == INITIALIZED before calling stagePreExistingArtifacts() and restoreStagedArtifacts(), ensuring these cache-dependent operations are only performed when the cache is properly initialized. Fixes the regression reported in PR #394 comment. --- .../maven/buildcache/BuildCacheMojosExecutionStrategy.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java index d4715ed5..952dd433 100644 --- a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java +++ b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java @@ -152,11 +152,12 @@ public void execute( } try { - if (!restored && !forkedExecution) { + if (cacheState == INITIALIZED && !restored && !forkedExecution) { // Move pre-existing artifacts to staging directory to prevent caching stale files // from previous builds (e.g., after source changes or from cache restored // with clock skew). This ensures save() only sees fresh files built during this session. // Skip for forked executions since they don't cache and shouldn't modify artifacts. + // Skip when cache is disabled to avoid accessing uninitialized cache configuration. try { cacheController.stagePreExistingArtifacts(session, project); } catch (IOException e) { @@ -193,7 +194,8 @@ public void execute( // Always restore staged files after build completes (whether save ran or not). // Files that were rebuilt are discarded; files that weren't rebuilt are restored. // Skip for forked executions since they don't stage artifacts. - if (!restored && !forkedExecution) { + // Skip when cache is disabled since staging was not performed. + if (cacheState == INITIALIZED && !restored && !forkedExecution) { cacheController.restoreStagedArtifacts(session, project); } } From a454b2223c8185d6592978a63268c02343b3ef11 Mon Sep 17 00:00:00 2001 From: cowwoc Date: Tue, 6 Jan 2026 08:55:18 -0500 Subject: [PATCH 2/2] Add integration test for cache disabled via command line Adds a test that verifies builds succeed when cache is disabled with -Dmaven.build.cache.enabled=false. This tests the fix for the regression where stagePreExistingArtifacts() was called without checking if the cache was initialized. --- .../its/SkipBuildExtensionTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/org/apache/maven/buildcache/its/SkipBuildExtensionTest.java b/src/test/java/org/apache/maven/buildcache/its/SkipBuildExtensionTest.java index 68f2da15..7b26021b 100644 --- a/src/test/java/org/apache/maven/buildcache/its/SkipBuildExtensionTest.java +++ b/src/test/java/org/apache/maven/buildcache/its/SkipBuildExtensionTest.java @@ -18,6 +18,7 @@ */ package org.apache.maven.buildcache.its; +import java.util.Arrays; import java.util.List; import org.apache.maven.buildcache.its.junit.IntegrationTest; @@ -56,6 +57,29 @@ void multipleGoals(Verifier verifier) throws VerificationException { verifyNoTextInLog(verifier, "Build cache is disabled for 'clean' goal."); } + /** + * Verifies that running with -Dmaven.build.cache.enabled=false does not cause + * IllegalStateException and the build completes successfully. + *

+ * This tests the fix for the regression where stagePreExistingArtifacts() was called + * without checking if the cache was initialized, causing IllegalStateException when + * cache is disabled via command line. + * + * @see PR #394 comment + */ + @Test + void cacheDisabledViaCommandLine(Verifier verifier) throws VerificationException { + verifier.setAutoclean(false); + verifier.addCliOption("-Dmaven.build.cache.enabled=false"); + + verifier.setLogFileName("../log-cache-disabled.txt"); + verifier.executeGoals(Arrays.asList("clean", "install")); + verifier.verifyErrorFreeLog(); + + // Verify cache was actually disabled + verifier.verifyTextInLog("Cache disabled by command line flag"); + } + private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException { Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text)); }