From ea63e87e705bf803bc3ae2734a96ce0d53b9a306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Thu, 6 Jul 2023 17:40:06 +0200 Subject: [PATCH] MSHARED-1285 use an up-to-date scanner instead the newscanner Currently it could happen that the scanner misses changed files (because they are not part of the delta) or copies files even if they have not changed (e.g. because the output has changes). This uses now a different approach, instead of only handling the delta files, we scan all inputs and compare if they are up-to-date with the output. --- .../DefaultMavenResourcesFiltering.java | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java index 5645601a..eb9a2e5b 100644 --- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java +++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java @@ -37,6 +37,7 @@ import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.maven.model.Resource; +import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.Scanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -189,8 +190,7 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr // as destination // see MNG-1345 File outputDirectory = mavenResourcesExecution.getOutputDirectory(); - boolean outputExists = outputDirectory.exists(); - if (!outputExists && !outputDirectory.mkdirs()) { + if (!outputDirectory.mkdirs() && !outputDirectory.exists()) { throw new MavenFilteringException("Cannot create resource output directory: " + outputDirectory); } @@ -198,11 +198,29 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr isFilteringUsed = true; } - boolean ignoreDelta = !outputExists - || buildContext.hasDelta(mavenResourcesExecution.getFileFilters()) - || buildContext.hasDelta(getRelativeOutputDirectory(mavenResourcesExecution)); - LOGGER.debug("ignoreDelta " + ignoreDelta); - Scanner scanner = buildContext.newScanner(resourceDirectory, ignoreDelta); + Path resourcePath = resourceDirectory.toPath(); + DirectoryScanner scanner = new DirectoryScanner() { + @Override + protected boolean isSelected(String name, File file) { + try { + if (file.isFile() && buildContext.isUptodate(getTargetFile(file), file)) { + return false; + } + } catch (MavenFilteringException e) { + // can't really do anything than to assume we must copy the file... + } + return true; + } + + private File getTargetFile(File file) throws MavenFilteringException { + Path relativize = resourcePath.relativize(file.toPath()); + return getDestinationFile( + outputDirectory, targetPath, relativize.toString(), mavenResourcesExecution); + } + }; + scanner.setBasedir(resourceDirectory); + + /* buildContext.newScanner(resourceDirectory, ignoreDelta); */ setupScanner(resource, scanner, mavenResourcesExecution.isAddDefaultExcludes()); @@ -276,13 +294,13 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr // deal with deleted source files - scanner = buildContext.newDeleteScanner(resourceDirectory); + Scanner deleteScanner = buildContext.newDeleteScanner(resourceDirectory); - setupScanner(resource, scanner, mavenResourcesExecution.isAddDefaultExcludes()); + setupScanner(resource, deleteScanner, mavenResourcesExecution.isAddDefaultExcludes()); - scanner.scan(); + deleteScanner.scan(); - for (String name : scanner.getIncludedFiles()) { + for (String name : deleteScanner.getIncludedFiles()) { File destinationFile = getDestinationFile(outputDirectory, targetPath, name, mavenResourcesExecution); destinationFile.delete();