1 package org.apache.maven.shared.artifact.filter.collection;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.RepositoryUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.project.DefaultProjectBuildingRequest;
25 import org.apache.maven.project.DependencyResolutionResult;
26 import org.apache.maven.project.ProjectBuilder;
27 import org.apache.maven.project.ProjectBuildingException;
28 import org.apache.maven.project.ProjectBuildingRequest;
29 import org.apache.maven.project.ProjectBuildingResult;
30 import org.eclipse.aether.graph.Dependency;
31
32 import java.util.LinkedHashSet;
33 import java.util.Set;
34
35 /**
36 * This filter will exclude everything that is not a dependency of the selected artifact.
37 *
38 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39 */
40 public class ArtifactTransitivityFilter
41 extends AbstractArtifactsFilter
42 {
43 /**
44 * List of dependencyConflictIds of transitiveArtifacts
45 */
46 private Set<String> transitiveArtifacts;
47
48 /**
49 * <p>
50 * Use {@link org.apache.maven.execution.MavenSession#getProjectBuildingRequest()} to get the buildingRequest.
51 * The projectBuilder should be resolved with CDI.
52 * </p>
53 * <pre>
54 * // For Mojo
55 * @Component
56 * private ProjectBuilder projectBuilder;
57 *
58 * // For Components
59 * @Requirement // or @Inject
60 * private ProjectBuilder projectBuilder;
61 * </pre>
62 *
63 * @param artifact the artifact to resolve the dependencies from
64 * @param buildingRequest the buildingRequest
65 * @param projectBuilder the projectBuilder
66 * @throws ProjectBuildingException if the project descriptor could not be successfully built
67 */
68 public ArtifactTransitivityFilter( Artifact artifact, ProjectBuildingRequest buildingRequest,
69 ProjectBuilder projectBuilder )
70 throws ProjectBuildingException
71 {
72 ProjectBuildingRequest request = new DefaultProjectBuildingRequest( buildingRequest );
73
74 request.setResolveDependencies( true );
75
76 ProjectBuildingResult buildingResult = projectBuilder.build( artifact, request );
77
78 DependencyResolutionResult resolutionResult = buildingResult.getDependencyResolutionResult();
79 if ( resolutionResult != null )
80 {
81 for ( Dependency dependency : resolutionResult.getDependencies() )
82 {
83 Artifact mavenArtifact = RepositoryUtils.toArtifact( dependency.getArtifact() );
84 transitiveArtifacts.add( mavenArtifact.getDependencyConflictId() );
85 }
86 }
87 }
88
89 /** {@inheritDoc} */
90 public Set<Artifact> filter( Set<Artifact> artifacts )
91 {
92 Set<Artifact> result = new LinkedHashSet<>();
93 for ( Artifact artifact : artifacts )
94 {
95 if ( artifactIsATransitiveDependency( artifact ) )
96 {
97 result.add( artifact );
98 }
99 }
100 return result;
101 }
102
103 /**
104 * Compares the artifact to the list of dependencies to see if it is directly included by this project
105 *
106 * @param artifact representing the item to compare.
107 * @return true if artifact is a transitive dependency
108 */
109 public boolean artifactIsATransitiveDependency( Artifact artifact )
110 {
111 return transitiveArtifacts.contains( artifact.getDependencyConflictId() );
112 }
113 }