Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down