Skip to content

Commit

Permalink
[MBUILDCACHE-93] Command line configuration to skip saving in cache (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kbuntrock authored Apr 29, 2024
1 parent 6a0be1c commit 48ca87c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@ public void execute(
}

if (cacheState == INITIALIZED && (!result.isSuccess() || !restored)) {
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
cacheController.save(result, mojoExecutions, executionEvents);
if (cacheConfig.isSkipSave()) {
LOGGER.info("Cache saving is disabled.");
} else {
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
cacheController.save(result, mojoExecutions, executionEvents);
}
}

if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ public interface CacheConfig {
boolean isRestoreGeneratedSources();

String getAlwaysRunPlugins();

/**
* Flag to disable cache saving
*/
boolean isSkipSave();
}
10 changes: 10 additions & 0 deletions src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
*/
public static final String CACHE_SKIP = "maven.build.cache.skipCache";

/**
* Flag to disable cache saving
*/
public static final String SKIP_SAVE = "maven.build.cache.skipSave";

private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfigImpl.class);

private final XmlService xmlService;
Expand Down Expand Up @@ -503,6 +508,11 @@ public String getAlwaysRunPlugins() {
return getProperty(ALWAYS_RUN_PLUGINS, null);
}

@Override
public boolean isSkipSave() {
return getProperty(SKIP_SAVE, false);
}

@Override
public String getId() {
checkInitializedState();
Expand Down
1 change: 1 addition & 0 deletions src/site/markdown/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This document contains various configuration parameters supported by the cache e
| `-Dmaven.build.cache.restoreGeneratedSources=(true/false)` | Do not restore generated sources and directly attached files | Performance optimization |
| `-Dmaven.build.cache.alwaysRunPlugins=<list of plugins>` | Comma separated list of plugins to always run regardless of cache state. An example: `maven-deploy-plugin:*,maven-install-plugin:install` | Remote cache setup/tuning/troubleshooting |
| `-Dmaven.build.cache.skipCache=(true/false)` | Skip looking up artifacts in caches. Does not affect writing artifacts to caches, disables only reading when set to `true` | May be used to trigger a forced rebuild when matching artifacts do exist in caches |
| `-Dmaven.build.cache.skipSave=(true/false)` | Skip writing build result in caches. Does not affect reading from the cache. | Configuring MR builds to benefits from the cache, but restricting writes to the `master` branch |

### Project-level properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@
*/
package org.apache.maven.buildcache.its;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.buildcache.its.junit.IntegrationTest;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.apache.maven.buildcache.util.LogFileUtils.findFirstLineContainingTextsInLogs;
import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_LOCATION_PROPERTY_NAME;
import static org.apache.maven.buildcache.xml.CacheConfigImpl.SKIP_SAVE;

@IntegrationTest("src/test/projects/build-extension")
public class BuildExtensionTest {

Expand All @@ -41,4 +50,28 @@ void simple(Verifier verifier) throws VerificationException {
verifier.verifyErrorFreeLog();
verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache");
}

@Test
void skipSaving(Verifier verifier) throws VerificationException, IOException {
verifier.setAutoclean(false);
Path tempDirectory = Files.createTempDirectory("skip-saving-test");
verifier.getCliOptions().clear();
verifier.addCliOption("-D" + CACHE_LOCATION_PROPERTY_NAME + "=" + tempDirectory.toAbsolutePath());
verifier.addCliOption("-D" + SKIP_SAVE + "=true");

verifier.setLogFileName("../log-1.txt");
verifier.executeGoal("verify");
verifier.verifyTextInLog("Cache saving is disabled.");
verifier.verifyErrorFreeLog();

verifier.setLogFileName("../log-2.txt");
verifier.executeGoal("verify");
verifier.verifyErrorFreeLog();
verifier.verifyTextInLog("Cache saving is disabled.");
verifyNoTextInLog(verifier, "Found cached build, restoring");
}

private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException {
Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text));
}
}

0 comments on commit 48ca87c

Please sign in to comment.