in src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java [483:567]
public void save(
CacheResult cacheResult,
List<MojoExecution> mojoExecutions,
Map<String, MojoExecutionEvent> executionEvents) {
CacheContext context = cacheResult.getContext();
if (context == null || context.getInputInfo() == null) {
LOGGER.info("Cannot save project in cache, skipping");
return;
}
final MavenProject project = context.getProject();
final MavenSession session = context.getSession();
try {
final HashFactory hashFactory = cacheConfig.getHashFactory();
final org.apache.maven.artifact.Artifact projectArtifact = project.getArtifact();
final List<org.apache.maven.artifact.Artifact> attachedArtifacts;
final List<Artifact> attachedArtifactDtos;
final Artifact projectArtifactDto;
if (project.hasLifecyclePhase("package")) {
final HashAlgorithm algorithm = hashFactory.createAlgorithm();
attachGeneratedSources(project);
attachOutputs(project);
attachedArtifacts = project.getAttachedArtifacts() != null
? project.getAttachedArtifacts()
: Collections.emptyList();
attachedArtifactDtos = artifactDtos(attachedArtifacts, algorithm, project);
projectArtifactDto = artifactDto(project.getArtifact(), algorithm, project);
} else {
attachedArtifacts = Collections.emptyList();
attachedArtifactDtos = new ArrayList<>();
projectArtifactDto = null;
}
List<CompletedExecution> completedExecution = buildExecutionInfo(mojoExecutions, executionEvents);
final Build build = new Build(
session.getGoals(),
projectArtifactDto,
attachedArtifactDtos,
context.getInputInfo(),
completedExecution,
hashFactory.getAlgorithm());
populateGitInfo(build, session);
build.getDto().set_final(cacheConfig.isSaveToRemoteFinal());
cacheResults.put(getVersionlessProjectKey(project), rebuilded(cacheResult, build));
localCache.beforeSave(context);
// if package phase presence means new artifacts were packaged
if (project.hasLifecyclePhase("package")) {
if (projectArtifact.getFile() != null) {
localCache.saveArtifactFile(cacheResult, projectArtifact);
}
for (org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts) {
if (attachedArtifact.getFile() != null) {
boolean storeArtifact =
isOutputArtifact(attachedArtifact.getFile().getName());
if (storeArtifact) {
localCache.saveArtifactFile(cacheResult, attachedArtifact);
} else {
LOGGER.debug(
"Skipping attached project artifact '{}' = "
+ " it is marked for exclusion from caching",
attachedArtifact.getFile().getName());
}
}
}
}
localCache.saveBuildInfo(cacheResult, build);
if (cacheConfig.isBaselineDiffEnabled()) {
produceDiffReport(cacheResult, build);
}
} catch (Exception e) {
LOGGER.error("Failed to save project, cleaning cache. Project: {}", project, e);
try {
localCache.clearCache(context);
} catch (Exception ex) {
LOGGER.error("Failed to clean cache due to unexpected error:", ex);
}
}
}