in src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java [100:173]
public void execute(
List<MojoExecution> mojoExecutions, MavenSession session, MojoExecutionRunner mojoExecutionRunner)
throws LifecycleExecutionException {
try {
final MavenProject project = session.getCurrentProject();
final Source source = getSource(mojoExecutions);
// execute clean bound goals before restoring to not interfere/slowdown clean
CacheState cacheState = DISABLED;
CacheResult result = CacheResult.empty();
boolean skipCache = cacheConfig.isSkipCache() || MavenProjectInput.isSkipCache(project);
boolean cacheIsDisabled = MavenProjectInput.isCacheDisabled(project);
// Forked execution should be thought as a part of originating mojo internal implementation
// If forkedExecution is detected, it means that originating mojo is not cached so forks should rerun too
boolean forkedExecution = lifecyclePhasesHelper.isForkedProject(project);
List<MojoExecution> cleanPhase = null;
if (source == Source.LIFECYCLE && !forkedExecution) {
cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
if (!cacheIsDisabled) {
cacheState = cacheConfig.initialize();
} else {
LOGGER.info(
"Cache is explicitly disabled on project level for {}", getVersionlessProjectKey(project));
}
if (cacheState == INITIALIZED || skipCache) {
result = cacheController.findCachedBuild(session, project, mojoExecutions, skipCache);
}
}
boolean restorable = result.isSuccess() || result.isPartialSuccess();
boolean restored = false; // if partially restored need to save increment
if (restorable) {
CacheRestorationStatus cacheRestorationStatus =
restoreProject(result, mojoExecutions, mojoExecutionRunner, cacheConfig);
restored = CacheRestorationStatus.SUCCESS == cacheRestorationStatus;
executeExtraCleanPhaseIfNeeded(cacheRestorationStatus, cleanPhase, mojoExecutionRunner);
}
if (!restored) {
for (MojoExecution mojoExecution : mojoExecutions) {
if (source == Source.CLI
|| mojoExecution.getLifecyclePhase() == null
|| lifecyclePhasesHelper.isLaterPhaseThanClean(mojoExecution.getLifecyclePhase())) {
mojoExecutionRunner.run(mojoExecution);
}
}
}
if (cacheState == INITIALIZED && (!result.isSuccess() || !restored)) {
if (cacheConfig.isSkipSave()) {
LOGGER.info("Cache saving is disabled.");
} else if (cacheConfig.isMandatoryClean()
&& lifecyclePhasesHelper
.getCleanSegment(project, mojoExecutions)
.isEmpty()) {
LOGGER.info("Cache storing is skipped since there was no \"clean\" phase.");
} else {
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
cacheController.save(result, mojoExecutions, executionEvents);
}
}
if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {
throw new LifecycleExecutionException(
"Failed to restore project[" + getVersionlessProjectKey(project)
+ "] from cache, failing build.",
project);
}
} catch (MojoExecutionException e) {
throw new LifecycleExecutionException(e.getMessage(), e);
}
}