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