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