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 2773bc48956142c503fd8410aa7b0fd0c2e749fd
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 01:13:49 2026 +0100

    Accept pom declarations, check POM-configured artifact and ranges in 
requireExplicitDependencies
---
 .../child-a/pom.xml                                |  39 ++++++
 .../child-a/src/main/resources/DoNotDelete.xml     |  20 +++
 .../child-b/pom.xml                                |  78 ++++++++++++
 .../invoker.properties                             |  18 +++
 .../require-explicit-dependencies-copy-pom/pom.xml |  53 ++++++++
 .../verify.groovy                                  |  23 ++++
 .../AbstractFromConfigurationMojo.java             |  69 ++++++----
 .../dependency/fromConfiguration/CopyMojo.java     |   1 +
 .../ExplicitDependencyMatcher.java                 |  45 +++++--
 .../dependency/fromConfiguration/UnpackMojo.java   |   1 +
 src/site/markdown/usage.md.vm                      |   4 +-
 .../ExplicitDependencyMatcherTest.java             |  46 +++++++
 .../TestRequireExplicitDependencies.java           | 139 +++++++++++++++++++++
 13 files changed, 503 insertions(+), 33 deletions(-)

diff --git 
a/src/it/projects/require-explicit-dependencies-copy-pom/child-a/pom.xml 
b/src/it/projects/require-explicit-dependencies-copy-pom/child-a/pom.xml
new file mode 100644
index 00000000..1f05edf1
--- /dev/null
+++ b/src/it/projects/require-explicit-dependencies-copy-pom/child-a/pom.xml
@@ -0,0 +1,39 @@
+<?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/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.its.dependency</groupId>
+    <artifactId>test</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>child-a</artifactId>
+  <packaging>jar</packaging>
+
+  <name>Copy from reactor source of dependency</name>
+  <description>
+    Test dependency:copy when searching the reactor
+  </description>
+
+</project>
diff --git 
a/src/it/projects/require-explicit-dependencies-copy-pom/child-a/src/main/resources/DoNotDelete.xml
 
b/src/it/projects/require-explicit-dependencies-copy-pom/child-a/src/main/resources/DoNotDelete.xml
new file mode 100644
index 00000000..4d3d9e49
--- /dev/null
+++ 
b/src/it/projects/require-explicit-dependencies-copy-pom/child-a/src/main/resources/DoNotDelete.xml
@@ -0,0 +1,20 @@
+<?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.
+  -->
+<root/>
diff --git 
a/src/it/projects/require-explicit-dependencies-copy-pom/child-b/pom.xml 
b/src/it/projects/require-explicit-dependencies-copy-pom/child-b/pom.xml
new file mode 100644
index 00000000..ae297181
--- /dev/null
+++ b/src/it/projects/require-explicit-dependencies-copy-pom/child-b/pom.xml
@@ -0,0 +1,78 @@
+<?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/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.maven.its.dependency</groupId>
+    <artifactId>test</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>child-b</artifactId>
+  <packaging>pom</packaging>
+
+  <name>Require explicit dependencies (pom declaration)</name>
+  <description>
+    Test dependency:copy with requireExplicitDependencies
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its.dependency</groupId>
+      <artifactId>child-a</artifactId>
+      <version>1.0-SNAPSHOT</version>
+      <type>pom</type>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>copy</goal>
+            </goals>
+            <configuration>
+              <requireExplicitDependencies>true</requireExplicitDependencies>
+              <artifactItems>
+                <artifactItem>
+                  <groupId>org.apache.maven.its.dependency</groupId>
+                  <artifactId>child-a</artifactId>
+                  <version>1.0-SNAPSHOT</version>
+                </artifactItem>
+              </artifactItems>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git 
a/src/it/projects/require-explicit-dependencies-copy-pom/invoker.properties 
b/src/it/projects/require-explicit-dependencies-copy-pom/invoker.properties
new file mode 100644
index 00000000..2e199f7b
--- /dev/null
+++ b/src/it/projects/require-explicit-dependencies-copy-pom/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.goals = clean package
diff --git a/src/it/projects/require-explicit-dependencies-copy-pom/pom.xml 
b/src/it/projects/require-explicit-dependencies-copy-pom/pom.xml
new file mode 100644
index 00000000..da86ae30
--- /dev/null
+++ b/src/it/projects/require-explicit-dependencies-copy-pom/pom.xml
@@ -0,0 +1,53 @@
+<?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/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <modules>
+    <module>child-a</module>
+    <module>child-b</module>
+  </modules>
+
+  <name>Require explicit dependencies (pom declaration)</name>
+  <description>
+    Test dependency:unpack with requireExplicitDependencies
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>@project.version@</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git 
a/src/it/projects/require-explicit-dependencies-copy-pom/verify.groovy 
b/src/it/projects/require-explicit-dependencies-copy-pom/verify.groovy
new file mode 100644
index 00000000..aac47286
--- /dev/null
+++ b/src/it/projects/require-explicit-dependencies-copy-pom/verify.groovy
@@ -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.
+ */
+
+File unpacked = new File( basedir, 
"child-b/target/dependency/child-a-1.0-SNAPSHOT.jar" )
+assert unpacked.isFile() : "Missing file " + unpacked
+
+return true;
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 2ea9dd1c..109e1cf2 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
@@ -18,6 +18,8 @@
  */
 package org.apache.maven.plugins.dependency.fromConfiguration;
 
