Skip to content

Commit c2878e1

Browse files
committed
[MBUILDCACHE-86] Adapt restoration logic to MBUILDCACHE-80 developments
1 parent 31345f3 commit c2878e1

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@
5151
import java.util.concurrent.Future;
5252
import java.util.concurrent.FutureTask;
5353
import java.util.concurrent.atomic.AtomicBoolean;
54-
import java.util.function.Consumer;
54+
import java.util.function.Function;
5555
import java.util.regex.Pattern;
5656

57-
import org.apache.commons.io.FileUtils;
5857
import org.apache.commons.io.FilenameUtils;
5958
import org.apache.commons.lang3.StringUtils;
6059
import org.apache.commons.lang3.mutable.MutableBoolean;
@@ -306,14 +305,15 @@ private boolean canIgnoreMissingSegment(MavenProject project, Build info, List<M
306305
return true;
307306
}
308307

309-
private Consumer<File> createRestorationToDiskConsumer(final MavenProject project, final Artifact artifact) {
308+
private Function<File, File> createRestorationToDiskConsumer(final MavenProject project, final Artifact artifact) {
309+
310310
if (cacheConfig.isRestoreOnDiskArtifacts() && MavenProjectInput.isRestoreOnDiskArtifacts(project)) {
311311

312+
Path restorationPath = project.getBasedir().toPath().resolve(artifact.getFilePath());
312313
final AtomicBoolean restored = new AtomicBoolean(false);
313314
return file -> {
314315
// Set to restored even if it fails later, we don't want multiple try
315316
if (restored.compareAndSet(false, true)) {
316-
Path restorationPath = project.getBasedir().toPath().resolve(artifact.getFilePath());
317317
verifyRestorationInsideProject(project, restorationPath);
318318
try {
319319
Files.createDirectories(restorationPath.getParent());
@@ -324,10 +324,11 @@ private Consumer<File> createRestorationToDiskConsumer(final MavenProject projec
324324
}
325325
LOGGER.debug("Restored file on disk ({} to {})", artifact.getFileName(), restorationPath);
326326
}
327+
return restorationPath.toFile();
327328
};
328329
}
329330
// Return a consumer doing nothing
330-
return file -> {};
331+
return file -> file;
331332
}
332333

333334
private boolean isPathInsideProject(final MavenProject project, Path path) {
@@ -434,7 +435,7 @@ private RestoredArtifact restoredArtifact(
434435
String artifactType,
435436
String artifactClassifier,
436437
Future<File> artifactFile,
437-
Consumer<File> restoreToDiskConsumer) {
438+
Function<File, File> restoreToDiskConsumer) {
438439
ArtifactHandler handler = null;
439440

440441
if (artifactType != null) {
@@ -462,24 +463,14 @@ private Future<File> createDownloadTask(
462463
final FutureTask<File> downloadTask = new FutureTask<>(() -> {
463464
LOGGER.debug("Downloading artifact {}", artifact.getArtifactId());
464465
final Path artifactFile = localCache.getArtifactFile(context, cacheResult.getSource(), artifact);
465-
final Path targetDir = Paths.get(project.getBuild().getDirectory());
466-
final Path targetArtifact = targetDir.resolve(project.getBuild().getFinalName()
467-
+ (artifact.getClassifier() != null ? "-".concat(artifact.getClassifier()) : "")
468-
+ ".".concat(FilenameUtils.getExtension(artifact.getFileName())));
469466

470467
if (!Files.exists(artifactFile)) {
471468
throw new FileNotFoundException("Missing file for cached build, cannot restore. File: " + artifactFile);
472469
}
473470
LOGGER.debug("Downloaded artifact " + artifact.getArtifactId() + " to: " + artifactFile);
474-
475-
File downloadFile = restoreArtifactHandler
471+
return restoreArtifactHandler
476472
.adjustArchiveArtifactVersion(project, originalVersion, artifactFile)
477473
.toFile();
478-
// Need to restore artifact to project build directory, so it can be saved into cached incremental build
479-
FileUtils.copyFile(downloadFile, targetArtifact.toFile());
480-
LOGGER.debug("Restored artifact " + artifact.getArtifactId() + " to: " + targetArtifact);
481-
482-
return targetArtifact.toFile();
483474
});
484475
if (!cacheConfig.isLazyRestore()) {
485476
downloadTask.run();

src/main/java/org/apache/maven/buildcache/artifact/RestoredArtifact.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import java.util.concurrent.ExecutionException;
2424
import java.util.concurrent.Future;
2525
import java.util.concurrent.RunnableFuture;
26-
import java.util.concurrent.atomic.AtomicBoolean;
27-
import java.util.function.Consumer;
26+
import java.util.function.Function;
2827

2928
import org.apache.maven.artifact.Artifact;
3029
import org.apache.maven.artifact.DefaultArtifact;
@@ -48,17 +47,15 @@ public class RestoredArtifact extends DefaultArtifact {
4847

4948
private volatile Future<File> fileFuture;
5049

51-
private AtomicBoolean restoredToDisk = new AtomicBoolean(false);
52-
53-
private Consumer<File> restoreToDiskConsumer;
50+
private Function<File, File> restoreToDiskConsumer;
5451

5552
public RestoredArtifact(
5653
Artifact parent,
5754
Future<File> fileFuture,
5855
String type,
5956
String classifier,
6057
ArtifactHandler handler,
61-
Consumer<File> restoreToDiskConsumer) {
58+
Function<File, File> restoreToDiskConsumer) {
6259
super(
6360
parent.getGroupId(),
6461
parent.getArtifactId(),
@@ -102,8 +99,7 @@ public File getFile() {
10299

103100
try {
104101
File file = fileFuture.get();
105-
restoreToDiskConsumer.accept(file);
106-
return file;
102+
return restoreToDiskConsumer.apply(file);
107103
} catch (InterruptedException e) {
108104
Thread.currentThread().interrupt();
109105
throw new InvalidArtifactRTException(

0 commit comments

Comments
 (0)