1 package org.apache.maven.execution;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.RepositoryCache;
32 import org.apache.maven.monitor.event.EventDispatcher;
33 import org.apache.maven.plugin.descriptor.PluginDescriptor;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.project.ProjectBuildingRequest;
36 import org.apache.maven.settings.Settings;
37 import org.codehaus.plexus.PlexusContainer;
38 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
39 import org.eclipse.aether.RepositorySystemSession;
40
41
42
43
44
45
46 public class MavenSession
47 implements Cloneable
48 {
49 private MavenExecutionRequest request;
50
51 private MavenExecutionResult result;
52
53 private RepositorySystemSession repositorySession;
54
55 private Properties executionProperties;
56
57 private MavenProject currentProject;
58
59
60
61
62
63
64 private List<MavenProject> projects;
65
66
67
68
69
70 private List<MavenProject> allProjects;
71
72 private MavenProject topLevelProject;
73
74 private ProjectDependencyGraph projectDependencyGraph;
75
76 private boolean parallel;
77
78 private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey =
79 new ConcurrentHashMap<>();
80
81
82 public void setProjects( List<MavenProject> projects )
83 {
84 if ( !projects.isEmpty() )
85 {
86 this.currentProject = projects.get( 0 );
87 this.topLevelProject = currentProject;
88 for ( MavenProject project : projects )
89 {
90 if ( project.isExecutionRoot() )
91 {
92 topLevelProject = project;
93 break;
94 }
95 }
96 }
97 else
98 {
99 this.currentProject = null;
100 this.topLevelProject = null;
101 }
102 this.projects = projects;
103 }
104
105 public ArtifactRepository getLocalRepository()
106 {
107 return request.getLocalRepository();
108 }
109
110 public List<String> getGoals()
111 {
112 return request.getGoals();
113 }
114
115
116
117
118
119
120
121
122 public Properties getUserProperties()
123 {
124 return request.getUserProperties();
125 }
126
127
128
129
130
131
132
133 public Properties getSystemProperties()
134 {
135 return request.getSystemProperties();
136 }
137
138 public Settings getSettings()
139 {
140 return settings;
141 }
142
143 public List<MavenProject> getProjects()
144 {
145 return projects;
146 }
147
148 public String getExecutionRootDirectory()
149 {
150 return request.getBaseDirectory();
151 }
152
153 public MavenExecutionRequest getRequest()
154 {
155 return request;
156 }
157
158 public void setCurrentProject( MavenProject currentProject )
159 {
160 this.currentProject = currentProject;
161 }
162
163 public MavenProject getCurrentProject()
164 {
165 return currentProject;
166 }
167
168 public ProjectBuildingRequest getProjectBuildingRequest()
169 {
170 return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
171 }
172
173 public List<String> getPluginGroups()
174 {
175 return request.getPluginGroups();
176 }
177
178 public boolean isOffline()
179 {
180 return request.isOffline();
181 }
182
183 public MavenProject getTopLevelProject()
184 {
185 return topLevelProject;
186 }
187
188 public MavenExecutionResult getResult()
189 {
190 return result;
191 }
192
193
194
195 public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
196 {
197 String projectKey = project.getId();
198
199 Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
200
201 if ( pluginContextsByKey == null )
202 {
203 pluginContextsByKey = new ConcurrentHashMap<>();
204
205 pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
206 }
207
208 String pluginKey = plugin.getPluginLookupKey();
209
210 Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
211
212 if ( pluginContext == null )
213 {
214 pluginContext = new ConcurrentHashMap<>();
215
216 pluginContextsByKey.put( pluginKey, pluginContext );
217 }
218
219 return pluginContext;
220 }
221
222 public ProjectDependencyGraph getProjectDependencyGraph()
223 {
224 return projectDependencyGraph;
225 }
226
227 public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
228 {
229 this.projectDependencyGraph = projectDependencyGraph;
230 }
231
232 public String getReactorFailureBehavior()
233 {
234 return request.getReactorFailureBehavior();
235 }
236
237 @Override
238 public MavenSession clone()
239 {
240 try
241 {
242 return (MavenSession) super.clone();
243 }
244 catch ( CloneNotSupportedException e )
245 {
246 throw new RuntimeException( "Bug", e );
247 }
248 }
249
250 public Date getStartTime()
251 {
252 return request.getStartTime();
253 }
254
255 public boolean isParallel()
256 {
257 return parallel;
258 }
259
260 public void setParallel( boolean parallel )
261 {
262 this.parallel = parallel;
263 }
264
265 public RepositorySystemSession getRepositorySession()
266 {
267 return repositorySession;
268 }
269
270 private Map<String, MavenProject> projectMap;
271
272 public void setProjectMap( Map<String, MavenProject> projectMap )
273 {
274 this.projectMap = projectMap;
275 }
276
277
278 public List<MavenProject> getAllProjects()
279 {
280 return allProjects;
281 }
282
283
284 public void setAllProjects( List<MavenProject> allProjects )
285 {
286 this.allProjects = allProjects;
287 }
288
289
290
291
292
293
294
295 private PlexusContainer container;
296
297 private final Settings settings;
298
299 @Deprecated
300
301 public Map<String, MavenProject> getProjectMap()
302 {
303 return projectMap;
304 }
305
306 @Deprecated
307 public MavenSession( PlexusContainer container, RepositorySystemSession repositorySession,
308 MavenExecutionRequest request, MavenExecutionResult result )
309 {
310 this.container = container;
311 this.request = request;
312 this.result = result;
313 this.settings = new SettingsAdapter( request );
314 this.repositorySession = repositorySession;
315 }
316
317 @Deprecated
318 public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
319 MavenProject project )
320 {
321 this( container, request, result, Arrays.asList( new MavenProject[]{project} ) );
322 }
323
324 @Deprecated
325 public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
326 EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
327 String executionRootDir, Properties executionProperties, Date startTime )
328 {
329 this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
330 executionProperties, null, startTime );
331 }
332
333 @Deprecated
334 public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
335 EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
336 String executionRootDir, Properties executionProperties, Properties userProperties,
337 Date startTime )
338 {
339 this.container = container;
340 this.settings = settings;
341 this.executionProperties = executionProperties;
342 this.request = new DefaultMavenExecutionRequest();
343 this.request.setUserProperties( userProperties );
344 this.request.setLocalRepository( localRepository );
345 this.request.setGoals( goals );
346 this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
347 this.request.setStartTime( startTime );
348 }
349
350 @Deprecated
351 public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
352 List<MavenProject> projects )
353 {
354 this.container = container;
355 this.request = request;
356 this.result = result;
357 this.settings = new SettingsAdapter( request );
358 setProjects( projects );
359 }
360
361 @Deprecated
362 public List<MavenProject> getSortedProjects()
363 {
364 return getProjects();
365 }
366
367 @Deprecated
368
369
370
371
372 public RepositoryCache getRepositoryCache()
373 {
374 return null;
375 }
376
377 @Deprecated
378 public EventDispatcher getEventDispatcher()
379 {
380 return null;
381 }
382
383 @Deprecated
384 public boolean isUsingPOMsFromFilesystem()
385 {
386 return request.isProjectPresent();
387 }
388
389
390
391
392 @Deprecated
393 public Properties getExecutionProperties()
394 {
395 if ( executionProperties == null )
396 {
397 executionProperties = new Properties();
398 executionProperties.putAll( request.getSystemProperties() );
399 executionProperties.putAll( request.getUserProperties() );
400 }
401
402 return executionProperties;
403 }
404
405 @Deprecated
406 public PlexusContainer getContainer()
407 {
408 return container;
409 }
410
411 @Deprecated
412 public Object lookup( String role )
413 throws ComponentLookupException
414 {
415 return container.lookup( role );
416 }
417
418 @Deprecated
419 public Object lookup( String role, String roleHint )
420 throws ComponentLookupException
421 {
422 return container.lookup( role, roleHint );
423 }
424
425 @Deprecated
426 public List<Object> lookupList( String role )
427 throws ComponentLookupException
428 {
429 return container.lookupList( role );
430 }
431
432 @Deprecated
433 public Map<String, Object> lookupMap( String role )
434 throws ComponentLookupException
435 {
436 return container.lookupMap( role );
437 }
438
439
440 }