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.project.collector;
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.List;
28  
29  import org.apache.maven.execution.MavenExecutionRequest;
30  import org.apache.maven.model.building.ModelProblem;
31  import org.apache.maven.model.building.ModelProblemUtils;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.ProjectBuilder;
34  import org.apache.maven.project.ProjectBuildingException;
35  import org.apache.maven.project.ProjectBuildingRequest;
36  import org.apache.maven.project.ProjectBuildingResult;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Utility to select projects for a given set of pom.xml files.
42   */
43  @Named
44  @Singleton
45  public class DefaultProjectsSelector implements ProjectsSelector {
46      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultProjectsSelector.class);
47      private final ProjectBuilder projectBuilder;
48  
49      @Inject
50      public DefaultProjectsSelector(ProjectBuilder projectBuilder) {
51          this.projectBuilder = projectBuilder;
52      }
53  
54      @Override
55      public List<MavenProject> selectProjects(List<File> files, MavenExecutionRequest request)
56              throws ProjectBuildingException {
57          ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest();
58  
59          boolean hasProjectSelection = !request.getProjectActivation().isEmpty();
60          boolean isRecursive = hasProjectSelection || request.isRecursive();
61          List<ProjectBuildingResult> results = projectBuilder.build(files, isRecursive, projectBuildingRequest);
62  
63          List<MavenProject> projects = new ArrayList<>(results.size());
64  
65          boolean problems = false;
66  
67          for (ProjectBuildingResult result : results) {
68              projects.add(result.getProject());
69  
70              if (!result.getProblems().isEmpty() && LOGGER.isWarnEnabled()) {
71                  LOGGER.warn("");
72                  LOGGER.warn(
73                          "Some problems were encountered while building the effective model for '{}'",
74                          result.getProject().getId());
75  
76                  for (ModelProblem problem : result.getProblems()) {
77                      String loc = ModelProblemUtils.formatLocation(problem, result.getProjectId());
78                      LOGGER.warn("{}{}", problem.getMessage(), ((loc != null && !loc.isEmpty()) ? " @ " + loc : ""));
79                  }
80  
81                  problems = true;
82              }
83          }
84  
85          if (problems) {
86              LOGGER.warn("");
87              LOGGER.warn("It is highly recommended to fix these problems"
88                      + " because they threaten the stability of your build.");
89              LOGGER.warn("");
90              LOGGER.warn("For this reason, future Maven versions might no"
91                      + " longer support building such malformed projects.");
92              LOGGER.warn("");
93          }
94  
95          return projects;
96      }
97  }