diff --git a/pom.xml b/pom.xml
index 1adbdf522..70702cc08 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@ under the License.
maven-dependency-plugin
- 3.9.1-SNAPSHOT
+ 3.10.0-SNAPSHOT
maven-plugin
Apache Maven Dependency Plugin
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 000000000..0ee601048
--- /dev/null
+++ b/src/it/mrm/repository/d-without-dep-3.2.1.pom
@@ -0,0 +1,30 @@
+
+
+
+
+ 4.0.0
+
+ org.apache.maven.its.dependencies
+ d-without-dep
+ 3.2.1
+
+
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 000000000..f50e476fd
--- /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 000000000..d030bf28a
--- /dev/null
+++ b/src/it/projects/copy-dependencies-graphroots/pom.xml
@@ -0,0 +1,112 @@
+
+
+
+
+ 4.0.0
+
+
+ org.apache
+ apache
+ 5
+
+
+ org.apache.maven.its.dependency
+ test
+ 1.0-SNAPSHOT
+
+ Test
+
+ Test dependency:copy-dependencies using graphRoots
+
+
+
+
+ org.apache.maven.its.dependencies
+ d-without-dep
+ 3.2.1
+
+
+ org.apache.maven.its.dependency
+ a-with-dep
+ 1.0.0
+ provided
+
+
+ org.apache.maven.its.dependency
+ get-artifact
+ 1.0
+ provided
+
+
+
+
+ UTF-8
+
+
+
+
+ package
+
+
+
+ maven-dependency-plugin
+ @project.version@
+
+
+ test-1
+
+ copy-dependencies
+
+
+
+
+ org.apache.maven.its.dependency
+ a-with-dep
+
+
+ org.apache.maven.its.dependencies
+ d-without-dep
+
+
+ ${project.build.directory}/it/copy-dep-test-1
+
+
+
+ test-2
+
+ copy-dependencies
+
+
+
+
+ org.apache.maven.its.dependency
+ get-artifact
+
+
+ ${project.build.directory}/it/copy-dep-test-2
+
+
+
+
+
+
+
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 000000000..1d70b6590
--- /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 992807f98..0463aa3db 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 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.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 graphRoots;
+
private final ResolverUtil resolverUtil;
private final ProjectBuilder projectBuilder;
@@ -292,6 +308,7 @@ protected DependencyStatusSets getDependencySets(boolean stopOnFailure) throws M
*/
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 @@ protected DependencyStatusSets getDependencySets(boolean stopOnFailure, boolean
DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));
// start with all artifacts.
- Set artifacts = getProject().getArtifacts();
+ Set artifacts;
+
+ try {
+ artifacts = collectArtifacts(getProject());
+ } catch (DependencyResolutionException e) {
+ throw new MojoExecutionException("Failed to collect artifacts", e);
+ }
if (includeParents) {
// add dependencies parents
@@ -479,6 +502,42 @@ private Set resolve(Set artifact
return resolvedArtifacts;
}
+ private Set 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 filterMatchers =
+ graphRoots.stream().map(GraphRootMatcher::new).collect(Collectors.toList());
+
+ DependencyMatcher subTreeMatcher = new OrDependencyMatcher(filterMatchers);
+
+ Set artifacts = new HashSet<>();
+ for (Dependency dep : project.getDependencies()) {
+ if (subTreeMatcher.matches(dep)) {
+ artifacts.addAll(resolveDependencyArtifacts(dep));
+ }
+ }
+ return artifacts;
+ }
+ }
+
+ private Set resolveDependencyArtifacts(Dependency root) throws DependencyResolutionException {
+ org.eclipse.aether.graph.Dependency dependency = RepositoryUtils.toDependency(
+ root, session.getRepositorySession().getArtifactTypeRegistry());
+
+ List remoteRepositories =
+ RepositoryUtils.toRepos(session.getProjectBuildingRequest().getRemoteRepositories());
+
+ Collection 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 000000000..f5b0acb80
--- /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 000000000..e85cb3ec0
--- /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 000000000..7bba0adf9
--- /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 000000000..34ff47b16
--- /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 matchers;
+
+ OrDependencyMatcher(Collection matchers) {
+ this.matchers = new ArrayList<>(validate(matchers));
+ }
+
+ private Collection validate(Collection 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 012dd1ce1..f904e80d7 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
[...]
-+---+
\ 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 <>.
+
++---+
+
+ [...]
+
+
+
+ org.apache.maven.plugins
+ maven-dependency-plugin
+ ${project.version}
+
+
+ copy-dependencies
+ package
+
+ copy-dependencies
+
+
+
+
+ org.apache.maven.resolver
+ maven-resolver-supplier-mvn4
+
+
+
+ \${project.build.directory}/lib-ext/maven-resolver-supplier-mvn4
+
+
+
+
+
+
+ [...]
+
+
+ org.apache.maven.resolver
+ maven-resolver-supplier-mvn4
+ provided
+
+
+
++---+
+
\ 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 000000000..7a98baafe
--- /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 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 000000000..630381f6e
--- /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 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 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 matchers = Arrays.asList(d -> input1, d -> input2);
+ assertThat(new OrDependencyMatcher(matchers).matches(null)).isEqualTo(result);
+ }
+}