1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }