Skip to content

Commit ac44ccb

Browse files
authored
Fix IllegalStateException when cache is disabled via command line (#424)
1 parent db0ce24 commit ac44ccb

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ public void execute(
152152
}
153153

154154
try {
155-
if (!restored && !forkedExecution) {
155+
if (cacheState == INITIALIZED && !restored && !forkedExecution) {
156156
// Move pre-existing artifacts to staging directory to prevent caching stale files
157157
// from previous builds (e.g., after source changes or from cache restored
158158
// with clock skew). This ensures save() only sees fresh files built during this session.
159159
// Skip for forked executions since they don't cache and shouldn't modify artifacts.
160+
// Skip when cache is disabled to avoid accessing uninitialized cache configuration.
160161
try {
161162
cacheController.stagePreExistingArtifacts(session, project);
162163
} catch (IOException e) {
@@ -193,7 +194,8 @@ public void execute(
193194
// Always restore staged files after build completes (whether save ran or not).
194195
// Files that were rebuilt are discarded; files that weren't rebuilt are restored.
195196
// Skip for forked executions since they don't stage artifacts.
196-
if (!restored && !forkedExecution) {
197+
// Skip when cache is disabled since staging was not performed.
198+
if (cacheState == INITIALIZED && !restored && !forkedExecution) {
197199
cacheController.restoreStagedArtifacts(session, project);
198200
}
199201
}

src/test/java/org/apache/maven/buildcache/its/SkipBuildExtensionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.maven.buildcache.its;
2020

21+
import java.util.Arrays;
2122
import java.util.List;
2223

2324
import org.apache.maven.buildcache.its.junit.IntegrationTest;
@@ -56,6 +57,29 @@ void multipleGoals(Verifier verifier) throws VerificationException {
5657
verifyNoTextInLog(verifier, "Build cache is disabled for 'clean' goal.");
5758
}
5859

60+
/**
61+
* Verifies that running with -Dmaven.build.cache.enabled=false does not cause
62+
* IllegalStateException and the build completes successfully.
63+
* <p>
64+
* This tests the fix for the regression where stagePreExistingArtifacts() was called
65+
* without checking if the cache was initialized, causing IllegalStateException when
66+
* cache is disabled via command line.
67+
*
68+
* @see <a href="https://github.com/apache/maven-build-cache-extension/pull/394#issuecomment-3714680789">PR #394 comment</a>
69+
*/
70+
@Test
71+
void cacheDisabledViaCommandLine(Verifier verifier) throws VerificationException {
72+
verifier.setAutoclean(false);
73+
verifier.addCliOption("-Dmaven.build.cache.enabled=false");
74+
75+
verifier.setLogFileName("../log-cache-disabled.txt");
76+
verifier.executeGoals(Arrays.asList("clean", "install"));
77+
verifier.verifyErrorFreeLog();
78+
79+
// Verify cache was actually disabled
80+
verifier.verifyTextInLog("Cache disabled by command line flag");
81+
}
82+
5983
private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException {
6084
Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text));
6185
}

0 commit comments

Comments
 (0)