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.graph;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.Set;
34  
35  import org.apache.maven.MavenExecutionException;
36  import org.apache.maven.ProjectCycleException;
37  import org.apache.maven.artifact.ArtifactUtils;
38  import org.apache.maven.execution.BuildResumptionDataRepository;
39  import org.apache.maven.execution.MavenExecutionRequest;
40  import org.apache.maven.execution.MavenSession;
41  import org.apache.maven.execution.ProjectActivation;
42  import org.apache.maven.execution.ProjectDependencyGraph;
43  import org.apache.maven.model.Plugin;
44  import org.apache.maven.model.building.DefaultModelProblem;
45  import org.apache.maven.model.building.Result;
46  import org.apache.maven.project.CycleDetectedException;
47  import org.apache.maven.project.DuplicateProjectException;
48  import org.apache.maven.project.MavenProject;
49  import org.apache.maven.project.ProjectBuildingException;
50  import org.apache.maven.project.collector.MultiModuleCollectionStrategy;
51  import org.apache.maven.project.collector.PomlessCollectionStrategy;
52  import org.apache.maven.project.collector.RequestPomCollectionStrategy;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import static java.util.Comparator.comparing;
57  
58  /**
59   * Builds the {@link ProjectDependencyGraph inter-dependencies graph} between projects in the reactor.
60   */
61  @Named(GraphBuilder.HINT)
62  @Singleton
63  public class DefaultGraphBuilder implements GraphBuilder {
64      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultGraphBuilder.class);
65  
66      private final BuildResumptionDataRepository buildResumptionDataRepository;
67      private final PomlessCollectionStrategy pomlessCollectionStrategy;
68      private final MultiModuleCollectionStrategy multiModuleCollectionStrategy;
69      private final RequestPomCollectionStrategy requestPomCollectionStrategy;
70      private final ProjectSelector projectSelector;
71  
72      /**
73       * @deprecated Use {@link #DefaultGraphBuilder(BuildResumptionDataRepository, PomlessCollectionStrategy,
74       * MultiModuleCollectionStrategy, RequestPomCollectionStrategy)} instead or rely on JSR 330
75       */
76      @Deprecated
77      public DefaultGraphBuilder() {
78          this(null, null, null, null);
79      }
80  
81      @Inject
82      public DefaultGraphBuilder(
83              BuildResumptionDataRepository buildResumptionDataRepository,
84              PomlessCollectionStrategy pomlessCollectionStrategy,
85              MultiModuleCollectionStrategy multiModuleCollectionStrategy,
86              RequestPomCollectionStrategy requestPomCollectionStrategy) {
87          this.buildResumptionDataRepository = buildResumptionDataRepository;
88          this.pomlessCollectionStrategy = pomlessCollectionStrategy;
89          this.multiModuleCollectionStrategy = multiModuleCollectionStrategy;
90          this.requestPomCollectionStrategy = requestPomCollectionStrategy;
91          this.projectSelector = new ProjectSelector(); // if necessary switch to DI
92      }
93  
94      @Override
95      public Result<ProjectDependencyGraph> build(MavenSession session) {
96          try {
97              Result<ProjectDependencyGraph> result = sessionDependencyGraph(session);
98  
99              if (result == null) {
100                 final List<MavenProject> projects = getProjectsForMavenReactor(session);
101                 validateProjects(projects, session.getRequest());
102                 processPackagingAttribute(projects, session.getRequest());
103                 enrichRequestFromResumptionData(projects, session.getRequest());
104                 result = reactorDependencyGraph(session, projects);
105             }
106 
107             return result;
108         } catch (final ProjectBuildingException | DuplicateProjectException | MavenExecutionException e) {
109             return Result.error(Collections.singletonList(new DefaultModelProblem(null, null, null, null, 0, 0, e)));
110         } catch (final CycleDetectedException e) {
111             String message = "The projects in the reactor contain a cyclic reference: " + e.getMessage();
112             ProjectCycleException error = new ProjectCycleException(message, e);
113             return Result.error(
114                     Collections.singletonList(new DefaultModelProblem(null, null, null, null, 0, 0, error)));
115         }
116     }
117 
118     private Result<ProjectDependencyGraph> sessionDependencyGraph(final MavenSession session)
119             throws CycleDetectedException, DuplicateProjectException {
120         Result<ProjectDependencyGraph> result = null;
121 
122         if (session.getProjectDependencyGraph() != null || session.getProjects() != null) {
123             final ProjectDependencyGraph graph =
124                     new DefaultProjectDependencyGraph(session.getAllProjects(), session.getProjects());
125 
126             result = Result.success(graph);
127         }
128 
129         return result;
130     }
131 
132     private Result<ProjectDependencyGraph> reactorDependencyGraph(MavenSession session, List<MavenProject> projects)
133             throws CycleDetectedException, DuplicateProjectException, MavenExecutionException {
134         ProjectDependencyGraph projectDependencyGraph = new DefaultProjectDependencyGraph(projects);
135         List<MavenProject> activeProjects = projectDependencyGraph.getSortedProjects();
136         List<MavenProject> allSortedProjects = projectDependencyGraph.getSortedProjects();
137         activeProjects = trimProjectsToRequest(activeProjects, projectDependencyGraph, session.getRequest());
138         activeProjects =
139                 trimSelectedProjects(activeProjects, allSortedProjects, projectDependencyGraph, session.getRequest());
140         activeProjects = trimResumedProjects(activeProjects, projectDependencyGraph, session.getRequest());
141         activeProjects = trimExcludedProjects(activeProjects, projectDependencyGraph, session.getRequest());
142 
143         if (activeProjects.size() != projectDependencyGraph.getSortedProjects().size()) {
144             projectDependencyGraph = new FilteredProjectDependencyGraph(projectDependencyGraph, activeProjects);
145         }
146 
147         return Result.success(projectDependencyGraph);
148     }
149 
150     private List<MavenProject> trimProjectsToRequest(
151             List<MavenProject> activeProjects, ProjectDependencyGraph graph, MavenExecutionRequest request)
152             throws MavenExecutionException {
153         List<MavenProject> result = activeProjects;
154 
155         if (request.getPom() != null) {
156             result = getProjectsInRequestScope(request, activeProjects);
157 
158             List<MavenProject> sortedProjects = graph.getSortedProjects();
159             result.sort(comparing(sortedProjects::indexOf));
160 
161             result = includeAlsoMakeTransitively(result, request, graph);
162         }
163 
164         return result;
165     }
166 
167     private List<MavenProject> trimSelectedProjects(
168             List<MavenProject> projects,
169             List<MavenProject> allSortedProjects,
170             ProjectDependencyGraph graph,
171             MavenExecutionRequest request)
172             throws MavenExecutionException {
173         List<MavenProject> result = projects;
174 
175         ProjectActivation projectActivation = request.getProjectActivation();
176         Set<String> requiredSelectors = projectActivation.getRequiredActiveProjectSelectors();
177         Set<String> optionalSelectors = projectActivation.getOptionalActiveProjectSelectors();
178         if (!requiredSelectors.isEmpty() || !optionalSelectors.isEmpty()) {
179             Set<MavenProject> selectedProjects = new HashSet<>(requiredSelectors.size() + optionalSelectors.size());
180             selectedProjects.addAll(
181                     projectSelector.getRequiredProjectsBySelectors(request, allSortedProjects, requiredSelectors));
182             selectedProjects.addAll(
183                     projectSelector.getOptionalProjectsBySelectors(request, allSortedProjects, optionalSelectors));
184 
185             // it can be empty when an optional project is missing from the reactor, fallback to returning all projects
186             if (!selectedProjects.isEmpty()) {
187                 result = new ArrayList<>(selectedProjects);
188 
189                 result = includeAlsoMakeTransitively(result, request, graph);
190 
191                 // Order the new list in the original order
192                 List<MavenProject> sortedProjects = graph.getSortedProjects();
193                 result.sort(comparing(sortedProjects::indexOf));
194             }
195         }
196 
197         return result;
198     }
199 
200     private List<MavenProject> trimResumedProjects(
201             List<MavenProject> projects, ProjectDependencyGraph graph, MavenExecutionRequest request)
202             throws MavenExecutionException {
203         List<MavenProject> result = projects;
204 
205         if (request.getResumeFrom() != null && !request.getResumeFrom().isEmpty()) {
206             File reactorDirectory = projectSelector.getBaseDirectoryFromRequest(request);
207 
208             String selector = request.getResumeFrom();
209 
210             MavenProject resumingFromProject = projects.stream()
211                     .filter(project -> projectSelector.isMatchingProject(project, selector, reactorDirectory))
212                     .findFirst()
213                     .orElseThrow(() -> new MavenExecutionException(
214                             "Could not find project to resume reactor build from: " + selector + " vs "
215                                     + formatProjects(projects),
216                             request.getPom()));
217             int resumeFromProjectIndex = projects.indexOf(resumingFromProject);
218             List<MavenProject> retainingProjects = result.subList(resumeFromProjectIndex, projects.size());
219 
220             result = includeAlsoMakeTransitively(retainingProjects, request, graph);
221         }
222 
223         return result;
224     }
225 
226     private List<MavenProject> trimExcludedProjects(
227             List<MavenProject> projects, ProjectDependencyGraph graph, MavenExecutionRequest request)
228             throws MavenExecutionException {
229         List<MavenProject> result = projects;
230 
231         ProjectActivation projectActivation = request.getProjectActivation();
232         Set<String> requiredSelectors = projectActivation.getRequiredInactiveProjectSelectors();
233         Set<String> optionalSelectors = projectActivation.getOptionalInactiveProjectSelectors();
234         if (!requiredSelectors.isEmpty() || !optionalSelectors.isEmpty()) {
235             Set<MavenProject> excludedProjects = new HashSet<>(requiredSelectors.size() + optionalSelectors.size());
236             List<MavenProject> allProjects = graph.getAllProjects();
237             excludedProjects.addAll(
238                     projectSelector.getRequiredProjectsBySelectors(request, allProjects, requiredSelectors));
239             excludedProjects.addAll(
240                     projectSelector.getOptionalProjectsBySelectors(request, allProjects, optionalSelectors));
241 
242             result = new ArrayList<>(projects);
243             result.removeAll(excludedProjects);
244 
245             if (result.isEmpty()) {
246                 boolean isPlural = excludedProjects.size() > 1;
247                 String message = String.format(
248                         "The project exclusion%s in --projects/-pl resulted in an "
249                                 + "empty reactor, please correct %s.",
250                         isPlural ? "s" : "", isPlural ? "them" : "it");
251                 throw new MavenExecutionException(message, request.getPom());
252             }
253         }
254 
255         return result;
256     }
257 
258     private List<MavenProject> includeAlsoMakeTransitively(
259             List<MavenProject> projects, MavenExecutionRequest request, ProjectDependencyGraph graph)
260             throws MavenExecutionException {
261         List<MavenProject> result = projects;
262 
263         String makeBehavior = request.getMakeBehavior();
264         boolean makeBoth = MavenExecutionRequest.REACTOR_MAKE_BOTH.equals(makeBehavior);
265 
266         boolean makeUpstream = makeBoth || MavenExecutionRequest.REACTOR_MAKE_UPSTREAM.equals(makeBehavior);
267         boolean makeDownstream = makeBoth || MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM.equals(makeBehavior);
268 
269         if ((makeBehavior != null && !makeBehavior.isEmpty()) && !makeUpstream && !makeDownstream) {
270             throw new MavenExecutionException("Invalid reactor make behavior: " + makeBehavior, request.getPom());
271         }
272 
273         if (makeUpstream || makeDownstream) {
274             Set<MavenProject> projectsSet = new HashSet<>(projects);
275 
276             for (MavenProject project : projects) {
277                 if (makeUpstream) {
278                     projectsSet.addAll(graph.getUpstreamProjects(project, true));
279                 }
280                 if (makeDownstream) {
281                     projectsSet.addAll(graph.getDownstreamProjects(project, true));
282                 }
283             }
284 
285             result = new ArrayList<>(projectsSet);
286 
287             // Order the new list in the original order
288             List<MavenProject> sortedProjects = graph.getSortedProjects();
289             result.sort(comparing(sortedProjects::indexOf));
290         }
291 
292         return result;
293     }
294 
295     private void enrichRequestFromResumptionData(List<MavenProject> projects, MavenExecutionRequest request) {
296         if (request.isResume()) {
297             projects.stream()
298                     .filter(MavenProject::isExecutionRoot)
299                     .findFirst()
300                     .ifPresent(rootProject -> buildResumptionDataRepository.applyResumptionData(request, rootProject));
301         }
302     }
303 
304     private List<MavenProject> getProjectsInRequestScope(MavenExecutionRequest request, List<MavenProject> projects)
305             throws MavenExecutionException {
306         if (request.getPom() == null) {
307             return projects;
308         }
309 
310         MavenProject requestPomProject = projects.stream()
311                 .filter(project -> request.getPom().equals(project.getFile()))
312                 .findFirst()
313                 .orElseThrow(() -> new MavenExecutionException(
314                         "Could not find a project in reactor matching the request POM", request.getPom()));
315 
316         List<MavenProject> modules = requestPomProject.getCollectedProjects() != null
317                 ? requestPomProject.getCollectedProjects()
318                 : Collections.emptyList();
319 
320         List<MavenProject> result = new ArrayList<>(modules);
321         result.add(requestPomProject);
322         return result;
323     }
324 
325     private String formatProjects(List<MavenProject> projects) {
326         StringBuilder projectNames = new StringBuilder();
327         Iterator<MavenProject> iterator = projects.iterator();
328         while (iterator.hasNext()) {
329             MavenProject project = iterator.next();
330             projectNames.append(project.getGroupId()).append(":").append(project.getArtifactId());
331             if (iterator.hasNext()) {
332                 projectNames.append(", ");
333             }
334         }
335         return projectNames.toString();
336     }
337 
338     // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
339     //
340     // Project collection
341     //
342     // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
343 
344     private List<MavenProject> getProjectsForMavenReactor(MavenSession session) throws ProjectBuildingException {
345         MavenExecutionRequest request = session.getRequest();
346         request.getProjectBuildingRequest().setRepositorySession(session.getRepositorySession());
347 
348         // 1. Collect project for invocation without a POM.
349         if (request.getPom() == null) {
350             return pomlessCollectionStrategy.collectProjects(request);
351         }
352 
353         // 2. Collect projects for all modules in the multi-module project.
354         if (request.getMakeBehavior() != null || !request.getProjectActivation().isEmpty()) {
355             List<MavenProject> projects = multiModuleCollectionStrategy.collectProjects(request);
356             if (!projects.isEmpty()) {
357                 return projects;
358             }
359         }
360 
361         // 3. Collect projects for explicitly requested POM.
362         return requestPomCollectionStrategy.collectProjects(request);
363     }
364 
365     private void validateProjects(List<MavenProject> projects, MavenExecutionRequest request)
366             throws MavenExecutionException {
367         Map<String, MavenProject> projectsMap = new HashMap<>();
368 
369         List<MavenProject> projectsInRequestScope = getProjectsInRequestScope(request, projects);
370         for (MavenProject p : projectsInRequestScope) {
371             String projectKey = ArtifactUtils.key(p.getGroupId(), p.getArtifactId(), p.getVersion());
372 
373             projectsMap.put(projectKey, p);
374         }
375 
376         for (MavenProject project : projects) {
377             // MNG-1911 / MNG-5572: Building plugins with extensions cannot be part of reactor
378             for (Plugin plugin : project.getBuildPlugins()) {
379                 if (plugin.isExtensions()) {
380                     String pluginKey =
381                             ArtifactUtils.key(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion());
382 
383                     if (projectsMap.containsKey(pluginKey)) {
384                         LOGGER.warn(
385                                 "'{}' uses '{}' as extension which is not possible within the same reactor build. "
386                                         + "This plugin was pulled from the local repository!",
387                                 project.getName(),
388                                 plugin.getKey());
389                     }
390                 }
391             }
392         }
393     }
394 
395     private void processPackagingAttribute(List<MavenProject> projects, MavenExecutionRequest request)
396             throws MavenExecutionException {
397         List<MavenProject> projectsInRequestScope = getProjectsInRequestScope(request, projects);
398         for (MavenProject p : projectsInRequestScope) {
399             if ("bom".equals(p.getPackaging())) {
400                 LOGGER.info(
401                         "The packaging attribute of the '{}' project is configured as 'bom' and changed to 'pom'",
402                         p.getName());
403                 p.setPackaging("pom");
404             }
405         }
406     }
407 }