+import javax.inject.Inject;
+
 import java.io.File;
 import java.util.Collections;
 import java.util.List;
@@ -28,6 +30,7 @@ import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -139,20 +142,24 @@ public abstract class AbstractFromConfigurationMojo 
extends AbstractDependencyMo
     private List<String> remoteRepositories;
 
     /**
-     * Fail if an item of {@code artifactItems} is not declared as a 
dependency of the project, or is declared with a
-     * different version. Maven orders the reactor by the declared 
dependencies only, so an undeclared item that is
-     * built by another module of the reactor may be missing or stale. Declare 
such an item as a dependency, for
-     * example with scope {@code provided} or {@code test}, or with type 
{@code pom} where only its content is needed.
-     * Items match on groupId, artifactId, type and classifier, where {@code 
test-jar} equals {@code jar} with the
-     * classifier {@code tests}. The check only applies to items configured in 
the POM, not to the command line
-     * parameter {@code artifact}.
+     * Fail if an item of {@code artifactItems} (or the {@code artifact} 
configured in the POM) is not declared as a
+     * dependency of the project, or is declared with a different version. 
Maven orders the reactor by the declared
+     * dependencies, not by plugin configuration, so an undeclared item that 
is built by another module of the reactor
+     * may be missing or stale. Declare such an item as a dependency, for 
example with scope {@code provided} or
+     * {@code test}. A dependency of type {@code pom} with the same groupId, 
artifactId and version also satisfies the
+     * check for any type and classifier, and keeps the artifact off the 
classpath, but then the artifact is not
+     * visible in {@code dependency:tree} or SBOMs either. Items match on 
groupId, artifactId, extension and
+     * classifier, where {@code test-jar} equals {@code jar} with the 
classifier {@code tests}. A version range
+     * matches a version it contains. The check does not apply to a goal 
invoked from the command line, such as
+     * {@code mvn dependency:copy -Dartifact=...}.
      *
      * @since 3.12.0
      */
     @Parameter(property = "mdep.requireExplicitDependencies", defaultValue = 
"false")
     private boolean requireExplicitDependencies;
 
-    private boolean artifactFromCommandLine;
+    @Inject
+    private MojoExecution mojoExecution;
 
     private final ArtifactHandlerManager artifactHandlerManager;
 
