This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git

commit e2311974b5d70b67b66593551a76cd36ef20304e
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 14:14:55 2026 +0100

    Detect case-only and symlinked duplicates in failOnDuplicates
    
    Keys use the real path of the output directory, and a target that exists on 
disk is also looked up by its real path, which finds names that differ only by 
case on case-insensitive file systems. Keys are not case-folded, which would be 
wrong on Linux.
---
 .../dependency/fromConfiguration/UnpackMojo.java   |  8 +++-
 .../fromDependencies/UnpackDependenciesMojo.java   |  8 +++-
 .../dependency/utils/DuplicateTargetTracker.java   | 36 ++++++++++------
 .../maven/plugins/dependency/utils/UnpackUtil.java | 17 ++++++--
 src/site/markdown/faq.md                           |  9 +++-
 .../TestUnpackFailOnDuplicates.java                | 48 ++++++++++++++++++++++
 .../TestUnpackDependenciesFailOnDuplicates.java    |  1 +
 7 files changed, 107 insertions(+), 20 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
index 424b71ad..a18eba06 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/UnpackMojo.java
@@ -119,10 +119,16 @@ public class UnpackMojo extends 
AbstractFromConfigurationMojo {
      * after all artifacts are processed, naming each path and the entries 
that target it. This also applies with
      * {@code overWriteFiles} set to {@code false}. The marker files are then 
not written, so a repeated execution
      * unpacks again.
+     * Names that differ only by case are found on file systems that ignore 
case, such as on macOS and Windows, and so
+     * are paths that reach the output directory through a symbolic link. 
Every jar delivers {@code META-INF/MANIFEST.MF},
+     * and often {@code module-info.class} and {@code META-INF/LICENSE} and 
{@code NOTICE}, so with
+     * {@code unpack-dependencies} use excludes such as {@code 
META-INF/**,module-info.class}.
      * <p>
      * Files that already exist in the output directory before the execution 
are not duplicates; use
      * {@code overWriteFiles} for those. Artifacts skipped because they are 
already unpacked (see the marker files)
-     * are not compared, and the option has no effect on single-file 
compression formats such as gzip.
+     * are not compared, and the option has no effect on single-file 
compression formats such as gzip. With the
+     * option set, a failed execution leaves none of its artifacts marked, as 
a repeated execution has to detect the
+     * duplicate again.
      * </p>
      *
      * @since 3.12.0
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
index fb2373f0..a5798857 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/UnpackDependenciesMojo.java
@@ -116,10 +116,16 @@ public class UnpackDependenciesMojo extends 
AbstractFromDependenciesMojo {
      * after all artifacts are processed, naming each path and the entries 
that target it. This also applies with
      * {@code overWriteFiles} set to {@code false}. The marker files are then 
not written, so a repeated execution
      * unpacks again.
+     * Names that differ only by case are found on file systems that ignore 
case, such as on macOS and Windows, and so
+     * are paths that reach the output directory through a symbolic link. 
Every jar delivers {@code META-INF/MANIFEST.MF},
+     * and often {@code module-info.class} and {@code META-INF/LICENSE} and 
{@code NOTICE}, so with
+     * {@code unpack-dependencies} use excludes such as {@code 
META-INF/**,module-info.class}.
      * <p>
      * Files that already exist in the output directory before the execution 
are not duplicates; use
      * {@code overWriteFiles} for those. Artifacts skipped because they are 
already unpacked (see the marker files)
-     * are not compared, and the option has no effect on single-file 
compression formats such as gzip.
+     * are not compared, and the option has no effect on single-file 
compression formats such as gzip. With the
+     * option set, a failed execution leaves none of its artifacts marked, as 
a repeated execution has to detect the
+     * duplicate again.
      * </p>
      *
      * @since 3.12.0
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/utils/DuplicateTargetTracker.java
 
b/src/main/java/org/apache/maven/plugins/dependency/utils/DuplicateTargetTracker.java
index 4040eb60..b30f21c4 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/utils/DuplicateTargetTracker.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/utils/DuplicateTargetTracker.java
@@ -19,6 +19,9 @@
 package org.apache.maven.plugins.dependency.utils;
 
 import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -35,21 +38,35 @@ import org.apache.maven.plugin.MojoFailureException;
  */
 public class DuplicateTargetTracker {
 
-    private final Map<File, List<String>> sources = new LinkedHashMap<>();
+    private final Map<Path, List<String>> sources = new LinkedHashMap<>();
 
-    private final Map<File, List<String>> duplicates = new LinkedHashMap<>();
+    private final Map<Path, List<String>> duplicates = new LinkedHashMap<>();
 
     /**
-     * Registers a target path.
+     * Registers a target path. Paths are compared as given, after 
normalization, so the caller should pass the
+     * real path of the output directory. In addition, when the target already 
exists on disk, its real path is
+     * looked up, which finds names that differ only by case on a 
case-insensitive file system and paths reached
+     * through symbolic links. Names are not case-folded, as that would be 
wrong on a case-sensitive file system.
      *
-     * @param target the absolute target path, after the file mappers were 
applied
+     * @param target the target path, after the file mappers were applied
      * @param source describes where the entry comes from, for the error 
message
      * @return {@code true} if no other entry of this execution targets the 
same path; {@code false} if it is a
      *         duplicate, which is then reported by {@link #verify()}
      */
     public boolean register(File target, String source) {
-        File key = target.toPath().toAbsolutePath().normalize().toFile();
+        Path key = target.toPath().toAbsolutePath().normalize();
         List<String> existing = sources.get(key);
+        if (existing == null && Files.exists(key)) {
+            try {
+                Path real = key.toRealPath();
+                existing = sources.get(real);
+                if (existing != null) {
+                    key = real;
+                }
+            } catch (IOException e) {
+                // cannot resolve: treat as a new path
+            }
+        }
         if (existing == null) {
             List<String> list = new ArrayList<>();
             list.add(source);
@@ -61,13 +78,6 @@ public class DuplicateTargetTracker {
         return false;
     }
 
-    /**
-     * @return {@code true} if at least one duplicate was registered
-     */
-    public boolean hasDuplicates() {
-        return !duplicates.isEmpty();
-    }
-
     /**
      * @throws MojoFailureException if at least one duplicate was registered, 
listing each path with all its sources
      */
@@ -76,7 +86,7 @@ public class DuplicateTargetTracker {
             return;
         }
         StringBuilder msg = new StringBuilder("Several entries are unpacked to 
the same path (failOnDuplicates):");
-        for (Map.Entry<File, List<String>> entry : duplicates.entrySet()) {
+        for (Map.Entry<Path, List<String>> entry : duplicates.entrySet()) {
             msg.append(System.lineSeparator())
                     .append("  ")
                     .append(entry.getKey())
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java 
b/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java
index a5049ff2..a8681804 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/utils/UnpackUtil.java
@@ -23,8 +23,10 @@ import javax.inject.Named;
 import javax.inject.Singleton;
 
 import java.io.File;
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.LinkOption;
+import java.nio.file.Path;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.logging.Log;
@@ -313,6 +315,8 @@ public class UnpackUtil {
             unArchiver.extract();
         } catch (NoSuchArchiverException e) {
             throw new MojoExecutionException("Unknown archiver type", e);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Cannot resolve the location to 
write unpacked files to: " + location, e);
         } catch (ArchiverException e) {
             throw new MojoExecutionException("Error unpacking file: " + file + 
" to: " + location, e);
         }
@@ -362,7 +366,7 @@ public class UnpackUtil {
      * mappers are applied here as well.
      */
     private static final class DuplicateFileSelector implements FileSelector {
-        private final File location;
+        private final Path location;
 
         private final FileMapper[] fileMappers;
 
@@ -370,8 +374,11 @@ public class UnpackUtil {
 
         private final String source;
 
-        DuplicateFileSelector(File location, FileMapper[] fileMappers, 
DuplicateTargetTracker tracker, String source) {
-            this.location = location;
+        DuplicateFileSelector(File location, FileMapper[] fileMappers, 
DuplicateTargetTracker tracker, String source)
+                throws IOException {
+            // The location exists by now. Its real path makes the same 
directory reached by different paths, such as
+            // through a symbolic link, give the same keys.
+            this.location = location.toPath().toRealPath();
             this.fileMappers = fileMappers;
             this.tracker = tracker;
             this.source = source;
@@ -383,7 +390,9 @@ public class UnpackUtil {
                 return true;
             }
             String name = fileInfo.getName();
-            return tracker.register(new File(location, mappedName(name, 
fileMappers)), source + " (" + name + ")");
+            File target =
+                    location.resolve(mappedName(name, 
fileMappers)).normalize().toFile();
+            return tracker.register(target, source + " (" + name + ")");
         }
     }
 
diff --git a/src/site/markdown/faq.md b/src/site/markdown/faq.md
index 39075566..133e842b 100644
--- a/src/site/markdown/faq.md
+++ b/src/site/markdown/faq.md
@@ -31,7 +31,8 @@ under the License.
 4. [Why is Maven resolving &quot;dependency:xxx&quot; to the older 
org.codehaus.mojo:dependency-maven-plugin?](#question)
 5. [Why am I having trouble unpacking only a specific file?](#includes)
 6. [How do I keep files that already exist in the output directory when 
unpacking?](#overwritefiles)
-7. [Why does my dependency analysis report `Unused declared 
dependencies`?](#unused)
+7. [How do I make unpacking fail when two archives contain the same 
file?](#failonduplicates)
+8. [Why does my dependency analysis report `Unused declared 
dependencies`?](#unused)
 
 <a id="plugin_name"></a>
 
@@ -112,6 +113,12 @@ with the entries that target it. The comparison is made 
after any `fileMappers`
 that a mapper moves to the same path, and a second entry for one path within a 
single archive. Directories are not
 compared.
 
+Every jar contains `META-INF/MANIFEST.MF`, and many contain 
`module-info.class` and `META-INF/LICENSE` or `NOTICE`, so
+`unpack-dependencies` on several jars reports those. Exclude them, for example 
with
+`<excludes>META-INF/**,module-info.class</excludes>`. Names that differ only 
by case, such as `A.txt` and `a.txt`, are
+also found on file systems that ignore case (macOS and Windows by default), 
where they would replace each other, and
+so are paths that reach the output directory through a symbolic link.
+
 Only the first entry for a path is written; later ones are skipped, also when 
`overWriteFiles` is `false`. The marker
 files are not written for a failed execution, so running it again unpacks 
again and fails again. Files that already
 exist in the output directory before the execution are not duplicates: use 
`overWriteFiles` to decide about those.
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackFailOnDuplicates.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackFailOnDuplicates.java
index 0803038f..304f50e0 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackFailOnDuplicates.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackFailOnDuplicates.java
@@ -24,12 +24,14 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -46,6 +48,8 @@ import 
org.codehaus.plexus.components.io.filemappers.FileMapper;
 import org.codehaus.plexus.components.io.filemappers.FlattenFileMapper;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
 import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -107,6 +111,9 @@ class TestUnpackFailOnDuplicates {
         }
         String content = new String(bytes.toByteArray(), 
StandardCharsets.ISO_8859_1);
         for (Map.Entry<String, String> placeholder : placeholders.entrySet()) {
+            // the local header and the central directory
+            int count = content.split(Pattern.quote(placeholder.getKey()), 
-1).length - 1;
+            assertEquals(2, count, placeholder.getKey());
             content = content.replace(placeholder.getKey(), 
placeholder.getValue());
         }
         File zip = new File(tempDir, zipName);
@@ -225,6 +232,7 @@ class TestUnpackFailOnDuplicates {
         mojo.setFailOnDuplicates(true);
         assertDoesNotThrow(mojo::execute);
         assertEquals("y", read(mojo.getOutputDirectory(), "y/a.txt"));
+        assertEquals(1, markerCount(mojo));
     }
 
     @Test
@@ -241,4 +249,44 @@ class TestUnpackFailOnDuplicates {
         assertDoesNotThrow(mojo::execute);
         assertEquals("from two", read(new File(tempDir, "out-two"), 
"same.txt"));
     }
+
+    @Test
+    @InjectMojo(goal = "unpack")
+    void 
testNamesDifferingByCaseFailOnlyOnCaseInsensitiveFileSystem(UnpackMojo mojo) 
throws Exception {
+        Path probe = tempDir.toPath().resolve("Probe.txt");
+        Files.write(probe, new byte[0]);
+        boolean caseInsensitive = 
Files.exists(tempDir.toPath().resolve("probe.txt"))
+                && Files.isSameFile(probe, 
tempDir.toPath().resolve("probe.txt"));
+        artifact("one", zip("one.zip", "A.txt", "upper", "a.txt", "lower"));
+        mojo.setArtifactItems(Collections.singletonList(item("one")));
+        mojo.setFailOnDuplicates(true);
+        if (caseInsensitive) {
+            MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+            assertTrue(e.getMessage().contains("A.txt"), e.getMessage());
+            assertTrue(e.getMessage().contains("(a.txt)"), e.getMessage());
+        } else {
+            // on a case-sensitive file system these are two files
+            assertDoesNotThrow(mojo::execute);
+            assertEquals("upper", read(mojo.getOutputDirectory(), "A.txt"));
+            assertEquals("lower", read(mojo.getOutputDirectory(), "a.txt"));
+        }
+    }
+
+    @Test
+    @DisabledOnOs(OS.WINDOWS)
+    @InjectMojo(goal = "unpack")
+    void testOutputDirectoryReachedThroughSymbolicLinkIsTheSame(UnpackMojo 
mojo) throws Exception {
+        Path real = 
Files.createDirectories(tempDir.toPath().resolve("real-out"));
+        Path link = 
Files.createSymbolicLink(tempDir.toPath().resolve("link-out"), real);
+        artifact("one", zip("one.zip", "same.txt", "from one"));
+        artifact("two", zip("two.zip", "same.txt", "from two"));
+        ArtifactItem one = item("one");
+        one.setOutputDirectory(real.toFile());
+        ArtifactItem two = item("two");
+        two.setOutputDirectory(link.toFile());
+        mojo.setArtifactItems(Arrays.asList(one, two));
+        mojo.setFailOnDuplicates(true);
+        MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+        assertTrue(e.getMessage().contains("same.txt"), e.getMessage());
+    }
 }
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesFailOnDuplicates.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesFailOnDuplicates.java
index 60acb1e8..cd6d1820 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesFailOnDuplicates.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesFailOnDuplicates.java
@@ -109,6 +109,7 @@ class TestUnpackDependenciesFailOnDuplicates {
         mojo.setFailOnDuplicates(true);
         mojo.setUseSubDirectoryPerArtifact(true);
         assertDoesNotThrow(mojo::execute);
+        assertTrue(markerCount(mojo) > 1);
     }
 
     @Test

Reply via email to