You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2024/04/29 00:22:47 UTC

(maven-build-cache-extension) branch master updated: [MBUILDCACHE-93] Command line configuration to skip saving in cache (#148)

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 48ca87c  [MBUILDCACHE-93] Command line configuration to skip saving in cache (#148)
48ca87c is described below

commit 48ca87cb577020e3441b9aca6851a4b863d7d3cf
Author: Kévin Buntrock <ke...@kleegroup.com>
AuthorDate: Mon Apr 29 02:22:42 2024 +0200

    [MBUILDCACHE-93] Command line configuration to skip saving in cache (#148)
---
 .../BuildCacheMojosExecutionStrategy.java          |  8 ++++--
 .../apache/maven/buildcache/xml/CacheConfig.java   |  5 ++++
 .../maven/buildcache/xml/CacheConfigImpl.java      | 10 +++++++
 src/site/markdown/parameters.md                    |  1 +
 .../maven/buildcache/its/BuildExtensionTest.java   | 33 ++++++++++++++++++++++
 5 files changed, 55 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 9012285..e35981d 100644
--- a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
+++ b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
@@ -148,8 +148,12 @@ public class BuildCacheMojosExecutionStrategy implements MojosExecutionStrategy
             }
 
             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) {
diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
index 2569a16..797e1f4 100644
--- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
+++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java
@@ -134,4 +134,9 @@ public interface CacheConfig {
     boolean isRestoreGeneratedSources();
 
     String getAlwaysRunPlugins();
+
+    /**
+     * Flag to disable cache saving
+     */
+    boolean isSkipSave();
 }
diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
index 75b16af..8c98afb 100644
--- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
@@ -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;
@@ -503,6 +508,11 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
         return getProperty(ALWAYS_RUN_PLUGINS, null);
     }
 
+    @Override
+    public boolean isSkipSave() {
+        return getProperty(SKIP_SAVE, false);
+    }
+
     @Override
     public String getId() {
         checkInitializedState();
diff --git a/src/site/markdown/parameters.md b/src/site/markdown/parameters.md
index 16269f1..abb2fe5 100644
--- a/src/site/markdown/parameters.md
+++ b/src/site/markdown/parameters.md
@@ -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
 
diff --git a/src/test/java/org/apache/maven/buildcache/its/BuildExtensionTest.java b/src/test/java/org/apache/maven/buildcache/its/BuildExtensionTest.java
index e111fd4..3878893 100644
--- a/src/test/java/org/apache/maven/buildcache/its/BuildExtensionTest.java
+++ b/src/test/java/org/apache/maven/buildcache/its/BuildExtensionTest.java
@@ -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 {
 
@@ -41,4 +50,28 @@ public class BuildExtensionTest {
         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));
+    }
 }