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 if ( result.isEmpty() && projectBuilds.size() > 0 )
79 {
80
81 result.add( projectBuilds.get( 0 ).getProject() );
82 }
83 return new ArrayList<>( result );
84 }
85
86
87
88
89
90
91
92 public List<MavenProject> markAsFinished( MavenProject mavenProject )
93 {
94 finishedProjects.add( mavenProject );
95 return getSchedulableNewProcesses( mavenProject );
96 }
97
98 private List<MavenProject> getSchedulableNewProcesses( MavenProject finishedProject )
99 {
100 List<MavenProject> result = new ArrayList<>();
101
102 for ( MavenProject dependentProject : projectDependencyGraph.getDownstreamProjects( finishedProject, false ) )
103 {
104 final List<MavenProject> upstreamProjects =
105 projectDependencyGraph.getUpstreamProjects( dependentProject, false );
106 if ( finishedProjects.containsAll( upstreamProjects ) )
107 {
108 result.add( dependentProject );
109 }
110 }
111 return result;
112 }
113
114
115
116
117 public Set<MavenProject> getUnfinishedProjects()
118 {
119 Set<MavenProject> unfinished = new HashSet<>( projectBuilds.getProjects() );
120 unfinished.removeAll( finishedProjects );
121 return unfinished;
122 }
123
124
125
126
127 protected Set<MavenProject> getFinishedProjects()
128 {
129 return finishedProjects;
130 }
131
132 protected ProjectBuildList getProjectBuilds()
133 {
134 return projectBuilds;
135 }
136
137
138
139
140
141
142
143 protected List<MavenProject> getDependencies( MavenProject p )
144 {
145 return projectDependencyGraph.getUpstreamProjects( p, false );
146 }
147
148
149
150
151
152
153
154 public List<MavenProject> getActiveDependencies( MavenProject p )
155 {
156 List<MavenProject> activeDependencies = projectDependencyGraph.getUpstreamProjects( p, false );
157 activeDependencies.removeAll( finishedProjects );
158 return activeDependencies;
159 }
160 }