This is an automated email from the ASF dual-hosted git repository.

olamy pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/maven-build-cache-extension.git


The following commit(s) were added to refs/heads/master by this push:
     new ac44ccb  Fix IllegalStateException when cache is disabled via command 
line (#424)
ac44ccb is described below

commit ac44ccb687a2ed1eb9d7010c8dad8b277edad6e9
Author: Gili Tzabari <[email protected]>
AuthorDate: Tue Jan 6 20:22:14 2026 -0500

    Fix IllegalStateException when cache is disabled via command line (#424)
---
 .../BuildCacheMojosExecutionStrategy.java          |  6 ++++--
 .../buildcache/its/SkipBuildExtensionTest.java     | 24 ++++++++++++++++++++++
 2 files changed, 28 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 d4715ed..952dd43 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 68f2da1..7b26021 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.
+     * <p>
+     * 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 <a 
href="https://github.com/apache/maven-build-cache-extension/pull/394#issuecomment-3714680789";>PR
 #394 comment</a>
+     */
+    @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));
     }

Reply via email to