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 e47b2723dff2c7c38bd93122f8803017923b3d59
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 01:17:07 2026 +0100

    Keep every existing file with overWriteFiles=false
---
 .../mdep-480-unpack-overwrite-files/cli.properties | 18 ++++++
 .../invoker.properties                             | 18 ++++++
 .../mdep-480-unpack-overwrite-files/pom.xml        | 64 +++-------------------
 .../mdep-480-unpack-overwrite-files/setup.groovy   |  7 +--
 .../mdep-480-unpack-overwrite-files/verify.groovy  | 10 +---
 .../dependency/fromConfiguration/ArtifactItem.java |  6 +-
 .../dependency/fromConfiguration/UnpackMojo.java   | 19 +++----
 .../fromDependencies/UnpackDependenciesMojo.java   | 19 +++----
 .../maven/plugins/dependency/utils/UnpackUtil.java | 61 +++++++++++++++++++--
 src/site/markdown/faq.md                           | 18 +++---
 .../TestUnpackOverWriteFiles.java                  | 47 +++++++++++++++-
 .../TestUnpackDependenciesOverWriteFiles.java      | 24 +++++++-
 12 files changed, 203 insertions(+), 108 deletions(-)

diff --git a/src/it/projects/mdep-480-unpack-overwrite-files/cli.properties 
b/src/it/projects/mdep-480-unpack-overwrite-files/cli.properties
new file mode 100644
index 00000000..f27aa015
--- /dev/null
+++ b/src/it/projects/mdep-480-unpack-overwrite-files/cli.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+mdep.unpack.overWriteFiles=false
diff --git a/src/it/projects/mdep-480-unpack-overwrite-files/invoker.properties 
b/src/it/projects/mdep-480-unpack-overwrite-files/invoker.properties
new file mode 100644
index 00000000..9eba3563
--- /dev/null
+++ b/src/it/projects/mdep-480-unpack-overwrite-files/invoker.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.userPropertiesFile = cli.properties
diff --git a/src/it/projects/mdep-480-unpack-overwrite-files/pom.xml 
b/src/it/projects/mdep-480-unpack-overwrite-files/pom.xml
index a7e13272..c42bbf00 100644
--- a/src/it/projects/mdep-480-unpack-overwrite-files/pom.xml
+++ b/src/it/projects/mdep-480-unpack-overwrite-files/pom.xml
@@ -26,7 +26,7 @@ under the License.
     <version>1.0-SNAPSHOT</version>
     <packaging>pom</packaging>
 
-    <description>Unpacking onto an existing file keeps it with 
overWriteFiles=false and replaces it by default</description>
+    <description>Unpacking onto an existing file keeps it when 
-Dmdep.unpack.overWriteFiles=false is set on the command line</description>
 
     <dependencies>
         <dependency>
@@ -44,67 +44,18 @@ under the License.
                 <version>@project.version@</version>
                 <executions>
                     <execution>
-                        <id>unpack-dependencies-default</id>
+                        <id>unpack-dependencies</id>
                         <phase>process-sources</phase>
                         <goals>
                             <goal>unpack-dependencies</goal>
                         </goals>
                         <configuration>
-                            
<outputDirectory>${basedir}/dependencies-default</outputDirectory>
-                            
<markersDirectory>${project.build.directory}/markers-dependencies-default</markersDirectory>
+                            
<outputDirectory>${basedir}/dependencies</outputDirectory>
+                            
<markersDirectory>${project.build.directory}/markers-dependencies</markersDirectory>
                         </configuration>
                     </execution>
                     <execution>
