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