1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.maven.lifecycle.internal.stub;
17
18 import org.apache.maven.execution.AbstractExecutionListener;
19 import org.apache.maven.execution.DefaultMavenExecutionRequest;
20 import org.apache.maven.execution.DefaultMavenExecutionResult;
21 import org.apache.maven.execution.MavenExecutionRequest;
22 import org.apache.maven.execution.MavenSession;
23 import org.apache.maven.execution.ProjectDependencyGraph;
24 import org.apache.maven.lifecycle.LifecycleNotFoundException;
25 import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
26 import org.apache.maven.lifecycle.internal.GoalTask;
27 import org.apache.maven.lifecycle.internal.ProjectBuildList;
28 import org.apache.maven.lifecycle.internal.ProjectSegment;
29 import org.apache.maven.lifecycle.internal.TaskSegment;
30 import org.apache.maven.plugin.InvalidPluginDescriptorException;
31 import org.apache.maven.plugin.MojoNotFoundException;
32 import org.apache.maven.plugin.PluginDescriptorParsingException;
33 import org.apache.maven.plugin.PluginNotFoundException;
34 import org.apache.maven.plugin.PluginResolutionException;
35 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
36 import org.apache.maven.plugin.version.PluginVersionResolutionException;
37 import org.apache.maven.project.MavenProject;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class ProjectDependencyGraphStub
58 implements ProjectDependencyGraph
59 {
60 public static final MavenProject A = new MavenProject();
61
62 public static final MavenProject B = new MavenProject();
63
64 public static final MavenProject C = new MavenProject();
65
66 public static final MavenProject X = new MavenProject();
67
68 public static final MavenProject Y = new MavenProject();
69
70 public static final MavenProject Z = new MavenProject();
71
72 public static final MavenProject UNKNOWN = new MavenProject();
73
74 static
75 {
76 A.setArtifactId( "A" );
77 B.setArtifactId( "B" );
78 C.setArtifactId( "C" );
79 X.setArtifactId( "X" );
80 Y.setArtifactId( "Y" );
81 Z.setArtifactId( "Z" );
82 }
83
84
85
86 public static ProjectBuildList getProjectBuildList( MavenSession session )
87 throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
88 NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
89 LifecyclePhaseNotFoundException, LifecycleNotFoundException
90 {
91 final List<ProjectSegment> list = getProjectBuilds( session );
92 return new ProjectBuildList( list );
93
94 }
95
96 public static List<ProjectSegment> getProjectBuilds( MavenSession session )
97 throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
98 NoPluginFoundForPrefixException, PluginNotFoundException, MojoNotFoundException, PluginResolutionException,
99 LifecyclePhaseNotFoundException, LifecycleNotFoundException
100 {
101 List<ProjectSegment> projectBuilds = new ArrayList<>();
102
103 TaskSegment segment = createTaskSegment();
104 projectBuilds.add( createProjectBuild( A, session, segment ) );
105 projectBuilds.add( createProjectBuild( B, session, segment ) );
106 projectBuilds.add( createProjectBuild( C, session, segment ) );
107 projectBuilds.add( createProjectBuild( X, session, segment ) );
108 projectBuilds.add( createProjectBuild( Y, session, segment ) );
109 projectBuilds.add( createProjectBuild( Z, session, segment ) );
110 return projectBuilds;
111 }
112
113 private static ProjectSegment createProjectBuild( MavenProject project, MavenSession session,
114 TaskSegment taskSegment )
115 throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
116 NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
117 LifecyclePhaseNotFoundException, LifecycleNotFoundException
118 {
119 final MavenSession session1 = session.clone();
120 return new ProjectSegment( project, taskSegment, session1 );
121 }
122
123
124 private static TaskSegment createTaskSegment()
125 {
126 TaskSegment result = new TaskSegment( false );
127 result.getTasks().add( new GoalTask( "t1" ) );
128 result.getTasks().add( new GoalTask( "t2" ) );
129 return result;
130 }
131
132 class Dependency
133 {
134 MavenProject dependant;
135
136 MavenProject dependency;
137
138 Dependency( MavenProject dependant, MavenProject dependency )
139 {
140 this.dependant = dependant;
141 this.dependency = dependency;
142 }
143
144 void addIfDownstream( MavenProject mavenProject, List<MavenProject> result )
145 {
146 if ( dependency == mavenProject )
147 {
148 result.add( dependant );
149 }
150 }
151
152 void addIfUpstreamOf( MavenProject mavenProject, List<MavenProject> result )
153 {
154 if ( dependant == mavenProject )
155 {
156 result.add( dependency );
157 }
158 }
159 }
160
161 private List<Dependency> getDependencies()
162 {
163 List<Dependency> dependencies = new ArrayList<>();
164 dependencies.add( new Dependency( B, A ) );
165 dependencies.add( new Dependency( C, A ) );
166 dependencies.add( new Dependency( X, B ) );
167 dependencies.add( new Dependency( X, C ) );
168 dependencies.add( new Dependency( Y, B ) );
169 dependencies.add( new Dependency( Z, C ) );
170 return dependencies;
171 }
172
173 public List<MavenProject> getAllProjects()
174 {
175 return Arrays.asList( A, B, C, X, Y, Z, UNKNOWN );
176 }
177
178 public List<MavenProject> getSortedProjects()
179 {
180 return Arrays.asList( A, B, C, X, Y, Z );
181 }
182
183 public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
184 {
185 if ( transitive )
186 {
187 throw new RuntimeException( "Not implemented yet" );
188 }
189 List<MavenProject> result = new ArrayList<>();
190 for ( Dependency dependency : getDependencies() )
191 {
192 dependency.addIfDownstream( project, result );
193 }
194 return result;
195 }
196
197 public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
198 {
199
200
201
202
203 List<MavenProject> result = new ArrayList<>();
204 final List<Dependency> dependencies = getDependencies();
205 for ( Dependency dependency : dependencies )
206 {
207 dependency.addIfUpstreamOf( project, result );
208 }
209 return result;
210 }
211
212 public static MavenSession getMavenSession( MavenProject mavenProject )
213 {
214 final MavenSession session = getMavenSession();
215 session.setCurrentProject( mavenProject );
216 return session;
217 }
218
219 public static MavenSession getMavenSession()
220 {
221 final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
222 MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
223 mavenExecutionRequest.setExecutionListener( new AbstractExecutionListener() );
224 mavenExecutionRequest.setGoals( Arrays.asList( "clean", "aggr", "install" ) );
225 mavenExecutionRequest.setDegreeOfConcurrency( 1 );
226 final MavenSession session = new MavenSession( null, null, mavenExecutionRequest, defaultMavenExecutionResult );
227 final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
228 session.setProjectDependencyGraph( dependencyGraphStub );
229 session.setProjects( dependencyGraphStub.getSortedProjects() );
230 return session;
231 }
232
233 }