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 e527e4061d6cda5d23cad6cb2d6c87856aa8991d Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 14:04:39 2026 +0100 [MDEP-772] Add failOnDuplicates to unpack and unpack-dependencies Fails after unpacking when two entries of one execution target the same path, after fileMappers, across archives or within one. Files that existed before the execution are not duplicates (that is overWriteFiles). Markers are withheld on failure so a rerun detects it again. --- .../cli.properties | 19 ++ .../invoker.properties | 23 ++ .../mdep-772-unpack-fail-on-duplicates/pom.xml | 89 ++++++++ .../verify.groovy | 29 +++ .../dependency/fromConfiguration/UnpackMojo.java | 54 ++++- .../fromDependencies/UnpackDependenciesMojo.java | 52 ++++- .../dependency/utils/DuplicateTargetTracker.java | 90 ++++++++ .../maven/plugins/dependency/utils/UnpackUtil.java | 112 +++++++++- .../markdown/examples/unpacking-artifacts.md.vm | 2 + .../examples/unpacking-project-dependencies.md.vm | 2 + src/site/markdown/faq.md | 17 ++ .../TestUnpackFailOnDuplicates.java | 244 +++++++++++++++++++++ .../TestUnpackDependenciesFailOnDuplicates.java | 127 +++++++++++ 13 files changed, 848 insertions(+), 12 deletions(-) diff --git a/src/it/projects/mdep-772-unpack-fail-on-duplicates/cli.properties b/src/it/projects/mdep-772-unpack-fail-on-duplicates/cli.properties new file mode 100644 index 00000000..d7880be5 --- /dev/null +++ b/src/it/projects/mdep-772-unpack-fail-on-duplicates/cli.properties @@ -0,0 +1,19 @@ +# 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 +mdep.unpack.failOnDuplicates=true diff --git a/src/it/projects/mdep-772-unpack-fail-on-duplicates/invoker.properties b/src/it/projects/mdep-772-unpack-fail-on-duplicates/invoker.properties new file mode 100644 index 00000000..f66ec992 --- /dev/null +++ b/src/it/projects/mdep-772-unpack-fail-on-duplicates/invoker.properties @@ -0,0 +1,23 @@ +# 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 +# both goals read mdep.unpack.failOnDuplicates; resource1.txt and resource2.txt are mapped to the same path +invoker.goals.1 = clean ${project.groupId}:${project.artifactId}:${project.version}:unpack@unpack +invoker.buildResult.1 = failure +invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:unpack-dependencies@unpack-dependencies +invoker.buildResult.2 = failure diff --git a/src/it/projects/mdep-772-unpack-fail-on-duplicates/pom.xml b/src/it/projects/mdep-772-unpack-fail-on-duplicates/pom.xml new file mode 100644 index 00000000..1c34b0d6 --- /dev/null +++ b/src/it/projects/mdep-772-unpack-fail-on-duplicates/pom.xml @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins.dependency.its</groupId> + <artifactId>mdep-772-unpack-fail-on-duplicates</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <description>-Dmdep.unpack.failOnDuplicates=true fails unpack and unpack-dependencies when two entries are mapped to one path</description> + + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>ResourceArtifact</artifactId> + <version>1.0</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <version>@project.version@</version> + <executions> + <execution> + <id>unpack-dependencies</id> + <goals> + <goal>unpack-dependencies</goal> + </goals> + <configuration> + <outputDirectory>${project.build.directory}/dependencies</outputDirectory> + <markersDirectory>${project.build.directory}/markers-dependencies</markersDirectory> + <fileMappers> + <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper> + <pattern>^resource.*\.txt$</pattern> + <replacement>same.txt</replacement> + </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper> + </fileMappers> + </configuration> + </execution> + <execution> + <id>unpack</id> + <goals> + <goal>unpack</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>ResourceArtifact</artifactId> + <version>1.0</version> + <fileMappers> + <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper> + <pattern>^resource.*\.txt$</pattern> + <replacement>same.txt</replacement> + </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper> + </fileMappers> + </artifactItem> + </artifactItems> + <outputDirectory>${project.build.directory}/unpack</outputDirectory> + <markersDirectory>${project.build.directory}/markers-unpack</markersDirectory> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/projects/mdep-772-unpack-fail-on-duplicates/verify.groovy b/src/it/projects/mdep-772-unpack-fail-on-duplicates/verify.groovy new file mode 100644 index 00000000..c88edcdf --- /dev/null +++ b/src/it/projects/mdep-772-unpack-fail-on-duplicates/verify.groovy @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// the first entry was written and the second was not; nothing is marked as unpacked, so a repeated run fails again +for ( dir in ["dependencies", "unpack"] ) +{ + assert new File( basedir, "target/" + dir + "/same.txt" ).isFile() : dir + " should hold the first entry" + assert !new File( basedir, "target/" + dir + "/resource1.txt" ).exists() : dir + " should hold the mapped name only" + File markers = new File( basedir, "target/markers-" + dir ) + assert !markers.exists() || markers.list().length == 0 : dir + " should not be marked as unpacked" +} + +return true 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 196b9074..424b71ad 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 @@ -21,6 +21,7 @@ package org.apache.maven.plugins.dependency.fromConfiguration; import javax.inject.Inject; import java.io.File; +import java.util.ArrayList; import java.util.List; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; @@ -30,6 +31,7 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.dependency.utils.DuplicateTargetTracker; import org.apache.maven.plugins.dependency.utils.ResolverUtil; import org.apache.maven.plugins.dependency.utils.UnpackUtil; import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter; @@ -110,6 +112,24 @@ public class UnpackMojo extends AbstractFromConfigurationMojo { @Parameter(property = "mdep.unpack.overWriteFiles", defaultValue = "true") private boolean overWriteFiles = true; + /** + * Whether to fail when, within this execution, two entries are unpacked to the same path of the output directory: + * from two different artifacts, or twice from one archive. Paths are compared after the {@code fileMappers} have + * been applied; directories are not compared. The first entry is written, a later one is not, and the build fails + * 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. + * <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. + * </p> + * + * @since 3.12.0 + */ + @Parameter(property = "mdep.unpack.failOnDuplicates", defaultValue = "false") + private boolean failOnDuplicates; + /** * Ignore to set file permissions when unpacking a dependency. * @@ -170,24 +190,42 @@ public class UnpackMojo extends AbstractFromConfigurationMojo { verifyRequirements(); verifyExplicitDependencies(); + DuplicateTargetTracker tracker = failOnDuplicates ? new DuplicateTargetTracker() : null; + List<MarkerHandler> handlers = new ArrayList<>(); List<ArtifactItem> processedItems = getProcessedArtifactItems(false); for (ArtifactItem artifactItem : processedItems) { if (artifactItem.isNeedsProcessing()) { - unpackArtifact(artifactItem); + MarkerHandler handler = unpackArtifact(artifactItem, tracker); + if (tracker == null) { + handler.setMarker(); + } else { + // only mark as unpacked once it is known that no entry was skipped as a duplicate + handlers.add(handler); + } } else { this.getLog().info(artifactItem.getArtifact().getFile().getName() + " already unpacked."); } } + + if (tracker != null) { + tracker.verify(); + for (MarkerHandler handler : handlers) { + handler.setMarker(); + } + } } /** * This method gets the Artifact object and calls DependencyUtil.unpackFile. * * @param artifactItem containing the information about the Artifact to unpack + * @param tracker records the unpacked paths, or {@code null} if duplicates are not checked + * @return the handler for the marker file of the artifact, which the caller sets * @throws MojoExecutionException with a message if an error occurs * @see #getArtifact */ - private void unpackArtifact(ArtifactItem artifactItem) throws MojoExecutionException { + private MarkerHandler unpackArtifact(ArtifactItem artifactItem, DuplicateTargetTracker tracker) + throws MojoExecutionException { MarkerHandler handler = new UnpackFileMarkerHandler(artifactItem, this.markersDirectory); unpackUtil.unpack( @@ -201,8 +239,10 @@ public class UnpackMojo extends AbstractFromConfigurationMojo { artifactItem.getEncoding(), ignorePermissions, artifactItem.getFileMappers(), + tracker, + artifactItem.getArtifact().getId(), getLog()); - handler.setMarker(); + return handler; } @Override @@ -268,6 +308,14 @@ public class UnpackMojo extends AbstractFromConfigurationMojo { this.overWriteFiles = overWriteFiles; } + /** + * @param failOnDuplicates whether to fail when several entries are unpacked to the same path + * @since 3.12.0 + */ + public void setFailOnDuplicates(boolean failOnDuplicates) { + this.failOnDuplicates = failOnDuplicates; + } + /** * @param useDefaultExcludes whether the default excludes are applied when excludes are set */ 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 c2c0d4c0..fb2373f0 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 @@ -21,17 +21,21 @@ package org.apache.maven.plugins.dependency.fromDependencies; import javax.inject.Inject; import java.io.File; +import java.util.ArrayList; +import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.plugins.dependency.utils.DependencyStatusSets; import org.apache.maven.plugins.dependency.utils.DependencyUtil; +import org.apache.maven.plugins.dependency.utils.DuplicateTargetTracker; import org.apache.maven.plugins.dependency.utils.ResolverUtil; import org.apache.maven.plugins.dependency.utils.UnpackUtil; import org.apache.maven.plugins.dependency.utils.filters.MarkerFileFilter; @@ -105,6 +109,24 @@ public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo { @Parameter(property = "mdep.unpack.overWriteFiles", defaultValue = "true") private boolean overWriteFiles = true; + /** + * Whether to fail when, within this execution, two entries are unpacked to the same path of the output directory: + * from two different artifacts, or twice from one archive. Paths are compared after the {@code fileMappers} have + * been applied; directories are not compared. The first entry is written, a later one is not, and the build fails + * 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. + * <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. + * </p> + * + * @since 3.12.0 + */ + @Parameter(property = "mdep.unpack.failOnDuplicates", defaultValue = "false") + private boolean failOnDuplicates; + /** * Ignore to set file permissions when unpacking a dependency. * @@ -154,12 +176,16 @@ public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo { * DependencyUtil.unpackFile(). * * @throws MojoExecutionException with a message if an error occurs + * @throws MojoFailureException if {@code failOnDuplicates} is set and several entries target the same path * @see #getDependencySets(boolean) */ @Override - protected void doExecute() throws MojoExecutionException { + protected void doExecute() throws MojoExecutionException, MojoFailureException { DependencyStatusSets dss = getDependencySets(this.failOnMissingClassifierArtifact); + DuplicateTargetTracker tracker = failOnDuplicates ? new DuplicateTargetTracker() : null; + List<DefaultFileMarkerHandler> handlers = new ArrayList<>(); + for (Artifact artifact : dss.getResolvedDependencies()) { File destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerScope, @@ -181,9 +207,23 @@ public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo { getEncoding(), ignorePermissions, getFileMappers(), + tracker, + artifact.getId(), getLog()); DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifact, this.markersDirectory); - handler.setMarker(); + if (tracker == null) { + handler.setMarker(); + } else { + // only mark as unpacked once it is known that no entry was skipped as a duplicate + handlers.add(handler); + } + } + + if (tracker != null) { + tracker.verify(); + for (DefaultFileMarkerHandler handler : handlers) { + handler.setMarker(); + } } for (Artifact artifact : dss.getSkippedDependencies()) { @@ -222,6 +262,14 @@ public class UnpackDependenciesMojo extends AbstractFromDependenciesMojo { this.overWriteFiles = overWriteFiles; } + /** + * @param failOnDuplicates whether to fail when several entries are unpacked to the same path + * @since 3.12.0 + */ + public void setFailOnDuplicates(boolean failOnDuplicates) { + this.failOnDuplicates = failOnDuplicates; + } + /** * @param useDefaultExcludes whether the default excludes are applied when excludes are set */ 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 new file mode 100644 index 00000000..4040eb60 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/DuplicateTargetTracker.java @@ -0,0 +1,90 @@ +/* + * 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. + */ +package org.apache.maven.plugins.dependency.utils; + +import java.io.File; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.maven.plugin.MojoFailureException; + +/** + * Records the files written by one execution of {@code unpack} or {@code unpack-dependencies}, to find entries that + * several archives, or one archive twice, deliver to the same target path. Files that already existed before the + * execution are not known to the tracker. + * + * @since 3.12.0 + */ +public class DuplicateTargetTracker { + + private final Map<File, List<String>> sources = new LinkedHashMap<>(); + + private final Map<File, List<String>> duplicates = new LinkedHashMap<>(); + + /** + * Registers a target path. + * + * @param target the absolute 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(); + List<String> existing = sources.get(key); + if (existing == null) { + List<String> list = new ArrayList<>(); + list.add(source); + sources.put(key, list); + return true; + } + existing.add(source); + duplicates.put(key, existing); + 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 + */ + public void verify() throws MojoFailureException { + if (duplicates.isEmpty()) { + return; + } + StringBuilder msg = new StringBuilder("Several entries are unpacked to the same path (failOnDuplicates):"); + for (Map.Entry<File, List<String>> entry : duplicates.entrySet()) { + msg.append(System.lineSeparator()) + .append(" ") + .append(entry.getKey()) + .append(" <- "); + msg.append(String.join(", ", entry.getValue())); + } + msg.append(System.lineSeparator()) + .append("Only the first entry was written to each path; the others were skipped."); + throw new MojoFailureException(msg.toString()); + } +} 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 c8922c8e..a5049ff2 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 @@ -173,6 +173,61 @@ public class UnpackUtil { FileMapper[] fileMappers, Log logger) throws MojoExecutionException { + unpack( + file, + type, + location, + includes, + excludes, + useDefaultExcludes, + overWriteFiles, + encoding, + ignorePermissions, + fileMappers, + null, + null, + logger); + } + + /** + * @param file file to unpack + * @param type file / artifact type + * @param location the location + * @param includes includes list + * @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 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 + * rewriting shall happen + * @param duplicateTracker if not {@code null}, every non-directory entry that is selected is registered here, + * and an entry whose target path was registered before is not extracted; the caller + * decides when to {@linkplain DuplicateTargetTracker#verify() fail}. Not applied to + * single-file compression formats such as gzip. + * @param source describes the file in messages about duplicates; used with {@code duplicateTracker} + * @param logger a Mojo logger + * @throws MojoExecutionException in case of an error + * @since 3.12.0 + */ + public void unpack( + File file, + String type, + File location, + String includes, + String excludes, + boolean useDefaultExcludes, + boolean overWriteFiles, + String encoding, + boolean ignorePermissions, + FileMapper[] fileMappers, + DuplicateTargetTracker duplicateTracker, + String source, + Log logger) + throws MojoExecutionException { try { logUnpack(logger, file, location, includes, excludes); @@ -232,6 +287,15 @@ public class UnpackUtil { fileSelectors = selectors; } + if (duplicateTracker != null) { + // Before the keep-existing selector, so that an entry is registered even when a file is kept. + FileSelector[] all = new FileSelector[fileSelectors.length + 1]; + System.arraycopy(fileSelectors, 0, all, 0, fileSelectors.length); + all[fileSelectors.length] = new DuplicateFileSelector( + location, fileMappers, duplicateTracker, source != null ? source : file.getName()); + fileSelectors = all; + } + 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. @@ -283,6 +347,46 @@ public class UnpackUtil { } } + private static String mappedName(String name, FileMapper[] fileMappers) { + if (fileMappers != null) { + for (FileMapper fileMapper : fileMappers) { + name = fileMapper.getMappedFileName(name); + } + } + return name; + } + + /** + * Registers every non-directory entry with its target, after the file mappers are applied, and rejects an entry + * whose target was registered before. The unarchiver asks selectors before it maps the entry name, so the + * mappers are applied here as well. + */ + private static final class DuplicateFileSelector implements FileSelector { + private final File location; + + private final FileMapper[] fileMappers; + + private final DuplicateTargetTracker tracker; + + private final String source; + + DuplicateFileSelector(File location, FileMapper[] fileMappers, DuplicateTargetTracker tracker, String source) { + this.location = location; + this.fileMappers = fileMappers; + this.tracker = tracker; + this.source = source; + } + + @Override + public boolean isSelected(FileInfo fileInfo) { + if (fileInfo.isDirectory()) { + return true; + } + String name = fileInfo.getName(); + return tracker.register(new File(location, mappedName(name, fileMappers)), source + " (" + name + ")"); + } + } + /** * 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. @@ -305,13 +409,7 @@ public class UnpackUtil { 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); + File target = new File(location, mappedName(fileInfo.getName(), fileMappers)); 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 60971d98..eedfbfad 100644 --- a/src/site/markdown/examples/unpacking-artifacts.md.vm +++ b/src/site/markdown/examples/unpacking-artifacts.md.vm @@ -121,6 +121,8 @@ you must not put the configuration inside the _executions_ tag. Your configurati <overWriteFiles>[ false to keep every file that already exists ]</overWriteFiles> </artifactItem> </artifactItems> + <!-- fail if two entries are unpacked to the same path --> + <failOnDuplicates>true</failOnDuplicates> <!-- other configurations here --> </configuration> </plugin> diff --git a/src/site/markdown/examples/unpacking-project-dependencies.md.vm b/src/site/markdown/examples/unpacking-project-dependencies.md.vm index 9714f282..193f32c7 100644 --- a/src/site/markdown/examples/unpacking-project-dependencies.md.vm +++ b/src/site/markdown/examples/unpacking-project-dependencies.md.vm @@ -57,6 +57,8 @@ To unpack the artifacts, use the `dependency:unpack-dependencies` mojo and confi <overWriteSnapshots>true</overWriteSnapshots> <!-- per file: never replace a file that already exists --> <overWriteFiles>false</overWriteFiles> + <!-- fail if two entries are unpacked to the same path --> + <failOnDuplicates>true</failOnDuplicates> </configuration> </execution> </executions> diff --git a/src/site/markdown/faq.md b/src/site/markdown/faq.md index 57b9c289..39075566 100644 --- a/src/site/markdown/faq.md +++ b/src/site/markdown/faq.md @@ -101,6 +101,23 @@ the goal's value in either direction. It has no effect on single-file compressio 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="failonduplicates"></a> + +### How do I make unpacking fail when two archives contain the same file? + +Set `failOnDuplicates` (user property `mdep.unpack.failOnDuplicates`) to `true` on `unpack` or `unpack-dependencies`. +When several artifacts are unpacked into one directory, a file that two of them contain is otherwise silently replaced +by the one unpacked last. With the option set, the build fails after all artifacts are processed and names each path +with the entries that target it. The comparison is made after any `fileMappers` are applied, so it also finds entries +that a mapper moves to the same path, and a second entry for one path within a single archive. Directories are not +compared. + +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. +Artifacts that are skipped because they are already unpacked are not compared, and the option has no effect on +single-file compression formats such as gzip. + <a id="unused"></a> ### Why does my dependency analysis report `Unused declared dependencies`? 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 new file mode 100644 index 00000000..0803038f --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestUnpackFailOnDuplicates.java @@ -0,0 +1,244 @@ +/* + * 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. + */ +package org.apache.maven.plugins.dependency.fromConfiguration; + +import javax.inject.Inject; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +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.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoFailureException; +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.FlattenFileMapper; +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.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * MDEP-772: failing when several entries are unpacked to the same path. + */ +@MojoTest(realRepositorySession = true) +@Basedir("/unit/unpack-dependencies-test") +@MojoParameter(name = "artifact", value = "test:one:1.0") +class TestUnpackFailOnDuplicates { + + @TempDir + private File tempDir; + + @Inject + private MavenSession session; + + @Inject + private MavenProject project; + + private DependencyArtifactStubFactory stubFactory; + + @BeforeEach + void setUp() throws Exception { + stubFactory = new DependencyArtifactStubFactory(tempDir, true, false); + session.getRequest().setLocalRepositoryPath(new File(tempDir, "localTestRepo")); + project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath()); + } + + /** + * Creates a zip whose entries are given as name, content pairs. A name may repeat: java.util.zip refuses duplicate + * names, so a repeat is written under a placeholder of the same length that is then replaced in the bytes. + */ + private File zip(String zipName, String... nameAndContent) throws Exception { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + Set<String> seen = new HashSet<>(); + Map<String, String> placeholders = new LinkedHashMap<>(); + try (ZipOutputStream out = new ZipOutputStream(bytes)) { + for (int i = 0; i < nameAndContent.length; i += 2) { + String name = nameAndContent[i]; + String written = name; + if (!seen.add(name)) { + char[] chars = name.toCharArray(); + chars[chars.length - 1] = (char) ('A' + placeholders.size()); + written = new String(chars); + placeholders.put(written, name); + } + out.putNextEntry(new ZipEntry(written)); + if (!name.endsWith("/")) { + out.write(nameAndContent[i + 1].getBytes(StandardCharsets.UTF_8)); + } + out.closeEntry(); + } + } + String content = new String(bytes.toByteArray(), StandardCharsets.ISO_8859_1); + for (Map.Entry<String, String> placeholder : placeholders.entrySet()) { + content = content.replace(placeholder.getKey(), placeholder.getValue()); + } + File zip = new File(tempDir, zipName); + Files.write(zip.toPath(), content.getBytes(StandardCharsets.ISO_8859_1)); + return zip; + } + + private void artifact(String artifactId, File zip) throws Exception { + stubFactory.setSrcFile(zip); + stubFactory.createArtifact("test", artifactId, "1.0", Artifact.SCOPE_COMPILE, "jar", null); + } + + private static ArtifactItem item(String artifactId, FileMapper... mappers) { + ArtifactItem item = new ArtifactItem(); + item.setGroupId("test"); + item.setArtifactId(artifactId); + item.setVersion("1.0"); + item.setType("jar"); + if (mappers.length > 0) { + item.setFileMappers(mappers); + } + return item; + } + + private static String read(File dir, String name) throws Exception { + return new String(Files.readAllBytes(new File(dir, name).toPath()), StandardCharsets.UTF_8); + } + + private static int markerCount(UnpackMojo mojo) { + String[] markers = mojo.getMarkersDirectory().list(); + return markers == null ? 0 : markers.length; + } + + private void twoArtifactsWithSameEntry(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "dir/", "", "same.txt", "from one", "only-one.txt", "one")); + artifact("two", zip("two.zip", "dir/", "", "same.txt", "from two", "only-two.txt", "two")); + mojo.setArtifactItems(Arrays.asList(item("one"), item("two"))); + } + + @Test + @InjectMojo(goal = "unpack") + void testDuplicateAcrossArtifactsFails(UnpackMojo mojo) throws Exception { + twoArtifactsWithSameEntry(mojo); + mojo.setFailOnDuplicates(true); + MojoFailureException e = assertThrows(MojoFailureException.class, mojo::execute); + String message = e.getMessage(); + assertTrue(message.contains("same.txt"), message); + assertTrue(message.contains("test:one:jar:1.0"), message); + assertTrue(message.contains("test:two:jar:1.0"), message); + // the directory entry present in both archives and the files that are not duplicated are no findings + assertFalse(message.contains("only-"), message); + assertFalse(message.contains("dir"), message); + // the first copy is kept; the second is not written + assertEquals("from one", read(mojo.getOutputDirectory(), "same.txt")); + assertEquals("two", read(mojo.getOutputDirectory(), "only-two.txt")); + // nothing is marked as unpacked, so that a repeated execution detects the duplicate again + assertEquals(0, markerCount(mojo)); + } + + @Test + @InjectMojo(goal = "unpack") + void testDuplicateAcrossArtifactsIsAllowedByDefault(UnpackMojo mojo) throws Exception { + twoArtifactsWithSameEntry(mojo); + assertDoesNotThrow(mojo::execute); + assertEquals("from two", read(mojo.getOutputDirectory(), "same.txt")); + assertEquals(2, markerCount(mojo)); + } + + @Test + @InjectMojo(goal = "unpack") + void testDuplicateIsReportedWhenExistingFilesAreKept(UnpackMojo mojo) throws Exception { + twoArtifactsWithSameEntry(mojo); + mojo.setFailOnDuplicates(true); + mojo.setOverWriteFiles(false); + assertThrows(MojoFailureException.class, mojo::execute); + } + + @Test + @InjectMojo(goal = "unpack") + void testExistingFileIsNotADuplicate(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "same.txt", "from one")); + mojo.setArtifactItems(Collections.singletonList(item("one"))); + mojo.getOutputDirectory().mkdirs(); + Files.write(new File(mojo.getOutputDirectory(), "same.txt").toPath(), "old".getBytes(StandardCharsets.UTF_8)); + mojo.setFailOnDuplicates(true); + assertDoesNotThrow(mojo::execute); + assertEquals("from one", read(mojo.getOutputDirectory(), "same.txt")); + } + + @Test + @InjectMojo(goal = "unpack") + void testDuplicateEntryInOneArchiveFails(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "a.txt", "first", "a.txt", "second")); + mojo.setArtifactItems(Collections.singletonList(item("one"))); + mojo.setFailOnDuplicates(true); + MojoFailureException e = assertThrows(MojoFailureException.class, mojo::execute); + assertTrue(e.getMessage().contains("a.txt"), e.getMessage()); + assertEquals("first", read(mojo.getOutputDirectory(), "a.txt")); + } + + @Test + @InjectMojo(goal = "unpack") + void testDuplicateCreatedByFileMapperFails(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "x/a.txt", "x", "y/a.txt", "y")); + mojo.setArtifactItems(Collections.singletonList(item("one", new FlattenFileMapper()))); + mojo.setFailOnDuplicates(true); + MojoFailureException e = assertThrows(MojoFailureException.class, mojo::execute); + assertTrue(e.getMessage().contains("a.txt"), e.getMessage()); + } + + @Test + @InjectMojo(goal = "unpack") + void testSameNamesWithoutFileMapperAreNotDuplicates(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "x/a.txt", "x", "y/a.txt", "y")); + mojo.setArtifactItems(Collections.singletonList(item("one"))); + mojo.setFailOnDuplicates(true); + assertDoesNotThrow(mojo::execute); + assertEquals("y", read(mojo.getOutputDirectory(), "y/a.txt")); + } + + @Test + @InjectMojo(goal = "unpack") + void testSameEntryInDifferentOutputDirectoriesIsNotADuplicate(UnpackMojo mojo) throws Exception { + artifact("one", zip("one.zip", "same.txt", "from one")); + artifact("two", zip("two.zip", "same.txt", "from two")); + ArtifactItem one = item("one"); + one.setOutputDirectory(new File(tempDir, "out-one")); + ArtifactItem two = item("two"); + two.setOutputDirectory(new File(tempDir, "out-two")); + mojo.setArtifactItems(Arrays.asList(one, two)); + mojo.setFailOnDuplicates(true); + assertDoesNotThrow(mojo::execute); + assertEquals("from two", read(new File(tempDir, "out-two"), "same.txt")); + } +} 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 new file mode 100644 index 00000000..60acb1e8 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestUnpackDependenciesFailOnDuplicates.java @@ -0,0 +1,127 @@ +/* + * 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. + */ +package org.apache.maven.plugins.dependency.fromDependencies; + +import javax.inject.Inject; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collections; +import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory; +import org.apache.maven.project.MavenProject; +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.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * MDEP-772: failing when several entries are unpacked to the same path. Every stub artifact holds the same archive, so + * the artifacts deliver the same paths. + */ +@MojoTest +@Basedir("/unit/unpack-dependencies-test") +class TestUnpackDependenciesFailOnDuplicates { + + @TempDir + private File tempDir; + + @Inject + private MavenProject project; + + private DependencyArtifactStubFactory stubFactory; + + @BeforeEach + void setUp() throws Exception { + File zip = new File(tempDir, "entries.zip"); + try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip))) { + out.putNextEntry(new ZipEntry("dir/")); + out.closeEntry(); + out.putNextEntry(new ZipEntry("same.txt")); + out.write("from archive".getBytes(StandardCharsets.UTF_8)); + out.closeEntry(); + } + stubFactory = new DependencyArtifactStubFactory(tempDir, true, false); + stubFactory.setSrcFile(zip); + project.getBuild().setDirectory(new File(tempDir, "target").getAbsolutePath()); + Set<Artifact> artifacts = stubFactory.getScopedArtifacts(); + project.setArtifacts(artifacts); + } + + private static int markerCount(UnpackDependenciesMojo mojo) { + String[] markers = mojo.getMarkersDirectory().list(); + return markers == null ? 0 : markers.length; + } + + @Test + @InjectMojo(goal = "unpack-dependencies") + void testDuplicatesAreAllowedByDefault(UnpackDependenciesMojo mojo) throws Exception { + assertDoesNotThrow(mojo::execute); + assertTrue(new File(mojo.getOutputDirectory(), "same.txt").exists()); + assertTrue(markerCount(mojo) > 1); + } + + @Test + @InjectMojo(goal = "unpack-dependencies") + void testDuplicateAcrossArtifactsFails(UnpackDependenciesMojo mojo) throws Exception { + mojo.setFailOnDuplicates(true); + MojoFailureException e = assertThrows(MojoFailureException.class, mojo::execute); + assertTrue(e.getMessage().contains("same.txt"), e.getMessage()); + assertTrue(e.getMessage().contains("<-"), e.getMessage()); + // the first copy was written, nothing is marked as unpacked + assertTrue(new File(mojo.getOutputDirectory(), "same.txt").exists()); + assertEquals(0, markerCount(mojo)); + } + + @Test + @InjectMojo(goal = "unpack-dependencies") + void testPerArtifactSubdirectoriesAreNotDuplicates(UnpackDependenciesMojo mojo) throws Exception { + mojo.setFailOnDuplicates(true); + mojo.setUseSubDirectoryPerArtifact(true); + assertDoesNotThrow(mojo::execute); + } + + @Test + @InjectMojo(goal = "unpack-dependencies") + void testExistingFileIsNotADuplicate(UnpackDependenciesMojo mojo) throws Exception { + // a single artifact of the set, so that nothing else delivers the path + mojo.setFailOnDuplicates(true); + project.setArtifacts(Collections.singleton( + stubFactory.createArtifact("test", "one", "1.0", Artifact.SCOPE_COMPILE, "jar", null))); + File existing = new File(mojo.getOutputDirectory(), "same.txt"); + existing.getParentFile().mkdirs(); + Files.write(existing.toPath(), "old".getBytes(StandardCharsets.UTF_8)); + assertDoesNotThrow(mojo::execute); + assertEquals("from archive", new String(Files.readAllBytes(existing.toPath()), StandardCharsets.UTF_8)); + } +}
