Skip to content

Commit 48ca87c

Browse files
authored
[MBUILDCACHE-93] Command line configuration to skip saving in cache (#148)
1 parent 6a0be1c commit 48ca87c

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ public void execute(
148148
}
149149

150150
if (cacheState == INITIALIZED && (!result.isSuccess() || !restored)) {
151-
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
152-
cacheController.save(result, mojoExecutions, executionEvents);
151+
if (cacheConfig.isSkipSave()) {
152+
LOGGER.info("Cache saving is disabled.");
153+
} else {
154+
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
155+
cacheController.save(result, mojoExecutions, executionEvents);
156+
}
153157
}
154158

155159
if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {

src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,9 @@ public interface CacheConfig {
134134
boolean isRestoreGeneratedSources();
135135

136136
String getAlwaysRunPlugins();
137+
138+
/**
139+
* Flag to disable cache saving
140+
*/
141+
boolean isSkipSave();
137142
}

src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
103103
*/
104104
public static final String CACHE_SKIP = "maven.build.cache.skipCache";
105105

106+
/**
107+
* Flag to disable cache saving
108+
*/
109+
public static final String SKIP_SAVE = "maven.build.cache.skipSave";
110+
106111
private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfigImpl.class);
107112

108113
private final XmlService xmlService;
@@ -503,6 +508,11 @@ public String getAlwaysRunPlugins() {
503508
return getProperty(ALWAYS_RUN_PLUGINS, null);
504509
}
505510

511+
@Override
512+
public boolean isSkipSave() {
513+
return getProperty(SKIP_SAVE, false);
514+
}
515+
506516
@Override
507517
public String getId() {
508518
checkInitializedState();

src/site/markdown/parameters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This document contains various configuration parameters supported by the cache e
3636
| `-Dmaven.build.cache.restoreGeneratedSources=(true/false)` | Do not restore generated sources and directly attached files | Performance optimization |
3737
| `-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 |
3838
| `-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 |
39+
| `-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 |
3940

4041
### Project-level properties
4142

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

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

21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
2125
import org.apache.maven.buildcache.its.junit.IntegrationTest;
2226
import org.apache.maven.it.VerificationException;
2327
import org.apache.maven.it.Verifier;
28+
import org.junit.jupiter.api.Assertions;
2429
import org.junit.jupiter.api.Test;
2530

31+
import static org.apache.maven.buildcache.util.LogFileUtils.findFirstLineContainingTextsInLogs;
32+
import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_LOCATION_PROPERTY_NAME;
33+
import static org.apache.maven.buildcache.xml.CacheConfigImpl.SKIP_SAVE;
34+
2635
@IntegrationTest("src/test/projects/build-extension")
2736
public class BuildExtensionTest {
2837

@@ -41,4 +50,28 @@ void simple(Verifier verifier) throws VerificationException {
4150
verifier.verifyErrorFreeLog();
4251
verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache");
4352
}
53+
54+
@Test
55+
void skipSaving(Verifier verifier) throws VerificationException, IOException {
56+
verifier.setAutoclean(false);
57+
Path tempDirectory = Files.createTempDirectory("skip-saving-test");
58+
verifier.getCliOptions().clear();
59+
verifier.addCliOption("-D" + CACHE_LOCATION_PROPERTY_NAME + "=" + tempDirectory.toAbsolutePath());
60+
verifier.addCliOption("-D" + SKIP_SAVE + "=true");
61+
62+
verifier.setLogFileName("../log-1.txt");
63+
verifier.executeGoal("verify");
64+
verifier.verifyTextInLog("Cache saving is disabled.");
65+
verifier.verifyErrorFreeLog();
66+
67+
verifier.setLogFileName("../log-2.txt");
68+
verifier.executeGoal("verify");
69+
verifier.verifyErrorFreeLog();
70+
verifier.verifyTextInLog("Cache saving is disabled.");
71+
verifyNoTextInLog(verifier, "Found cached build, restoring");
72+
}
73+
74+
private static void verifyNoTextInLog(Verifier verifier, String text) throws VerificationException {
75+
Assertions.assertNull(findFirstLineContainingTextsInLogs(verifier, text));
76+
}
4477
}

0 commit comments

Comments
 (0)