-
Notifications
You must be signed in to change notification settings - Fork 185
Introduce graphRoots for dependencyFilter based mojos #1571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0bb8bd3
217a0b1
be1f53a
1824a93
8b27e36
ffba5a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<GraphRoot> graphRoots; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As |
||
|
|
||
| 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<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,42 @@ private Set<Artifact> resolve(Set<org.eclipse.aether.artifact.Artifact> artifact | |
| 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)); | ||
| } | ||
| } | ||
mthmulders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is |
||
|
|
||
| Collection<org.eclipse.aether.artifact.Artifact> depArtifacts = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify: we do full (transitive) dependency resolution only when the user specified one or more
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The project dependencies have already been resolved, as the goal of the final Mojo contains
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get it. Maybe worth copying that text to an inline comment, in order to prevent future maintainers from asking themselves why the code is there.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| resolverUtil.resolveDependencies(dependency.getArtifact(), remoteRepositories); | ||
|
|
||
| return depArtifacts.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| /** | ||
| * @return returns the markersDirectory | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.