View Javadoc
1   package org.apache.maven.project.collector;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.execution.MavenExecutionRequest;
23  import org.apache.maven.model.building.ModelProblem;
24  import org.apache.maven.model.building.ModelProblemUtils;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.project.ProjectBuilder;
27  import org.apache.maven.project.ProjectBuildingException;
28  import org.apache.maven.project.ProjectBuildingRequest;
29  import org.apache.maven.project.ProjectBuildingResult;
30  import org.codehaus.plexus.util.StringUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.inject.Inject;
35  import javax.inject.Named;
36  import javax.inject.Singleton;
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.List;
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  {
48      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultProjectsSelector.class );
49      private final ProjectBuilder projectBuilder;
50  
51      @Inject
52      public DefaultProjectsSelector( ProjectBuilder projectBuilder )
53      {
54          this.projectBuilder = projectBuilder;
55      }
56  
57      @Override
58      public List<MavenProject> selectProjects( List<File> files, MavenExecutionRequest request )
59              throws ProjectBuildingException
60      {
61          ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest();
62  
63          boolean hasProjectSelection = !request.getProjectActivation().isEmpty();
64          boolean isRecursive = hasProjectSelection || request.isRecursive();
65          List<ProjectBuildingResult> results = projectBuilder.build( files, isRecursive, projectBuildingRequest );
66  
67          List<MavenProject> projects = new ArrayList<>( results.size() );
68  
69          boolean problems = false;
70  
71          for ( ProjectBuildingResult result : results )
72          {
73              projects.add( result.getProject() );
74  
75              if ( !result.getProblems().isEmpty() && LOGGER.isWarnEnabled() )
76              {
77                  LOGGER.warn( "" );
78                  LOGGER.warn( "Some problems were encountered while building the effective model for '{}'",
79                          result.getProject().getId() );
80  
81                  for ( ModelProblem problem : result.getProblems() )
82                  {
83                      String loc = ModelProblemUtils.formatLocation( problem, result.getProjectId() );
84                      LOGGER.warn( "{}{}", problem.getMessage(), ( StringUtils.isNotEmpty( loc ) ? " @ " + loc : "" ) );
85                  }
86  
87                  problems = true;
88              }
89          }
90  
91          if ( problems )
92          {
93              LOGGER.warn( "" );
94              LOGGER.warn( "It is highly recommended to fix these problems"
95                      + " because they threaten the stability of your build." );
96              LOGGER.warn( "" );
97              LOGGER.warn( "For this reason, future Maven versions might no"
98                      + " longer support building such malformed projects." );
99              LOGGER.warn( "" );
100         }
101 
102         return projects;
103     }
104 }