@@ -184,22 +191,38 @@ public abstract class AbstractFromConfigurationMojo 
extends AbstractDependencyMo
         if (artifactItems == null || artifactItems.isEmpty()) {
             throw new MojoFailureException("Either artifact or artifactItems 
is required ");
         }
-        if (requireExplicitDependencies) {
-            if (artifactFromCommandLine) {
-                getLog().warn("requireExplicitDependencies is ignored for the 
artifact parameter");
-            } else {
-                List<String> violations = new ExplicitDependencyMatcher(
-                                artifactHandlerManager, 
getProject().getDependencies())
-                        .findViolations(artifactItems);
-                if (!violations.isEmpty()) {
-                    throw new 
MojoFailureException("requireExplicitDependencies is set, but artifactItems 
must be "
-                            + "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.");
-                }
+    }
+
+    /**
+     * Fails if {@link #requireExplicitDependencies} is set and an item is not 
declared as a dependency. Must run
+     * after {@link #verifyRequirements()} and before anything is resolved.
+     *
+     * @throws MojoFailureException if an item is not declared
+     * @throws MojoExecutionException if a missing version cannot be determined
+     */
+    protected void verifyExplicitDependencies() throws MojoExecutionException, 
MojoFailureException {
+        if (!requireExplicitDependencies) {
+            return;
+        }
+        if (mojoExecution != null && mojoExecution.getSource() == 
MojoExecution.Source.CLI) {
+            getLog().warn("requireExplicitDependencies is ignored when the 
goal is invoked from the command line");
+            return;
+        }
+        for (ArtifactItem item : artifactItems) {
+            if (item.getVersion() == null || item.getVersion().isEmpty()) {
+                fillMissingArtifactVersion(item);
             }
         }
+        List<String> violations = new ExplicitDependencyMatcher(
+                        artifactHandlerManager, getProject().getDependencies())
+                .findViolations(artifactItems);
+        if (!violations.isEmpty()) {
+            throw new MojoFailureException("requireExplicitDependencies is 
set, but artifactItems must be "
+                    + "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.");
+        }
     }
 
     /**
@@ -370,7 +393,6 @@ public abstract class AbstractFromConfigurationMojo extends 
AbstractDependencyMo
      */
     public void setArtifactItems(List<ArtifactItem> theArtifactItems) {
         this.artifactItems = theArtifactItems;
-        this.artifactFromCommandLine = false;
     }
 
     /**
@@ -495,7 +517,6 @@ public abstract class AbstractFromConfigurationMojo extends 
AbstractDependencyMo
             }
 
             setArtifactItems(Collections.singletonList(artifactItem));
-            artifactFromCommandLine = true;
         }
     }
 }
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
index 3ad5f2ef..ead5e163 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java
@@ -119,6 +119,7 @@ public class CopyMojo extends AbstractFromConfigurationMojo 
{
     @Override
     protected void doExecute() throws MojoExecutionException, 
MojoFailureException {
         verifyRequirements();
+        verifyExplicitDependencies();
 
         List<ArtifactItem> theArtifactItems = getProcessedArtifactItems(
                 new ProcessArtifactItemsRequest(stripVersion, prependGroupId, 
useBaseVersion, stripClassifier));
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 0f90a3c4..65644f8e 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
@@ -24,6 +24,9 @@ import java.util.Objects;
 
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import 
org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.model.Dependency;
 
 /**
@@ -32,8 +35,9 @@ import org.apache.maven.model.Dependency;
  * <p>
  * 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. An item 
without a version matches regardless of
- * the declared version.
+ * 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, and a
+ * version range matches a version it contains.
  */
 final class ExplicitDependencyMatcher {
     private final ArtifactHandlerManager artifactHandlerManager;
@@ -69,11 +73,17 @@ final class ExplicitDependencyMatcher {
 
         List<String> declaredVersions = new ArrayList<>();
         for (Dependency dependency : declared) {
-            if (Objects.equals(item.getGroupId(), dependency.getGroupId())
-                    && Objects.equals(item.getArtifactId(), 
dependency.getArtifactId())
-                    && extension.equals(extension(dependency.getType()))
-                    && Objects.equals(classifier, 
classifier(dependency.getType(), dependency.getClassifier()))) {
-                if (version.isEmpty() || 
version.equals(dependency.getVersion())) {
+            if (!Objects.equals(item.getGroupId(), dependency.getGroupId())
+                    || !Objects.equals(item.getArtifactId(), 
dependency.getArtifactId())) {
+                continue;
+            }
+            // The reactor orders on groupId:artifactId:version only, so a pom 
declaration covers every variant.
+            boolean sameVariant = "pom".equals(dependency.getType())
+                    || (extension.equals(extension(dependency.getType()))
+                            && Objects.equals(
+                                    classifier, 
classifier(dependency.getType(), dependency.getClassifier())));
+            if (sameVariant) {
+                if (version.isEmpty() || versionsMatch(version, 
dependency.getVersion())) {
                     return null;
                 }
                 declaredVersions.add(String.valueOf(dependency.getVersion()));
@@ -86,6 +96,27 @@ final class ExplicitDependencyMatcher {
                 + version;
     }
 
+    static boolean versionsMatch(String itemVersion, String declaredVersion) {
+        if (itemVersion.equals(declaredVersion)) {
+            return true;
+        }
+        if (declaredVersion == null) {
+            return false;
+        }
+        return contains(declaredVersion, itemVersion) || contains(itemVersion, 
declaredVersion);
+    }
+
+    private static boolean contains(String rangeSpec, String version) {
+        if (!rangeSpec.startsWith("[") && !rangeSpec.startsWith("(")) {
+            return false;
+        }
+        try {
+            return 
VersionRange.createFromVersionSpec(rangeSpec).containsVersion(new 
DefaultArtifactVersion(version));
+        } catch (InvalidVersionSpecificationException e) {
+            return false;
+        }
+    }
+
     private ArtifactHandler handler(String type) {
         return artifactHandlerManager.getArtifactHandler(type == null || 
type.isEmpty() ? "jar" : type);
     }
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 f2ec5fb9..2f23f7d9 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
@@ -147,6 +147,7 @@ public class UnpackMojo extends 
AbstractFromConfigurationMojo {
         }
 
         verifyRequirements();
+        verifyExplicitDependencies();
 
         List<ArtifactItem> processedItems = getProcessedArtifactItems(false);
         for (ArtifactItem artifactItem : processedItems) {
diff --git a/src/site/markdown/usage.md.vm b/src/site/markdown/usage.md.vm
index 90920ae1..a761a54f 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 only, so an undeclared item 
built by another module can be missing or stale. The check does not apply to 
the `artifact` command line parameter.
+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. A 
dependency of type `pom` with the same groupId, artifactId and version 
satisfies the check for any type and [...]
 
 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 only, so an undeclared item 
built by another module can be missing or stale. The check does not apply to 
the `artifact` command line parameter.
+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. A 
dependency of type `pom` with the same groupId, artifactId and version 
satisfies the check for any type and [...]
 
 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 f8e56cb8..211f0012 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
@@ -172,4 +172,50 @@ class ExplicitDependencyMatcherTest {
                 check(Collections.<Dependency>emptyList(), item("a", "1", 
null, null), item("b", "1", null, null))
                         .size());
     }
+
+    @Test
+    void pomDeclarationCoversEveryVariant() {
+        List<Dependency> declared = Collections.singletonList(dependency("a", 
"1.0", "pom", null));
+        ArtifactItem zip = item("a", "1.0", "zip", "resources");
+        assertTrue(check(declared, zip).isEmpty());
+        assertTrue(check(declared, item("a", "1.0", "test-jar", 
null)).isEmpty());
+        assertEquals(1, check(declared, item("a", "2.0", "zip", 
"resources")).size());
+        assertEquals(1, check(declared, item("b", "1.0", "zip", 
"resources")).size());
+    }
+
+    @Test
+    void rangeMatchesContainedVersionInBothDirections() {
+        assertTrue(
+                check(Collections.singletonList(dependency("a", "[1.0,2.0)", 
null, null)), item("a", "1.5", null, null))
+                        .isEmpty());
+        assertTrue(
+                check(Collections.singletonList(dependency("a", "1.5", null, 
null)), item("a", "[1.0,2.0)", null, null))
+                        .isEmpty());
+        assertEquals(
+                1,
+                check(Collections.singletonList(dependency("a", "[1.0,2.0)", 
null, null)), item("a", "2.0", null, null))
+                        .size());
+        assertEquals(
+                1,
+                check(Collections.singletonList(dependency("a", "2.0", null, 
null)), item("a", "[1.0,2.0)", null, null))
+                        .size());
+    }
+
+    @Test
+    void snapshotVersions() {
+        assertTrue(check(
+                        Collections.singletonList(dependency("a", 
"1.0-SNAPSHOT", null, null)),
+                        item("a", "1.0-SNAPSHOT", null, null))
+                .isEmpty());
+        assertEquals(
+                1,
+                check(
+                                Collections.singletonList(dependency("a", 
"[1.0,2.0)", null, null)),
+                                item("a", "1.0-SNAPSHOT", null, null))
+                        .size());
+        assertTrue(check(
+                        Collections.singletonList(dependency("a", 
"[1.0-SNAPSHOT,2.0)", null, null)),
+                        item("a", "1.0-SNAPSHOT", null, null))
+                .isEmpty());
+    }
 }
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
new file mode 100644
index 00000000..e3e12926
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestRequireExplicitDependencies.java
@@ -0,0 +1,139 @@
+/*
+ * 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 java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+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.model.Dependency;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@MojoTest(realRepositorySession = true)
+class TestRequireExplicitDependencies {
+
+    private static Dependency dependency(String artifactId, String version, 
String type, String classifier) {
+        Dependency dependency = new Dependency();
+        dependency.setGroupId("g");
+        dependency.setArtifactId(artifactId);
+        dependency.setVersion(version);
+        dependency.setType(type);
+        dependency.setClassifier(classifier);
+        return dependency;
+    }
+
+    private static ArtifactItem item(String artifactId, String version, String 
type, String classifier) {
+        ArtifactItem item = new ArtifactItem();
+        item.setGroupId("g");
+        item.setArtifactId(artifactId);
+        item.setVersion(version);
+        item.setType(type);
+        item.setClassifier(classifier);
+        return item;
+    }
+
+    private static void setSource(CopyMojo mojo, MojoExecution execution) 
throws Exception {
+        Field field = 
AbstractFromConfigurationMojo.class.getDeclaredField("mojoExecution");
+        field.setAccessible(true);
+        field.set(mojo, execution);
+    }
+
+    private static MojoExecution lifecycleExecution() {
+        return new MojoExecution(new org.apache.maven.model.Plugin(), "copy", 
"test");
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "requireExplicitDependencies", value = "true")
+    void reportsEveryUndeclaredItemWithTheFix(CopyMojo mojo) throws Exception {
+        setSource(mojo, lifecycleExecution());
+        
mojo.getProject().setDependencies(Collections.singletonList(dependency("a", 
"1.0", "jar", null)));
+        List<ArtifactItem> items = new ArrayList<>();
+        items.add(item("a", "1.0", "jar", null));
+        items.add(item("b", "1.0", "zip", "resources"));
+        items.add(item("a", "2.0", "jar", null));
+        mojo.setArtifactItems(items);
+
+        MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+
+        String nl = System.lineSeparator();
+        assertEquals(
+                "requireExplicitDependencies is set, but artifactItems must be 
declared as dependencies of the "
+                        + "project, so that Maven orders the reactor by them:" 
+ nl
+                        + "  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.",
+                e.getMessage());
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "requireExplicitDependencies", value = "true")
+    @MojoParameter(name = "artifact", value = "g:undeclared:1.0")
+    void checksAnArtifactConfiguredInThePom(CopyMojo mojo) throws Exception {
+        setSource(mojo, lifecycleExecution());
+        
mojo.getProject().setDependencies(Collections.singletonList(dependency("a", 
"1.0", "jar", null)));
+
+        MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("g:undeclared:1.0:jar is not 
declared"), e.getMessage());
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "requireExplicitDependencies", value = "true")
+    @MojoParameter(name = "artifact", value = "g:undeclared:1.0")
+    void skipsTheCheckForAGoalInvokedFromTheCommandLine(CopyMojo mojo) throws 
Exception {
+        setSource(mojo, new MojoExecution(new MojoDescriptor(), "default-cli", 
MojoExecution.Source.CLI));
+        mojo.getProject().setDependencies(Collections.<Dependency>emptyList());
+
+        // the check passes; the execution then fails later, while resolving 
the artifact
+        Exception e = assertThrows(Exception.class, mojo::execute);
+
+        assertTrue(!(e instanceof MojoFailureException), String.valueOf(e));
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    @MojoParameter(name = "requireExplicitDependencies", value = "true")
+    void versionlessItemMustResolveToTheDeclaredVariant(CopyMojo mojo) throws 
Exception {
+        setSource(mojo, lifecycleExecution());
+        // g:a:jar:tests without a version falls back to the loose match 
g:a:2.0, which is not the test-jar
+        mojo.getProject()
+                .setDependencies(java.util.Arrays.asList(
+                        dependency("a", "2.0", "jar", null), dependency("a", 
"1.0", "test-jar", null)));
+        mojo.setArtifactItems(Collections.singletonList(item("a", null, "jar", 
"tests")));
+
+        MojoFailureException e = assertThrows(MojoFailureException.class, 
mojo::execute);
+
+        assertTrue(e.getMessage().contains("is declared as a dependency with 
version 1.0, not 2.0"), e.getMessage());
+    }
+}

Reply via email to