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.plugins.pmd;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Provider;
24  import javax.inject.Singleton;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Optional;
32  import java.util.stream.Collectors;
33  import java.util.stream.Stream;
34  
35  import org.apache.maven.RepositoryUtils;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.model.Dependency;
38  import org.apache.maven.model.DependencyManagement;
39  import org.apache.maven.project.MavenProject;
40  import org.eclipse.aether.RepositorySystemSession;
41  import org.eclipse.aether.artifact.Artifact;
42  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
43  import org.eclipse.aether.collection.CollectRequest;
44  import org.eclipse.aether.resolution.ArtifactResult;
45  import org.eclipse.aether.resolution.DependencyRequest;
46  import org.eclipse.aether.resolution.DependencyResolutionException;
47  import org.eclipse.aether.resolution.DependencyResult;
48  
49  /**
50   * utils service for provide configuration needed to execute CPD/PMD.
51   */
52  @Named
53  @Singleton
54  public class ConfigurationService {
55  
56      private final Provider<MavenSession> sessionProvider;
57  
58      private final org.eclipse.aether.RepositorySystem repositorySystem;
59  
60      @Inject
61      public ConfigurationService(
62              Provider<MavenSession> sessionProvider, org.eclipse.aether.RepositorySystem repositorySystem) {
63          this.sessionProvider = sessionProvider;
64          this.repositorySystem = repositorySystem;
65      }
66  
67      public List<File> resolveDependenciesAsFile(
68              MavenProject localProject, Collection<MavenProject> aggregatedProjects, boolean includeTests)
69              throws DependencyResolutionException {
70  
71          RepositorySystemSession repositorySession = sessionProvider.get().getRepositorySession();
72          ArtifactTypeRegistry artifactTypeRegistry = repositorySession.getArtifactTypeRegistry();
73  
74          List<String> includesScope =
75                  includeTests ? Arrays.asList("compile", "provided", "test") : Arrays.asList("compile", "provided");
76  
77          // collect exclusions for projects within the reactor
78          // if module a depends on module b and both are in the reactor
79          // then we don't want to resolve the dependency as an artifact.
80          List<String> exclusionPatterns = new ArrayList<>();
81          for (MavenProject project : aggregatedProjects) {
82              exclusionPatterns.add(getExclusionKey(project));
83          }
84  
85          List<org.eclipse.aether.graph.Dependency> dependencies = localProject.getDependencies().stream()
86                  .filter(dependency -> includesScope.contains(dependency.getScope()))
87                  .filter(dependency -> !exclusionPatterns.contains(getExclusionKey(dependency)))
88                  .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
89                  .collect(Collectors.toList());
90  
91          List<org.eclipse.aether.graph.Dependency> dependencyManagements = Optional.ofNullable(
92                          localProject.getDependencyManagement())
93                  .map(DependencyManagement::getDependencies)
94                  .map(Collection::stream)
95                  .orElse(Stream.empty())
96                  .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
97                  .collect(Collectors.toList());
98  
99          CollectRequest collectRequest =
100                 new CollectRequest(dependencies, dependencyManagements, localProject.getRemoteProjectRepositories());
101         DependencyRequest request = new DependencyRequest(collectRequest, null);
102 
103         DependencyResult result =
104                 repositorySystem.resolveDependencies(sessionProvider.get().getRepositorySession(), request);
105 
106         return result.getArtifactResults().stream()
107                 .map(ArtifactResult::getArtifact)
108                 .map(Artifact::getFile)
109                 .collect(Collectors.toList());
110     }
111 
112     private String getExclusionKey(Dependency dependency) {
113         return dependency.getGroupId() + ":" + dependency.getArtifactId();
114     }
115 
116     private String getExclusionKey(MavenProject project) {
117         return project.getGroupId() + ":" + project.getArtifactId();
118     }
119 }