Skip to content

Commit

Permalink
[MBUILDCACHE-80] - Fix for incremental builds with a higher goal than…
Browse files Browse the repository at this point in the history
… the highest cached goal is rebuilding the full project from scratch (#128)

* [MBUILDCACHE-80] Fix Incremental builds with a higher goal than the highest cached goal is rebuilding the full project from scratch (#1)

* fix incremental build cache replay problem

* fix Issue67Test regression for restoration error is handled properly

* Add simple IncrementalRestoreTest unit test

* Fix restore cached artifact using build's final artifact name

* Resolve the dual meaning of `restored` variable usage in cache restore
  • Loading branch information
igdianov authored Feb 4, 2024
1 parent 17c707e commit 5af2549
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public void execute(
}

boolean restorable = result.isSuccess() || result.isPartialSuccess();
boolean restored = result.isSuccess(); // if partially restored need to save increment
boolean restored = false; // if partially restored need to save increment
if (restorable) {
CacheRestorationStatus cacheRestorationStatus =
restoreProject(result, mojoExecutions, mojoExecutionRunner, cacheConfig);
restored &= CacheRestorationStatus.SUCCESS == cacheRestorationStatus;
restored = CacheRestorationStatus.SUCCESS == cacheRestorationStatus;
executeExtraCleanPhaseIfNeeded(cacheRestorationStatus, cleanPhase, mojoExecutionRunner);
}
if (!restored) {
Expand All @@ -147,7 +147,7 @@ public void execute(
}
}

if (cacheState == INITIALIZED && (!restorable || !restored)) {
if (cacheState == INITIALIZED && (!result.isSuccess() || !restored)) {
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
cacheController.save(result, mojoExecutions, executionEvents);
}
Expand Down Expand Up @@ -262,6 +262,20 @@ private CacheRestorationStatus restoreProject(
LOGGER.info(
"Skipping plugin execution (cached): {}",
cacheCandidate.getMojoDescriptor().getFullGoalName());
// Need to populate cached candidate executions for the build cache save result
Mojo mojo = null;
try {
mojo = mavenPluginManager.getConfiguredMojo(Mojo.class, session, cacheCandidate);
MojoExecutionEvent mojoExecutionEvent =
new MojoExecutionEvent(session, project, cacheCandidate, mojo);
mojoListener.beforeMojoExecution(mojoExecutionEvent);
} catch (PluginConfigurationException | PluginContainerException e) {
throw new RuntimeException(e);
} finally {
if (mojo != null) {
mavenPluginManager.releaseMojo(mojo, cacheCandidate);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ public ArtifactRestorationReport restoreProjectArtifacts(CacheResult cacheResult
// in which case, the project is unmodified and we continue with normal build.
if (restoredProjectArtifact != null) {
project.setArtifact(restoredProjectArtifact);
// need to include package lifecycle to save build info for incremental builds
if (!project.hasLifecyclePhase("package")) {
project.addLifecyclePhase("package");
}
}
restoredAttachedArtifacts.forEach(project::addAttachedArtifact);
restorationReport.setSuccess(true);
Expand Down Expand Up @@ -399,13 +403,24 @@ private Future<File> createDownloadTask(
final FutureTask<File> downloadTask = new FutureTask<>(() -> {
LOGGER.debug("Downloading artifact {}", artifact.getArtifactId());
final Path artifactFile = localCache.getArtifactFile(context, cacheResult.getSource(), artifact);
final Path targetDir = Paths.get(project.getBuild().getDirectory());
final Path targetArtifact = targetDir.resolve(project.getBuild().getFinalName()
+ (artifact.getClassifier() != null ? "-".concat(artifact.getClassifier()) : "")
+ ".".concat(FilenameUtils.getExtension(artifact.getFileName())));

if (!Files.exists(artifactFile)) {
throw new FileNotFoundException("Missing file for cached build, cannot restore. File: " + artifactFile);
}
LOGGER.debug("Downloaded artifact " + artifact.getArtifactId() + " to: " + artifactFile);
return restoreArtifactHandler

File downloadFile = restoreArtifactHandler
.adjustArchiveArtifactVersion(project, originalVersion, artifactFile)
.toFile();
// Need to restore artifact to project build directory, so it can be saved into cached incremental build
FileUtils.copyFile(downloadFile, targetArtifact.toFile());
LOGGER.debug("Restored artifact " + artifact.getArtifactId() + " to: " + targetArtifact);

return targetArtifact.toFile();
});
if (!cacheConfig.isLazyRestore()) {
downloadTask.run();
Expand Down
Loading

0 comments on commit 5af2549

Please sign in to comment.