-                        <id>unpack-dependencies-keep</id>
-                        <phase>process-sources</phase>
-                        <goals>
-                            <goal>unpack-dependencies</goal>
-                        </goals>
-                        <configuration>
-                            <overWriteFiles>false</overWriteFiles>
-                            
<outputDirectory>${basedir}/dependencies-keep</outputDirectory>
-                            
<markersDirectory>${project.build.directory}/markers-dependencies-keep</markersDirectory>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>unpack-default</id>
-                        <phase>process-sources</phase>
-                        <goals>
-                            <goal>unpack</goal>
-                        </goals>
-                        <configuration>
-                            <artifactItems>
-                                <artifactItem>
-                                    
<groupId>org.apache.maven.its.dependency</groupId>
-                                    <artifactId>ResourceArtifact</artifactId>
-                                    <version>1.0</version>
-                                </artifactItem>
-                            </artifactItems>
-                            
<outputDirectory>${basedir}/unpack-default</outputDirectory>
-                            
<markersDirectory>${project.build.directory}/markers-unpack-default</markersDirectory>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>unpack-keep</id>
-                        <phase>process-sources</phase>
-                        <goals>
-                            <goal>unpack</goal>
-                        </goals>
-                        <configuration>
-                            <artifactItems>
-                                <artifactItem>
-                                    
<groupId>org.apache.maven.its.dependency</groupId>
-                                    <artifactId>ResourceArtifact</artifactId>
-                                    <version>1.0</version>
-                                </artifactItem>
-                            </artifactItems>
-                            <overWriteFiles>false</overWriteFiles>
-                            
<outputDirectory>${basedir}/unpack-keep</outputDirectory>
-                            
<markersDirectory>${project.build.directory}/markers-unpack-keep</markersDirectory>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>unpack-item-keep</id>
+                        <id>unpack</id>
                         <phase>process-sources</phase>
                         <goals>
                             <goal>unpack</goal>
@@ -115,11 +66,10 @@ under the License.
                                     
<groupId>org.apache.maven.its.dependency</groupId>
                                     <artifactId>ResourceArtifact</artifactId>
                                     <version>1.0</version>
-                                    <overWriteFiles>false</overWriteFiles>
                                 </artifactItem>
                             </artifactItems>
-                            
<outputDirectory>${basedir}/unpack-item-keep</outputDirectory>
-                            
<markersDirectory>${project.build.directory}/markers-unpack-item-keep</markersDirectory>
+                            
<outputDirectory>${basedir}/unpack</outputDirectory>
+                            
<markersDirectory>${project.build.directory}/markers-unpack</markersDirectory>
                         </configuration>
                     </execution>
                 </executions>
diff --git a/src/it/projects/mdep-480-unpack-overwrite-files/setup.groovy 
b/src/it/projects/mdep-480-unpack-overwrite-files/setup.groovy
index 86160b00..7ab33415 100644
--- a/src/it/projects/mdep-480-unpack-overwrite-files/setup.groovy
+++ b/src/it/projects/mdep-480-unpack-overwrite-files/setup.groovy
@@ -17,15 +17,14 @@
  * under the License.
  */
 
-// resource1.txt exists before the plugin runs and is newer than the archive 
entry (a filtered resource, say);
+// resource1.txt exists before the plugin runs and is OLDER than the archive 
entry: it must be kept all the same;
 // the ITs run "clean" first, so the directories are outside of target
-def now = System.currentTimeMillis()
-for ( dir in ["dependencies-default", "dependencies-keep", "unpack-default", 
"unpack-keep", "unpack-item-keep"] )
+for ( dir in ["dependencies", "unpack"] )
 {
     File existing = new File( basedir, dir + "/resource1.txt" )
     existing.parentFile.mkdirs()
     existing.text = "existing"
-    existing.setLastModified( now + 24L * 60 * 60 * 1000 )
+    existing.setLastModified( 1000000000000L )
 }
 
 return true
diff --git a/src/it/projects/mdep-480-unpack-overwrite-files/verify.groovy 
b/src/it/projects/mdep-480-unpack-overwrite-files/verify.groovy
index babfebc0..6ec8d3cf 100644
--- a/src/it/projects/mdep-480-unpack-overwrite-files/verify.groovy
+++ b/src/it/projects/mdep-480-unpack-overwrite-files/verify.groovy
@@ -17,14 +17,8 @@
  * under the License.
  */
 
