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 4b41b45c168c98e7c72925f2824a4b32177a146a Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 13:07:24 2026 +0100 Correct overWriteFiles examples and keep dangling symlinks With overWriteFiles=false a dangling symlink at the target path now counts as an existing file, so the archive entry is not written through it. --- .../dependency/fromConfiguration/ArtifactItem.java | 3 ++- .../dependency/fromConfiguration/UnpackMojo.java | 3 ++- .../fromDependencies/UnpackDependenciesMojo.java | 3 ++- .../maven/plugins/dependency/utils/UnpackUtil.java | 4 +++- .../markdown/examples/unpacking-artifacts.md.vm | 2 +- .../examples/unpacking-project-dependencies.md.vm | 2 +- .../TestUnpackOverWriteFiles.java | 22 ++++++++++++++++++++++ 7 files changed, 33 insertions(+), 6 deletions(-) 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 15182838..9c533e8b 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 @@ -101,7 +101,8 @@ 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, 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 + * goal's value applies. With {@code false}, no file that already exists at the target path is replaced. When several entries or + * artifacts map to the same path, the first one written wins. 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. * 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 129ad3a8..196b9074 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 @@ -93,7 +93,8 @@ public class UnpackMojo extends AbstractFromConfigurationMojo { * 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 + * that file is; only the missing files are written, so when several entries or dependencies map to the same path, + * the first one wins. Directories are not affected. The option has no effect on * single-file compression formats such as gzip. * <p> * This differs from {@code overWriteReleases}, {@code overWriteSnapshots} and {@code overWriteIfNewer}, and from 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 8ccb6629..c2c0d4c0 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 @@ -90,7 +90,8 @@ public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo { * 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 + * that file is; only the missing files are written, so when several entries or dependencies map to the same path, + * the first one wins. Directories are not affected. The option has no effect on * single-file compression formats such as gzip. * <p> * This differs from {@code overWriteReleases}, {@code overWriteSnapshots} and {@code overWriteIfNewer}, and from 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 89ac176b..c8922c8e 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,6 +23,8 @@ import javax.inject.Named; import javax.inject.Singleton; import java.io.File; +import java.nio.file.Files; +import java.nio.file.LinkOption; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; @@ -310,7 +312,7 @@ public class UnpackUtil { } } File target = new File(location, name); - if (target.exists()) { + if (Files.exists(target.toPath(), LinkOption.NOFOLLOW_LINKS)) { logger.debug("Keeping existing file " + target); return false; } diff --git a/src/site/markdown/examples/unpacking-artifacts.md.vm b/src/site/markdown/examples/unpacking-artifacts.md.vm index 174766de..60971d98 100644 --- a/src/site/markdown/examples/unpacking-artifacts.md.vm +++ b/src/site/markdown/examples/unpacking-artifacts.md.vm @@ -118,7 +118,7 @@ you must not put the configuration inside the _executions_ tag. Your configurati <destFileName>[ filename ]</destFileName> <includes>[ comma separated list of file filters ]</includes> <excludes>[ comma separated list of file filters ]</excludes> - <overWriteFiles>[ false to keep existing files that are not older than the archive entry ]</overWriteFiles> + <overWriteFiles>[ false to keep every file that already exists ]</overWriteFiles> </artifactItem> </artifactItems> <!-- other configurations here --> diff --git a/src/site/markdown/examples/unpacking-project-dependencies.md.vm b/src/site/markdown/examples/unpacking-project-dependencies.md.vm index 21266320..9714f282 100644 --- a/src/site/markdown/examples/unpacking-project-dependencies.md.vm +++ b/src/site/markdown/examples/unpacking-project-dependencies.md.vm @@ -55,7 +55,7 @@ To unpack the artifacts, use the `dependency:unpack-dependencies` mojo and confi <outputDirectory>${esc.d}{project.build.directory}/alternateLocation</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> - <!-- per file: keep existing files that are not older than the archive entry --> + <!-- per file: never replace a file that already exists --> <overWriteFiles>false</overWriteFiles> </configuration> </execution> 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 00dc3523..6219cc06 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 @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -38,12 +39,14 @@ import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFacto 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.Assumptions; 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; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * MDEP-480: existing files can be kept when unpacking. @@ -137,6 +140,25 @@ class TestUnpackOverWriteFiles { assertEquals("from archive", read(mojo.getOutputDirectory(), "added.txt")); } + @Test + @InjectMojo(goal = "unpack") + void testDanglingSymlinkCountsAsExisting(UnpackMojo mojo) throws Exception { + Assumptions.assumeFalse(System.getProperty("os.name").toLowerCase().contains("win")); + File out = mojo.getOutputDirectory(); + out.mkdirs(); + Path link = new File(out, "existing.txt").toPath(); + try { + Files.createSymbolicLink(link, new File(tempDir, "does-not-exist").toPath()); + } catch (UnsupportedOperationException | java.io.IOException e) { + Assumptions.abort("symbolic links are not available: " + e); + } + mojo.setOverWriteFiles(false); + mojo.execute(); + assertTrue(Files.isSymbolicLink(link)); + assertFalse(new File(out, "existing.txt").exists()); + assertEquals("from archive", read(out, "added.txt")); + } + @Test @InjectMojo(goal = "unpack") void testMappedTargetIsChecked(UnpackMojo mojo) throws Exception {
