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