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