1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.internal;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
30 import org.apache.maven.project.MavenProject;
31
32
33
34
35
36
37 @Named
38 @Singleton
39 public class BuildListCalculator {
40 public ProjectBuildList calculateProjectBuilds(MavenSession session, List<TaskSegment> taskSegments) {
41 List<ProjectSegment> projectBuilds = new ArrayList<>();
42
43 MavenProject rootProject = session.getTopLevelProject();
44
45 for (TaskSegment taskSegment : taskSegments) {
46 List<MavenProject> projects;
47
48 if (taskSegment.isAggregating()) {
49 projects = Collections.singletonList(rootProject);
50 } else {
51 projects = session.getProjects();
52 }
53 for (MavenProject project : projects) {
54 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
55 MavenProject currentProject = session.getCurrentProject();
56 try {
57 BuilderCommon.attachToThread(project);
58 session.setCurrentProject(project);
59 projectBuilds.add(new ProjectSegment(project, taskSegment, session));
60 } finally {
61 session.setCurrentProject(currentProject);
62 Thread.currentThread().setContextClassLoader(tccl);
63 }
64 }
65 }
66 return new ProjectBuildList(projectBuilds);
67 }
68 }