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.builder.multithreaded;
20
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.execution.ProjectDependencyGraph;
28 import org.apache.maven.lifecycle.internal.ProjectBuildList;
29 import org.apache.maven.lifecycle.internal.ProjectSegment;
30 import org.apache.maven.project.MavenProject;
31
32
33
34
35
36
37
38
39
40 public class ConcurrencyDependencyGraph {
41
42 private final ProjectBuildList projectBuilds;
43
44 private final ProjectDependencyGraph projectDependencyGraph;
45
46 private final Set<MavenProject> finishedProjects = new HashSet<>();
47
48 public ConcurrencyDependencyGraph(ProjectBuildList projectBuilds, ProjectDependencyGraph projectDependencyGraph) {
49 this.projectDependencyGraph = projectDependencyGraph;
50 this.projectBuilds = projectBuilds;
51 }
52
53 public int getNumberOfBuilds() {
54 return projectBuilds.size();
55 }
56
57
58
59
60
61
62 public List<MavenProject> getRootSchedulableBuilds() {
63 Set<MavenProject> result = new LinkedHashSet<>();
64 for (ProjectSegment projectBuild : projectBuilds) {
65 if (projectDependencyGraph
66 .getUpstreamProjects(projectBuild.getProject(), false)
67 .isEmpty()) {
68 result.add(projectBuild.getProject());
69 }
70 }
71 if (result.isEmpty() && projectBuilds.size() > 0) {
72
73 result.add(projectBuilds.get(0).getProject());
74 }
75 return new ArrayList<>(result);
76 }
77
78
79
80
81
82
83
84 public List<MavenProject> markAsFinished(MavenProject mavenProject) {
85 finishedProjects.add(mavenProject);
86 return getSchedulableNewProcesses(mavenProject);
87 }
88
89 private List<MavenProject> getSchedulableNewProcesses(MavenProject finishedProject) {
90 List<MavenProject> result = new ArrayList<>();
91
92 for (MavenProject dependentProject : projectDependencyGraph.getDownstreamProjects(finishedProject, false)) {
93 final List<MavenProject> upstreamProjects =
94 projectDependencyGraph.getUpstreamProjects(dependentProject, false);
95 if (finishedProjects.containsAll(upstreamProjects)) {
96 result.add(dependentProject);
97 }
98 }
99 return result;
100 }
101
102
103
104
105 public Set<MavenProject> getUnfinishedProjects() {
106 Set<MavenProject> unfinished = new HashSet<>(projectBuilds.getProjects());
107 unfinished.removeAll(finishedProjects);
108 return unfinished;
109 }
110
111
112
113
114 protected Set<MavenProject> getFinishedProjects() {
115 return finishedProjects;
116 }
117
118 protected ProjectBuildList getProjectBuilds() {
119 return projectBuilds;
120 }
121
122
123
124
125
126
127
128 protected List<MavenProject> getDependencies(MavenProject p) {
129 return projectDependencyGraph.getUpstreamProjects(p, false);
130 }
131
132
133
134
135
136
137
138 public List<MavenProject> getActiveDependencies(MavenProject p) {
139 List<MavenProject> activeDependencies = projectDependencyGraph.getUpstreamProjects(p, false);
140 activeDependencies.removeAll(finishedProjects);
141 return activeDependencies;
142 }
143 }