This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 933e203d Introduce graphRoots for dependencyFilter based mojos (#1571)
933e203d is described below

commit 933e203d1b20deb57dca6f8cedb6cbf2f12a9ec8
Author: Robert Scholte <[email protected]>
AuthorDate: Fri Jan 16 17:56:12 2026 +0100

    Introduce graphRoots for dependencyFilter based mojos (#1571)
    
    With graphRoots it is possible to select one or more dependencies as the 
root of the artifact filters. By doing this you can isolate a set of jars for 
further purpose, like giving them their own classloader.
    
    ---------
    
    Co-authored-by: Maarten Mulders <[email protected]>
---
 pom.xml                                            |   2 +-
 src/it/mrm/repository/d-without-dep-3.2.1.pom      |  30 ++++++
 .../invoker.properties                             |  18 ++++
 .../projects/copy-dependencies-graphroots/pom.xml  | 112 +++++++++++++++++++++
 .../copy-dependencies-graphroots/verify.groovy     |  37 +++++++
 .../AbstractDependencyFilterMojo.java              |  60 ++++++++++-
 .../fromDependencies/DependencyMatcher.java        |  27 +++++
 .../dependency/fromDependencies/GraphRoot.java     |  42 ++++++++
 .../fromDependencies/GraphRootMatcher.java         |  38 +++++++
 .../fromDependencies/OrDependencyMatcher.java      |  50 +++++++++
 .../examples/copying-project-dependencies.apt.vm   |  50 ++++++++-
 .../fromDependencies/GraphRootMatcherTest.java     |  68 +++++++++++++
 .../fromDependencies/OrDependencyMatcherTest.java  |  61 +++++++++++
 13 files changed, 592 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index c26903bf..44d8fcc8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@ under the License.
   </parent>
 
   <artifactId>maven-dependency-plugin</artifactId>
-  <version>3.9.1-SNAPSHOT</version>
+  <version>3.10.0-SNAPSHOT</version>
   <packaging>maven-plugin</packaging>
 
   <name>Apache Maven Dependency Plugin</name>
diff --git a/src/it/mrm/repository/d-without-dep-3.2.1.pom 
b/src/it/mrm/repository/d-without-dep-3.2.1.pom
new file mode 100644
index 00000000..0ee60104
--- /dev/null
+++ b/src/it/mrm/repository/d-without-dep-3.2.1.pom
@@ -0,0 +1,30 @@
+<?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 
https://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.dependencies</groupId>
+  <artifactId>d-without-dep</artifactId>
+  <version>3.2.1</version>
+
+</project>
diff --git a/src/it/projects/copy-dependencies-graphroots/invoker.properties 
b/src/it/projects/copy-dependencies-graphroots/invoker.properties
new file mode 100644
index 00000000..f50e476f
--- /dev/null
+++ b/src/it/projects/copy-dependencies-graphroots/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 process-sources
diff --git a/src/it/projects/copy-dependencies-graphroots/pom.xml 
b/src/it/projects/copy-dependencies-graphroots/pom.xml
new file mode 100644
index 00000000..d030bf28
--- /dev/null
+++ b/src/it/projects/copy-dependencies-graphroots/pom.xml
@@ -0,0 +1,112 @@
+<?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</groupId>
+    <artifactId>apache</artifactId>
+    <version>5</version>
+  </parent>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>Test</name>
+  <description>
+    Test dependency:copy-dependencies using graphRoots
+  </description>
+  
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.its.dependencies</groupId>
+      <artifactId>d-without-dep</artifactId>
+      <version>3.2.1</version>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.maven.its.dependency</groupId>
+         <artifactId>a-with-dep</artifactId>
+      <version>1.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.its.dependency</groupId>
+      <artifactId>get-artifact</artifactId>
+      <version>1.0</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+
+    <defaultGoal>package</defaultGoal>
+
+    <plugins>
+      <plugin>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test-1</id>
+            <goals>
+              <goal>copy-dependencies</goal>
+            </goals>
+            <configuration>
+              <graphRoots>
+                <graphRoot>
+                  <groupId>org.apache.maven.its.dependency</groupId>
+                     <artifactId>a-with-dep</artifactId>
+                </graphRoot>
+                <graphRoot>
+                  <groupId>org.apache.maven.its.dependencies</groupId>
+                  <artifactId>d-without-dep</artifactId>
+                </graphRoot>
+              </graphRoots>
+              
<outputDirectory>${project.build.directory}/it/copy-dep-test-1</outputDirectory>
+            </configuration>
+          </execution>
+          <execution>
+            <id>test-2</id>
+            <goals>
+              <goal>copy-dependencies</goal>
+            </goals>
+            <configuration>
+              <graphRoots>
+                <graphRoot>
+                             <groupId>org.apache.maven.its.dependency</groupId>
+                             <artifactId>get-artifact</artifactId>
+                </graphRoot>
+              </graphRoots>
+              
<outputDirectory>${project.build.directory}/it/copy-dep-test-2</outputDirectory>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/copy-dependencies-graphroots/verify.groovy 
b/src/it/projects/copy-dependencies-graphroots/verify.groovy
new file mode 100644
index 00000000..1d70b659
--- /dev/null
+++ b/src/it/projects/copy-dependencies-graphroots/verify.groovy
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+def targetFiles1 = ['a-with-dep-1.0.0.jar', 'b-with-dep-1.0.0.jar', 
'c-without-dep-1.0.0.jar', 'd-without-dep-3.2.1.jar']
+def directory1 = new File(basedir, 'target/it/copy-dep-test-1')
+
+// Get only file names from the directory (excludes subdirectories)
+def actualFiles1 = directory1.listFiles().findAll { it.isFile() }.collect { 
it.name }
+
+// Check if the sets are identical and have exactly 3 files
+assert (actualFiles1.size() == 4 && actualFiles1.containsAll(targetFiles1))
+
+
+def targetFiles2 = ['get-artifact-1.0.jar', 'get-artifact-transitive-1.0.jar']
+def directory2 = new File(basedir, 'target/it/copy-dep-test-2')
+
+// Get only file names from the directory (excludes subdirectories)
+def actualFiles2 = directory2.listFiles().findAll { it.isFile() }.collect { 
it.name }
+
+// Check if the sets are identical and have exactly 3 files
+assert (actualFiles2.size() == 2 && actualFiles2.containsAll(targetFiles2))
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java
index 992807f9..b14f0113 100644
--- 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/AbstractDependencyFilterMojo.java
@@ -23,13 +23,17 @@ import javax.inject.Inject;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
@@ -52,7 +56,9 @@ import 
org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
 import 
org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
 import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
+import org.eclipse.aether.repository.RemoteRepository;
 import org.eclipse.aether.resolution.ArtifactResolutionException;
+import org.eclipse.aether.resolution.DependencyResolutionException;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
@@ -231,6 +237,16 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
     @Parameter(property = "mdep.prependGroupId", defaultValue = "false")
     protected boolean prependGroupId = false;
 
+    /**
+     * By default, this goal uses the project itself as the root of the 
dependency tree.
+     * With graphRoots, you can select a subtree of dependencies based on 
groupId and artifactId.
+     * After that, the general include/exclude filters can be applied.
+     *
+     * @since 3.10.0
+     */
+    @Parameter
+    private List<GraphRoot> graphRoots;
+
     private final ResolverUtil resolverUtil;
 
     private final ProjectBuilder projectBuilder;
@@ -292,6 +308,7 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
      */
     protected DependencyStatusSets getDependencySets(boolean stopOnFailure, 
boolean includeParents)
             throws MojoExecutionException {
+
         // add filters in well known order, least specific to most specific
         FilterArtifacts filter = new FilterArtifacts();
 
@@ -323,7 +340,13 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
                 
DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));
 
         // start with all artifacts.
-        Set<Artifact> artifacts = getProject().getArtifacts();
+        Set<Artifact> artifacts;
+
+        try {
+            artifacts = collectArtifacts(getProject());
+        } catch (DependencyResolutionException e) {
+            throw new MojoExecutionException("Failed to collect artifacts", e);
+        }
 
         if (includeParents) {
             // add dependencies parents
@@ -479,6 +502,41 @@ public abstract class AbstractDependencyFilterMojo extends 
AbstractDependencyMoj
         return resolvedArtifacts;
     }
 
+    private Set<Artifact> collectArtifacts(MavenProject project) throws 
DependencyResolutionException {
+        if (graphRoots == null || graphRoots.isEmpty()) {
+            // artifact have already been resolved here due to
+            // @Mojo(requiresDependencyResolution = ResolutionScope.TEST) on 
final Mojo
+            return project.getArtifacts();
+        } else {
+            // MavenProject doesn't provide access to the graph of 
dependencies(only the direct dependencies)
+            // Hence we need to re-resolve artifacts, but only for the 
matching graphnodes
+            List<DependencyMatcher> filterMatchers =
+                    
graphRoots.stream().map(GraphRootMatcher::new).collect(Collectors.toList());
+
+            DependencyMatcher subTreeMatcher = new 
OrDependencyMatcher(filterMatchers);
+
+            Set<Artifact> artifacts = new HashSet<>();
+            for (Dependency dep : project.getDependencies()) {
+                if (subTreeMatcher.matches(dep)) {
+                    artifacts.addAll(resolveDependencyArtifacts(dep));
+                }
+            }
+            return artifacts;
+        }
+    }
+
+    private Set<Artifact> resolveDependencyArtifacts(Dependency root) throws 
DependencyResolutionException {
+        org.eclipse.aether.graph.Dependency dependency = 
RepositoryUtils.toDependency(
+                root, 
session.getRepositorySession().getArtifactTypeRegistry());
+
+        List<RemoteRepository> remoteRepositories = 
getProject().getRemoteProjectRepositories();
+
+        Collection<org.eclipse.aether.artifact.Artifact> depArtifacts =
+                resolverUtil.resolveDependencies(dependency.getArtifact(), 
remoteRepositories);
+
+        return 
depArtifacts.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toSet());
+    }
+
     /**
      * @return returns the markersDirectory
      */
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/DependencyMatcher.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/DependencyMatcher.java
new file mode 100644
index 00000000..f5b0acb8
--- /dev/null
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/DependencyMatcher.java
@@ -0,0 +1,27 @@
+/*
+ * 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 org.apache.maven.model.Dependency;
+
+@FunctionalInterface
+public interface DependencyMatcher {
+
+    boolean matches(Dependency dependency);
+}
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRoot.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRoot.java
new file mode 100644
index 00000000..e85cb3ec
--- /dev/null
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRoot.java
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+public class GraphRoot {
+
+    private String groupId;
+
+    private String artifactId;
+
+    public String getGroupId() {
+        return groupId;
+    }
+
+    public void setGroupId(String groupId) {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId() {
+        return artifactId;
+    }
+
+    public void setArtifactId(String artifactId) {
+        this.artifactId = artifactId;
+    }
+}
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcher.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcher.java
new file mode 100644
index 00000000..7bba0adf
--- /dev/null
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcher.java
@@ -0,0 +1,38 @@
+/*
+ * 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 java.util.Objects;
+
+import org.apache.maven.model.Dependency;
+
+class GraphRootMatcher implements DependencyMatcher {
+
+    private final GraphRoot graphRoot;
+
+    GraphRootMatcher(GraphRoot graphRoot) {
+        this.graphRoot = Objects.requireNonNull(graphRoot);
+    }
+
+    @Override
+    public boolean matches(Dependency dependency) {
+        return Objects.equals(graphRoot.getGroupId(), dependency.getGroupId())
+                && Objects.equals(graphRoot.getArtifactId(), 
dependency.getArtifactId());
+    }
+}
diff --git 
a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcher.java
 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcher.java
new file mode 100644
index 00000000..34ff47b1
--- /dev/null
+++ 
b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcher.java
@@ -0,0 +1,50 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.maven.model.Dependency;
+
+class OrDependencyMatcher implements DependencyMatcher {
+
+    private Collection<DependencyMatcher> matchers;
+
+    OrDependencyMatcher(Collection<DependencyMatcher> matchers) {
+        this.matchers = new ArrayList<>(validate(matchers));
+    }
+
+    private Collection<DependencyMatcher> 
validate(Collection<DependencyMatcher> matchers) {
+        if (matchers == null || matchers.isEmpty()) {
+            throw new IllegalArgumentException("There must be at least 1 
dependencyMatcher");
+        }
+        return matchers;
+    }
+
+    @Override
+    public boolean matches(Dependency dependency) {
+        for (DependencyMatcher matcher : matchers) {
+            if (matcher.matches(dependency)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
diff --git a/src/site/apt/examples/copying-project-dependencies.apt.vm 
b/src/site/apt/examples/copying-project-dependencies.apt.vm
index 012dd1ce..f904e80d 100644
--- a/src/site/apt/examples/copying-project-dependencies.apt.vm
+++ b/src/site/apt/examples/copying-project-dependencies.apt.vm
@@ -96,4 +96,52 @@ Copying project dependencies
   </build>
   [...]
 </project>
-+---+
\ No newline at end of file
++---+
+
+* Subtrees
+
+  By default, the pom is considered the root of the dependency tree, but it 
can be useful to get the jars of one dependency and all its transitive 
dependencies. 
+  This can be done by specifying one or more <<graphRoots>>.
+
++---+
+<project>
+  [...]
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>${project.version}</version>
+        <executions>
+          <execution>
+            <id>copy-dependencies</id>
+            <phase>package</phase>
+            <goals>
+              <goal>copy-dependencies</goal>
+            </goals>
+            <configuration>
+              <graphRoots>
+                <graphRoot>
+                             <groupId>org.apache.maven.resolver</groupId>
+                             
<artifactId>maven-resolver-supplier-mvn4</artifactId>
+                </graphRoot>
+              </graphRoots>
+              <!-- move to separate directory, so it can get its own 
classloader -->
+              
<outputDirectory>\${project.build.directory}/lib-ext/maven-resolver-supplier-mvn4</outputDirectory>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+  [...]
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.resolver</groupId>
+      <artifactId>maven-resolver-supplier-mvn4</artifactId>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+</project>
++---+
+    
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcherTest.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcherTest.java
new file mode 100644
index 00000000..7a98baaf
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcherTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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 java.util.stream.Stream;
+
+import org.apache.maven.model.Dependency;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.NullSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class GraphRootMatcherTest {
+
+    @ParameterizedTest
+    @NullSource
+    void illegalArgument(GraphRoot root) {
+        assertThatThrownBy(() -> new 
GraphRootMatcher(root)).isInstanceOf(NullPointerException.class);
+    }
+
+    @ParameterizedTest
+    @MethodSource
+    void match(GraphRoot graphRoot, Dependency dependency, boolean result) {
+        assertThat(new 
GraphRootMatcher(graphRoot).matches(dependency)).isEqualTo(result);
+    }
+
+    static Stream<Arguments> match() {
+        return Stream.of(
+                Arguments.arguments(newGraphRoot("g", "a"), newDependency("g", 
"a"), true),
+                Arguments.arguments(newGraphRoot("x", "a"), newDependency("g", 
"a"), false),
+                Arguments.arguments(newGraphRoot("g", "x"), newDependency("g", 
"a"), false),
+                Arguments.arguments(newGraphRoot("g", "a"), newDependency("x", 
"a"), false),
+                Arguments.arguments(newGraphRoot("g", "a"), newDependency("g", 
"x"), false));
+    }
+
+    private static GraphRoot newGraphRoot(String groupId, String artifactId) {
+        GraphRoot graphRoot = new GraphRoot();
+        graphRoot.setGroupId(groupId);
+        graphRoot.setArtifactId(artifactId);
+        return graphRoot;
+    }
+
+    private static Dependency newDependency(String groupId, String artifactId) 
{
+        Dependency dependency = new Dependency();
+        dependency.setGroupId(groupId);
+        dependency.setArtifactId(artifactId);
+        return dependency;
+    }
+}
diff --git 
a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcherTest.java
 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcherTest.java
new file mode 100644
index 00000000..630381f6
--- /dev/null
+++ 
b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcherTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class OrDependencyMatcherTest {
+
+    @ParameterizedTest
+    @NullAndEmptySource
+    void illegalArgument(Collection<DependencyMatcher> source) {
+        assertThatThrownBy(() -> new OrDependencyMatcher(source))
+                .hasMessage("There must be at least 1 dependencyMatcher");
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "true, true",
+        "false, false",
+    })
+    void singleMatcher(boolean input, boolean result) {
+        Collection<DependencyMatcher> matchers = Arrays.asList(d -> input);
+        assertThat(new 
OrDependencyMatcher(matchers).matches(null)).isEqualTo(result);
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "true,  true,  true",
+        "true,  false, true",
+        "false, true,  true",
+        "false, false, false",
+    })
+    void doubleMatcher(boolean input1, boolean input2, boolean result) {
+        Collection<DependencyMatcher> matchers = Arrays.asList(d -> input1, d 
-> input2);
+        assertThat(new 
OrDependencyMatcher(matchers).matches(null)).isEqualTo(result);
+    }
+}

Reply via email to