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); } } 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)); }