From 7a182a57945381d69bb6cd227279b4b3a3e29325 Mon Sep 17 00:00:00 2001 From: Zorglube <630192+zorglube@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:22:25 +0100 Subject: [PATCH] Add option: `maven.build.cache.forceRenew` --- .../BuildCacheMojosExecutionStrategy.java | 38 +++++++++---------- .../checksum/MavenProjectInput.java | 17 +++++---- .../maven/buildcache/xml/CacheConfigImpl.java | 17 +++------ src/main/mdo/build-cache-config.mdo | 6 +++ 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java index 90122857..f928368f 100644 --- a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java +++ b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java @@ -69,7 +69,6 @@ @SessionScoped @Named @Priority(10) -@SuppressWarnings("unused") public class BuildCacheMojosExecutionStrategy implements MojosExecutionStrategy { private static final Logger LOGGER = LoggerFactory.getLogger(BuildCacheMojosExecutionStrategy.class); @@ -79,7 +78,7 @@ public class BuildCacheMojosExecutionStrategy implements MojosExecutionStrategy private final MojoParametersListener mojoListener; private final LifecyclePhasesHelper lifecyclePhasesHelper; private final MavenPluginManager mavenPluginManager; - private MojoExecutionScope mojoExecutionScope; + private final MojoExecutionScope mojoExecutionScope; @Inject public BuildCacheMojosExecutionStrategy( @@ -97,6 +96,7 @@ public BuildCacheMojosExecutionStrategy( this.mojoExecutionScope = mojoExecutionScope; } + @Override public void execute( List mojoExecutions, MavenSession session, MojoExecutionRunner mojoExecutionRunner) throws LifecycleExecutionException { @@ -137,7 +137,8 @@ public void execute( restored = CacheRestorationStatus.SUCCESS == cacheRestorationStatus; executeExtraCleanPhaseIfNeeded(cacheRestorationStatus, cleanPhase, mojoExecutionRunner); } - if (!restored) { + + if (!restored || MavenProjectInput.isForceRenew(project)) { for (MojoExecution mojoExecution : mojoExecutions) { if (source == Source.CLI || mojoExecution.getLifecyclePhase() == null @@ -222,12 +223,10 @@ private CacheRestorationStatus restoreProject( for (MojoExecution cacheCandidate : cachedSegment) { if (cacheController.isForcedExecution(project, cacheCandidate)) { forcedExecutionMojos.add(cacheCandidate); - } else { - if (!verifyCacheConsistency( - cacheCandidate, build, project, session, mojoExecutionRunner, cacheConfig)) { - LOGGER.info("A cached mojo is not consistent, continuing with non cached build"); - return CacheRestorationStatus.FAILURE; - } + } else if (!verifyCacheConsistency( + cacheCandidate, build, project, session, mojoExecutionRunner, cacheConfig)) { + LOGGER.info("A cached mojo is not consistent, continuing with non cached build"); + return CacheRestorationStatus.FAILURE; } } @@ -250,13 +249,13 @@ private CacheRestorationStatus restoreProject( mojoExecutionScope.seed(MojoExecution.class, cacheCandidate); // need maven 4 as minumum // mojoExecutionScope.seed( - // org.apache.maven.api.plugin.Log.class, - // new DefaultLog(LoggerFactory.getLogger( - // cacheCandidate.getMojoDescriptor().getFullGoalName()))); + // org.apache.maven.api.plugin.Log.class, + // new DefaultLog(LoggerFactory.getLogger( + // cacheCandidate.getMojoDescriptor().getFullGoalName()))); // mojoExecutionScope.seed(Project.class, ((DefaultSession) // session.getSession()).getProject(project)); // mojoExecutionScope.seed( - // org.apache.maven.api.MojoExecution.class, new DefaultMojoExecution(cacheCandidate)); + // org.apache.maven.api.MojoExecution.class, new DefaultMojoExecution(cacheCandidate)); mojoExecutionRunner.run(cacheCandidate); } else { LOGGER.info( @@ -365,7 +364,7 @@ boolean isParamsMatched( currentValue = normalizedPath(path, baseDirPath); } else if (value instanceof Path) { Path baseDirPath = project.getBasedir().toPath(); - currentValue = normalizedPath(((Path) value), baseDirPath); + currentValue = normalizedPath((Path) value, baseDirPath); } else if (value != null && value.getClass().isArray()) { currentValue = ArrayUtils.toString(value); } else { @@ -384,13 +383,12 @@ boolean isParamsMatched( expectedValue, currentValue); return false; - } else { - LOGGER.warn( - "Cache contains plugin execution with skip flag and might be incomplete. " - + "Property: {}, execution {}", - propertyName, - mojoExecutionKey(mojoExecution)); } + LOGGER.warn( + "Cache contains plugin execution with skip flag and might be incomplete. " + + "Property: {}, execution {}", + propertyName, + mojoExecutionKey(mojoExecution)); } } return true; diff --git a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java index b6df5ec6..1692eda0 100644 --- a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java +++ b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java @@ -91,6 +91,7 @@ import static org.apache.maven.buildcache.CacheUtils.isPom; import static org.apache.maven.buildcache.CacheUtils.isSnapshot; import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_ENABLED_PROPERTY_NAME; +import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_FORCE_RENEW_PROPERTY_NAME; import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_SKIP; import static org.apache.maven.buildcache.xml.CacheConfigImpl.RESTORE_GENERATED_SOURCES_PROPERTY_NAME; @@ -419,11 +420,9 @@ private void startWalk( } catch (IOException e) { throw new RuntimeException(e); } - } else { - if (!exclusionResolver.excludesPath(normalized)) { - LOGGER.debug("Adding: {}", normalized); - collectedFiles.add(normalized); - } + } else if (!exclusionResolver.excludesPath(normalized)) { + LOGGER.debug("Adding: {}", normalized); + collectedFiles.add(normalized); } } @@ -556,7 +555,7 @@ private Path getPathOrNull(String text) { // do not even bother logging about blank/null values } else if (equalsAnyIgnoreCase(text, "true", "false", "utf-8", "null", "\\") // common values || contains(text, "*") // tag value is a glob or regex - unclear how to process - || (contains(text, ":") && !contains(text, ":\\")) // artifactId + || contains(text, ":") && !contains(text, ":\\") // artifactId || startsWithAny(text, "com.", "org.", "io.", "java.", "javax.") // java packages || startsWithAny(text, "${env.") // env variables in maven notation || startsWithAny( @@ -720,7 +719,7 @@ public static boolean isSkipCache(MavenProject project) { * Allow skipping generated sources restoration on a per-project level via a property (which defaults to true) * e.g. {@code false}. * - * @param project + * @param project * @return */ public static boolean isRestoreGeneratedSources(MavenProject project) { @@ -738,4 +737,8 @@ public static boolean isRestoreGeneratedSources(MavenProject project) { public static boolean isCacheDisabled(MavenProject project) { return !Boolean.parseBoolean(project.getProperties().getProperty(CACHE_ENABLED_PROPERTY_NAME, "true")); } + + public static boolean isForceRenew(MavenProject project) { + return !Boolean.parseBoolean(project.getProperties().getProperty(CACHE_FORCE_RENEW_PROPERTY_NAME, "false")); + } } 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 75b16afc..246b541c 100644 --- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java +++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java @@ -76,7 +76,6 @@ */ @SessionScoped @Named -@SuppressWarnings("unused") public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheConfig { public static final String CONFIG_PATH_PROPERTY_NAME = "maven.build.cache.configPath"; @@ -92,6 +91,7 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon public static final String LAZY_RESTORE_PROPERTY_NAME = "maven.build.cache.lazyRestore"; public static final String RESTORE_GENERATED_SOURCES_PROPERTY_NAME = "maven.build.cache.restoreGeneratedSources"; public static final String ALWAYS_RUN_PLUGINS = "maven.build.cache.alwaysRunPlugins"; + public static final String CACHE_FORCE_RENEW_PROPERTY_NAME = "maven.build.cache.forceRenew"; /** * Flag to control if we should skip lookup for cached artifacts globally or for a particular project even if @@ -219,9 +219,8 @@ public List getTrackedProperties(MojoExecution mojoExecution) { final GoalReconciliation reconciliationConfig = findReconciliationConfig(mojoExecution); if (reconciliationConfig != null) { return reconciliationConfig.getReconciles(); - } else { - return Collections.emptyList(); } + return Collections.emptyList(); } @Override @@ -267,9 +266,8 @@ public List getLoggedProperties(MojoExecution mojoExecution) { final GoalReconciliation reconciliationConfig = findReconciliationConfig(mojoExecution); if (reconciliationConfig != null) { return reconciliationConfig.getLogs(); - } else { - return Collections.emptyList(); } + return Collections.emptyList(); } @Nonnull @@ -279,9 +277,8 @@ public List getNologProperties(MojoExecution mojoExecution) { final GoalReconciliation reconciliationConfig = findReconciliationConfig(mojoExecution); if (reconciliationConfig != null) { return reconciliationConfig.getNologs(); - } else { - return Collections.emptyList(); } + return Collections.emptyList(); } @Nonnull @@ -546,9 +543,8 @@ public boolean adjustMetaInfVersion() { return Optional.ofNullable(getConfiguration().getProjectVersioning()) .map(ProjectVersioning::isAdjustMetaInf) .orElse(false); - } else { - return false; } + return false; } @Override @@ -557,9 +553,8 @@ public boolean calculateProjectVersionChecksum() { return Optional.ofNullable(getConfiguration().getProjectVersioning()) .map(ProjectVersioning::isCalculateProjectVersionChecksum) .orElse(false); - } else { - return false; } + return false; } @Nonnull diff --git a/src/main/mdo/build-cache-config.mdo b/src/main/mdo/build-cache-config.mdo index 65f2ec7b..bcab10b7 100644 --- a/src/main/mdo/build-cache-config.mdo +++ b/src/main/mdo/build-cache-config.mdo @@ -235,6 +235,12 @@ under the License. FileHash (causes file hash is saved in build metadata) or EffectivePom (causes effective pom info is saved in build metadata) + + forceRenew + boolean + false + Force the reconstruction of the Cache +