-// overWriteFiles decides per file; resource2.txt did not exist and is 
extracted in every case
-for ( dir in ["dependencies-default", "unpack-default"] )
-{
-    assert new File( basedir, dir + "/resource1.txt" ).text.contains( 
"Resource 1" ) : dir + " should be replaced"
-    assert new File( basedir, dir + "/resource2.txt" ).isFile()
-}
-
-for ( dir in ["dependencies-keep", "unpack-keep", "unpack-item-keep"] )
+// -Dmdep.unpack.overWriteFiles=false from cli.properties reaches both goals; 
resource2.txt did not exist
+for ( dir in ["dependencies", "unpack"] )
 {
     assert new File( basedir, dir + "/resource1.txt" ).text == "existing" : 
dir + " should be kept"
     assert new File( basedir, dir + "/resource2.txt" ).isFile()
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
index fb38f7ac..15182838 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ArtifactItem.java
@@ -100,8 +100,10 @@ public class ArtifactItem {
 
     /**
      * Whether unpacking may replace files that already exist in the output 
directory, for this item only. Overrides
-     * the {@code overWriteFiles} parameter of the {@code unpack} goal; {@code 
null} means the goal's value applies.
-     * Used by the {@code unpack} goal only.
+     * the {@code overWriteFiles} parameter of the {@code unpack} goal, in 
either direction; {@code null} means the
+     * goal's value applies. With {@code false}, no file that already exists 
at the target path is replaced. Unlike
+     * {@link #getOverWrite() overWrite}, which decides per artifact whether 
it is unpacked at all, this applies to
+     * the files of an artifact that is unpacked. Used by the {@code unpack} 
goal only.
      *
      * @since 3.12.0
      */
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 955fa902..129ad3a8 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
@@ -90,17 +90,16 @@ public class UnpackMojo extends 
AbstractFromConfigurationMojo {
     private boolean useDefaultExcludes = true;
 
     /**
-     * Whether unpacking may replace files that already exist in the output 
directory. This is decided for each file,
-     * independently of the per-artifact {@code overWriteReleases}, {@code 
overWriteSnapshots} and
-     * {@code overWriteIfNewer} parameters and of the marker files: those 
decide whether an artifact is unpacked at
-     * all, this one decides what happens to a file that is already there once 
the artifact is unpacked.
+     * Whether unpacking may replace files that already exist in the output 
directory. This is decided for each file.
+     * With {@code true} (the default) every entry of the archive is written. 
With {@code false} an entry is skipped
+     * whenever a file already exists at its target path, after the {@code 
fileMappers} have been applied, however old
+     * that file is; only the missing files are written. Directories are not 
affected. The option has no effect on
+     * single-file compression formats such as gzip.
      * <p>
-     * With {@code true} (the default) every archive entry is written, as 
before. With {@code false} the unarchiver
-     * ({@code plexus-archiver}) writes an entry only if there is no file of 
that name yet, or if the existing file's
-     * last modified time is older than the entry's time stored in the 
archive. An existing file that is as new as or
-     * newer than the archive entry is kept. This means that a file created or 
filtered after the archive was built
-     * is kept, whereas an older file is still replaced. Entries are compared 
by the archive's own timestamp, so
-     * archives that carry a reproducible build timestamp are compared against 
that timestamp.
+     * This differs from {@code overWriteReleases}, {@code overWriteSnapshots} 
and {@code overWriteIfNewer}, and from
+     * the {@code overWrite} of an {@code artifactItem}, which are decided per 
artifact (together with the marker
+     * files) and only determine whether an artifact is unpacked at all. 
{@code overWriteFiles} applies to the files of
+     * an artifact that is unpacked.
      * </p>
      *
      * Can also be set for a single {@code artifactItem}, which then takes 
precedence.
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 669c9946..8ccb6629 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
@@ -87,17 +87,16 @@ public class UnpackDependenciesMojo extends 
AbstractFromDependenciesMojo {
     private boolean useDefaultExcludes = true;
 
     /**
-     * Whether unpacking may replace files that already exist in the output 
directory. This is decided for each file,
-     * independently of the per-artifact {@code overWriteReleases}, {@code 
overWriteSnapshots} and
-     * {@code overWriteIfNewer} parameters and of the marker files: those 
decide whether an artifact is unpacked at
-     * all, this one decides what happens to a file that is already there once 
the artifact is unpacked.
+     * Whether unpacking may replace files that already exist in the output 
directory. This is decided for each file.
+     * With {@code true} (the default) every entry of the archive is written. 
With {@code false} an entry is skipped
+     * whenever a file already exists at its target path, after the {@code 
fileMappers} have been applied, however old
+     * that file is; only the missing files are written. Directories are not 
affected. The option has no effect on
+     * single-file compression formats such as gzip.
      * <p>
-     * With {@code true} (the default) every archive entry is written, as 
before. With {@code false} the unarchiver
-     * ({@code plexus-archiver}) writes an entry only if there is no file of 
that name yet, or if the existing file's
-     * last modified time is older than the entry's time stored in the 
archive. An existing file that is as new as or
-     * newer than the archive entry is kept. This means that a file created or 
filtered after the archive was built
-     * is kept, whereas an older file is still replaced. Entries are compared 
by the archive's own timestamp, so
-     * archives that carry a reproducible build timestamp are compared against 
that timestamp.
+     * This differs from {@code overWriteReleases}, {@code overWriteSnapshots} 
and {@code overWriteIfNewer}, and from
+     * the {@code overWrite} of an {@code artifactItem}, which are decided per 
artifact (together with the marker
+     * files) and only determine whether an artifact is unpacked at all. 
{@code overWriteFiles} applies to the files of
+     * an artifact that is unpacked.
      * </p>
      *
      * @since 3.12.0
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 e3300790..89ac176b 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
@@ -32,6 +32,8 @@ import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
 import org.codehaus.plexus.components.io.filemappers.FileMapper;
+import org.codehaus.plexus.components.io.fileselectors.FileInfo;
+import org.codehaus.plexus.components.io.fileselectors.FileSelector;
 import 
org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
@@ -145,8 +147,9 @@ public class UnpackUtil {
      * @param excludes          excludes list
      * @param useDefaultExcludes whether the default excludes (such as {@code 
.svn} and {@code .git}) are applied
      *                          when excludes are set
-     * @param overWriteFiles    {@code false} to keep an existing file that is 
not older than its archive entry, see
-     *                          {@link UnArchiver#setOverwrite(boolean)}
+     * @param overWriteFiles    {@code false} to keep every file that already 
exists in the location, whatever its
+     *                          timestamp; not applied to single-file 
compression formats such as gzip, whose
+     *                          unarchivers do not consult file selectors
      * @param encoding          the encoding
      * @param ignorePermissions ignore permissions
      * @param fileMappers       {@link FileMapper}s to be used for rewriting 
each target path, or {@code null} if no
@@ -200,12 +203,11 @@ public class UnpackUtil {
 
             unArchiver.setIgnorePermissions(ignorePermissions);
 
-            unArchiver.setOverwrite(overWriteFiles);
-
             unArchiver.setSourceFile(file);
 
             unArchiver.setDestDirectory(location);
 
+            FileSelector[] fileSelectors = new FileSelector[0];
             if ((excludes != null && !excludes.isEmpty()) || (includes != null 
&& !includes.isEmpty())) {
                 // Create the selectors that will filter
                 // based on include/exclude parameters
@@ -225,7 +227,19 @@ public class UnpackUtil {
                     selectors[0].setIncludes(includes.split(","));
                 }
 
-                unArchiver.setFileSelectors(selectors);
+                fileSelectors = selectors;
+            }
+
+            if (!overWriteFiles) {
+                // plexus-archiver's own overwrite=false still replaces a file 
that is older than its entry, so keep
+                // existing files with a selector. It is AND-ed with the 
include/exclude selector.
+                FileSelector[] all = new FileSelector[fileSelectors.length + 
1];
+                System.arraycopy(fileSelectors, 0, all, 0, 
fileSelectors.length);
+                all[fileSelectors.length] = new 
KeepExistingFileSelector(location, fileMappers, logger);
+                fileSelectors = all;
+            }
+            if (fileSelectors.length > 0) {
+                unArchiver.setFileSelectors(fileSelectors);
             }
 
             unArchiver.setFileMappers(fileMappers);
@@ -266,4 +280,41 @@ public class UnpackUtil {
             logger.debug(msg.toString());
         }
     }
+
+    /**
+     * Rejects a non-directory entry whose target, after the file mappers are 
applied, already exists. The
+     * unarchiver asks selectors before it maps the entry name, so the mappers 
are applied here as well.
+     */
+    private static final class KeepExistingFileSelector implements 
FileSelector {
+        private final File location;
+
+        private final FileMapper[] fileMappers;
+
+        private final Log logger;
+
+        KeepExistingFileSelector(File location, FileMapper[] fileMappers, Log 
logger) {
+            this.location = location;
+            this.fileMappers = fileMappers;
+            this.logger = logger;
+        }
+
+        @Override
+        public boolean isSelected(FileInfo fileInfo) {
+            if (fileInfo.isDirectory()) {
+                return true;
+            }
+            String name = fileInfo.getName();
+            if (fileMappers != null) {
+                for (FileMapper fileMapper : fileMappers) {
+                    name = fileMapper.getMappedFileName(name);
+                }
+            }
+            File target = new File(location, name);
+            if (target.exists()) {
+                logger.debug("Keeping existing file " + target);
+                return false;
+            }
+            return true;
+        }
+    }
 }
diff --git a/src/site/markdown/faq.md b/src/site/markdown/faq.md
index b0f6e289..57b9c289 100644
--- a/src/site/markdown/faq.md
+++ b/src/site/markdown/faq.md
@@ -30,7 +30,8 @@ under the License.
 3. [Why am I getting errors that a documented goal or parameter is 
missing?](#missing)
 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. [Why does my dependency analysis report `Unused declared 
dependencies`?](#unused)
+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)
 
 <a id="plugin_name"></a>
 
@@ -91,13 +92,14 @@ Set `useDefaultExcludes` to `false` to unpack those files 
too.
 
 ### How do I keep files that already exist in the output directory when 
unpacking?
 
-`overWriteReleases`, `overWriteSnapshots`, `overWriteIfNewer` and the marker 
files decide whether an artifact is
-unpacked at all. Once it is, `unpack` and `unpack-dependencies` write every 
entry of the archive. Set
-`overWriteFiles` (user property `mdep.unpack.overWriteFiles`) to `false` to 
decide per file instead: an entry is
-written only if no file of that name exists yet, or if the existing file is 
older than the entry's timestamp in the
-archive. A file that is as new as or newer than the archive entry is kept, for 
example a resource that was filtered
-into the output directory after the archive was built. Because an older 
existing file is still replaced, this is not
-a guarantee that no file is ever touched. For `unpack`, the parameter can also 
be set on a single `artifactItem`.
+Set `overWriteFiles` (user property `mdep.unpack.overWriteFiles`) to `false` 
on `unpack` or `unpack-dependencies`.
+Each entry of the archive is then written only if no file exists at its target 
path yet, after any `fileMappers` are
+applied. An existing file is never replaced, whatever its timestamp, so a 
resource that was filtered into the output
+directory stays as it is. For `unpack`, the parameter can also be set on a 
single `artifactItem`, where it overrides
+the goal's value in either direction. It has no effect on single-file 
compression formats such as gzip.
+
+This is a per-file rule. `overWriteReleases`, `overWriteSnapshots`, 
`overWriteIfNewer` and the `overWrite` of an
+`artifactItem` work per artifact: together with the marker files they decide 
whether an artifact is unpacked at all.
 
 <a id="unused"></a>
 
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackOverWriteFiles.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackOverWriteFiles.java
index ca865cfa..00dc3523 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackOverWriteFiles.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackOverWriteFiles.java
@@ -36,11 +36,14 @@ import org.apache.maven.artifact.Artifact;
 import org.apache.maven.execution.MavenSession;
 import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
+import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 /**
  * MDEP-480: existing files can be kept when unpacking.
@@ -112,8 +115,8 @@ class TestUnpackOverWriteFiles {
         mojo.setOverWriteFiles(false);
         mojo.execute();
         assertEquals(EXISTING, read(mojo.getOutputDirectory(), 
"existing.txt"));
-        // the per file rule of plexus-archiver still replaces a file that is 
older than its archive entry
-        assertEquals("from archive", read(mojo.getOutputDirectory(), 
"old.txt"));
+        // timestamps do not matter: a file older than its archive entry is 
kept as well
+        assertEquals(EXISTING, read(mojo.getOutputDirectory(), "old.txt"));
         assertEquals("from archive", read(mojo.getOutputDirectory(), 
"added.txt"));
     }
 
@@ -133,4 +136,44 @@ class TestUnpackOverWriteFiles {
         assertEquals(EXISTING, read(mojo.getOutputDirectory(), 
"existing.txt"));
         assertEquals("from archive", read(mojo.getOutputDirectory(), 
"added.txt"));
     }
+
+    @Test
+    @InjectMojo(goal = "unpack")
+    void testMappedTargetIsChecked(UnpackMojo mojo) throws Exception {
+        File mapped = new File(mojo.getOutputDirectory(), "sub");
+        createExisting(mapped);
+        PrefixFileMapper mapper = new PrefixFileMapper();
+        mapper.setPrefix("sub/");
+        ArtifactItem item = new ArtifactItem();
+        item.setGroupId("test");
+        item.setArtifactId("test");
+        item.setVersion("1.0");
+        item.setType("jar");
+        item.setFileMappers(new FileMapper[] {mapper});
+        mojo.setArtifactItems(Collections.singletonList(item));
+        mojo.setOverWriteFiles(false);
+        mojo.execute();
+        assertEquals(EXISTING, read(mapped, "existing.txt"));
+        assertEquals(EXISTING, read(mapped, "old.txt"));
+        assertEquals("from archive", read(mapped, "added.txt"));
+        // nothing was written to the unmapped paths
+        assertFalse(new File(mojo.getOutputDirectory(), "added.txt").exists());
+    }
+
+    @Test
+    @InjectMojo(goal = "unpack")
+    void testArtifactItemTrueOverridesMojoFalse(UnpackMojo mojo) throws 
Exception {
+        ArtifactItem item = new ArtifactItem();
+        item.setGroupId("test");
+        item.setArtifactId("test");
+        item.setVersion("1.0");
+        item.setType("jar");
+        item.setOverWriteFiles(true);
+        mojo.setArtifactItems(Collections.singletonList(item));
+        mojo.setOverWriteFiles(false);
+        createExisting(mojo.getOutputDirectory());
+        mojo.execute();
+        assertEquals("from archive", read(mojo.getOutputDirectory(), 
"existing.txt"));
+        assertEquals("from archive", read(mojo.getOutputDirectory(), 
"old.txt"));
+    }
 }
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesOverWriteFiles.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesOverWriteFiles.java
index 6871e7d1..b8e0931b 100644
--- 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesOverWriteFiles.java
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesOverWriteFiles.java
@@ -34,11 +34,14 @@ import org.apache.maven.api.plugin.testing.MojoTest;
 import org.apache.maven.artifact.Artifact;
 import 
org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.components.io.filemappers.FileMapper;
+import org.codehaus.plexus.components.io.filemappers.PrefixFileMapper;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 /**
  * MDEP-480: existing files can be kept when unpacking.
@@ -105,8 +108,25 @@ class TestUnpackDependenciesOverWriteFiles {
         mojo.setOverWriteFiles(false);
         mojo.execute();
         assertEquals(EXISTING, read(mojo.getOutputDirectory(), 
"existing.txt"));
-        // the per file rule of plexus-archiver still replaces a file that is 
older than its archive entry
-        assertEquals("from archive", read(mojo.getOutputDirectory(), 
"old.txt"));
+        // timestamps do not matter: a file older than its archive entry is 
kept as well
+        assertEquals(EXISTING, read(mojo.getOutputDirectory(), "old.txt"));
         assertEquals("from archive", read(mojo.getOutputDirectory(), 
"added.txt"));
     }
+
+    @Test
+    @InjectMojo(goal = "unpack-dependencies")
+    void testMappedTargetIsChecked(UnpackDependenciesMojo mojo) throws 
Exception {
+        File mapped = new File(mojo.getOutputDirectory(), "sub");
+        createExisting(mapped);
+        PrefixFileMapper mapper = new PrefixFileMapper();
+        mapper.setPrefix("sub/");
+        mojo.setFileMappers(new FileMapper[] {mapper});
+        mojo.setOverWriteFiles(false);
+        mojo.execute();
+        assertEquals(EXISTING, read(mapped, "existing.txt"));
+        assertEquals(EXISTING, read(mapped, "old.txt"));
+        assertEquals("from archive", read(mapped, "added.txt"));
+        // nothing was written to the unmapped paths
+        assertFalse(new File(mojo.getOutputDirectory(), "added.txt").exists());
+    }
 }

Reply via email to