This is an automated email from the ASF dual-hosted git repository. rfscholte pushed a commit to branch copy-deps in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit 0bb8bd35174a9ff9640639bf8c86c34b13256632 Author: Robert Scholte <[email protected]> AuthorDate: Mon Dec 29 13:34:10 2025 +0100 Introduce graphRoots for dependencyFilter based mojos --- pom.xml | 2 +- .../invoker.properties | 18 ++++ .../projects/copy-dependencies-graphroots/pom.xml | 103 +++++++++++++++++++++ .../copy-dependencies-graphroots/verify.groovy | 37 ++++++++ .../AbstractDependencyFilterMojo.java | 57 +++++++++++- .../fromDependencies/DependencyMatcher.java | 27 ++++++ .../dependency/fromDependencies/GraphRoot.java | 55 +++++++++++ .../fromDependencies/GraphRootMatcher.java | 41 ++++++++ .../fromDependencies/OrDependencyMatcher.java | 43 +++++++++ .../examples/copying-project-dependencies.apt.vm | 50 +++++++++- 10 files changed, 430 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 1adbdf52..70702cc0 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/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..ff7f98d4 --- /dev/null +++ b/src/it/projects/copy-dependencies-graphroots/pom.xml @@ -0,0 +1,103 @@ +<?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 + </description> + + <dependencies> + <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> + </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..605f3248 --- /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'] +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() == 3 && 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..d8ab89a6 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 matches the project itself is the root. + * With graphRoots you can choose for a subtree of dependencies. + * 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,38 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj return resolvedArtifacts; } + private Set<Artifact> collectArtifacts(MavenProject project) throws DependencyResolutionException { + if (graphRoots == null || graphRoots.isEmpty()) { + return project.getArtifacts(); + } else { + 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 = + RepositoryUtils.toRepos(session.getProjectBuildingRequest().getRemoteRepositories()); + + 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..36f5c055 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRoot.java @@ -0,0 +1,55 @@ +/* + * 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; + +/** + * All values are static, no expression, so matcher can use equals + */ +public class GraphRoot { + + private String groupId; + + private String artifactId; + + private String type; + + 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; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} 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..b4edb8fb --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/GraphRootMatcher.java @@ -0,0 +1,41 @@ +/* + * 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; + +class GraphRootMatcher implements DependencyMatcher { + + private final GraphRoot graphRoot; + + GraphRootMatcher(GraphRoot graphRoot) { + this.graphRoot = graphRoot; + } + + @Override + public boolean matches(Dependency dependency) { + return matches(graphRoot.getGroupId(), dependency.getGroupId()) + && matches(graphRoot.getArtifactId(), dependency.getArtifactId()) + && matches(graphRoot.getType(), dependency.getType()); + } + + private static boolean matches(String filterValue, String nodeValue) { + return filterValue == null || filterValue.equals(nodeValue); + } +} 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..44e87bfa --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/OrDependencyMatcher.java @@ -0,0 +1,43 @@ +/* + * 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<>(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..d85848d4 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 graphRoot, 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
