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