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 36c366928509577fc4fca0f8cf7811ef57918922 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 13:23:59 2026 +0100 Align requireExplicitDependencies messages with the version lookup --- .../AbstractFromConfigurationMojo.java | 5 +++-- .../dependency/fromConfiguration/ArtifactItem.java | 4 ++-- .../fromConfiguration/ExplicitDependencyMatcher.java | 13 +++++++++++-- src/site/markdown/usage.md.vm | 4 ++-- .../ExplicitDependencyMatcherTest.java | 3 ++- .../TestRequireExplicitDependencies.java | 20 +++++++++++++++++++- .../fromConfiguration/TestUnpackOverWriteFiles.java | 3 ++- 7 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java index 382c2035..1336b682 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java @@ -155,7 +155,8 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo * </p> * <p> * Items match on groupId, artifactId, extension and classifier, where {@code test-jar} equals {@code jar} with - * the classifier {@code tests}. An item without a version matches a declaration of any version. A version + * the classifier {@code tests}. An item without a version takes its version from the + * project, as for the goal itself, and is then checked like any other item. A version * range in the declaration matches an item version it contains; a version range in an item matches only an * identical declaration, as the goal resolves it to the highest available version. * </p> @@ -236,7 +237,7 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo + "declared as dependencies of the project, so that Maven orders the reactor by them:" + System.lineSeparator() + " " + String.join(System.lineSeparator() + " ", violations) + System.lineSeparator() + "Declare each of them in the dependencies of the project, " - + "for example with scope provided or test, or with type pom, and use the same version."); + + "for example with scope provided or test, and use the same version."); } } 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 9c533e8b..b087e420 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,8 +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. When several entries or - * artifacts map to the same path, the first one written wins. 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/ExplicitDependencyMatcher.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java index c5166b70..34efa758 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcher.java @@ -36,7 +36,7 @@ import org.apache.maven.model.Dependency; * Items and dependencies are compared on groupId, artifactId, extension and classifier. The type is translated with * the artifact handler, so {@code type=test-jar} equals {@code type=jar} with {@code classifier=tests}. The type * defaults to {@code jar} and an empty classifier is no classifier. A declared dependency of type {@code pom} - * matches every type and classifier. An item without a version matches regardless of the declared version. A declared + * matches every type and classifier. The mojo fills in a missing item version before the check. A declared * version range matches an item version it contains; an item version range is only matched by an identical * declaration, because the goal resolves such a range to the highest available version, which may not be the * declared one. @@ -74,6 +74,7 @@ final class ExplicitDependencyMatcher { String version = item.getVersion() == null ? "" : item.getVersion().trim(); List<String> declaredVersions = new ArrayList<>(); + List<String> otherVariants = new ArrayList<>(); for (Dependency dependency : declared) { if (!Objects.equals(item.getGroupId(), dependency.getGroupId()) || !Objects.equals(item.getArtifactId(), dependency.getArtifactId())) { @@ -89,10 +90,18 @@ final class ExplicitDependencyMatcher { return null; } declaredVersions.add(String.valueOf(dependency.getVersion())); + } else { + String other = dependency.getType() == null ? "jar" : dependency.getType(); + if (dependency.getClassifier() != null + && !dependency.getClassifier().isEmpty()) { + other += ":" + dependency.getClassifier(); + } + otherVariants.add(other); } } if (declaredVersions.isEmpty()) { - return item + " is not declared as a dependency of the project"; + return item + " is not declared as a dependency of the project" + + (otherVariants.isEmpty() ? "" : " (declared as " + String.join(", ", otherVariants) + ")"); } return item + " is declared as a dependency with version " + String.join(", ", declaredVersions) + ", not " + version; diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm index 43050050..407e499b 100644 --- a/src/site/markdown/usage.md.vm +++ b/src/site/markdown/usage.md.vm @@ -38,7 +38,7 @@ This goal is meant to be bound to a lifecycle phase and configured in your `pom. The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section. -Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactI [...] +Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactI [...] See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwriting is handled. @@ -196,7 +196,7 @@ See the [Overwrite Rules](#Overwrite_Rules) section for rules about how overwrit The artifact version is optional. If not set, the plugin will attempt to resolve it from the project dependencies and then the dependencyManagement section. -Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactI [...] +Set `requireExplicitDependencies` (`-Dmdep.requireExplicitDependencies=true`) to fail the build when an artifact item is not declared in the `dependencies` of the project, or is declared with another version. Maven orders the modules of a reactor build by the declared dependencies, not by plugin configuration, so an undeclared item built by another module can be missing or stale. Declare the item with scope `provided` or `test`. A dependency of type `pom` with the same groupId, artifactI [...] Configure the plugin something like this if you intend to bind it to execute along with your build: diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java index 61831969..a67f5ca8 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java +++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/ExplicitDependencyMatcherTest.java @@ -29,6 +29,7 @@ import org.apache.maven.model.Dependency; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -217,7 +218,7 @@ class ExplicitDependencyMatcherTest { handlers, Collections.singletonList(dependency("a", "1.0", "zip", "resources"))); assertTrue(matcher.isDeclaredInAnyVariant(item("a", null, "jar", null))); - assertTrue(!matcher.isDeclaredInAnyVariant(item("b", null, "jar", null))); + assertFalse(matcher.isDeclaredInAnyVariant(item("b", null, "jar", null))); } @Test diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestRequireExplicitDependencies.java b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestRequireExplicitDependencies.java index ffb5b1b7..784e1965 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestRequireExplicitDependencies.java +++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestRequireExplicitDependencies.java @@ -90,7 +90,7 @@ class TestRequireExplicitDependencies { + " g:b:resources:1.0:zip is not declared as a dependency of the project" + nl + " g:a:2.0:jar is declared as a dependency with version 1.0, not 2.0" + nl + "Declare each of them in the dependencies of the project, for example with scope provided " - + "or test, or with type pom, and use the same version.", + + "or test, and use the same version.", e.getMessage()); } @@ -150,4 +150,22 @@ class TestRequireExplicitDependencies { assertTrue(e.getMessage().contains("g:undeclared"), e.getMessage()); assertTrue(e.getMessage().contains("is not declared as a dependency of the project"), e.getMessage()); } + + @Test + @InjectMojo(goal = "copy") + @MojoParameter(name = "requireExplicitDependencies", value = "true") + void namesTheVariantsThatAreDeclaredInstead(CopyMojo mojo) throws Exception { + setSource(mojo, lifecycleExecution()); + mojo.getProject() + .setDependencies(java.util.Arrays.asList( + dependency("a", "1.0", "jar", null), dependency("a", "1.0", "zip", "resources"))); + mojo.setArtifactItems(Collections.singletonList(item("a", "1.0", "war", null))); + + MojoFailureException e = assertThrows(MojoFailureException.class, mojo::execute); + + assertTrue( + e.getMessage() + .contains("is not declared as a dependency of the project (declared as jar, zip:resources)"), + e.getMessage()); + } } 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 6219cc06..70bb7a21 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 @@ -22,6 +22,7 @@ import javax.inject.Inject; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -149,7 +150,7 @@ class TestUnpackOverWriteFiles { 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) { + } catch (UnsupportedOperationException | IOException e) { Assumptions.abort("symbolic links are not available: " + e); } mojo.setOverWriteFiles(false);
