View Javadoc
1   package org.apache.maven.execution;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Jason van Zyl
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       * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before
63       * being passed into the session. This is also the potentially constrained set of projects by using --projects
64       * on the command line.
65       */
66      private List<MavenProject> projects;
67  
68      /**
69       * The full set of projects before any potential constraining by --projects. Useful in the case where you want to
70       * build a smaller set of projects but perform other operations in the context of your reactor.
71       */
72      private List<MavenProject> allProjects;
73  
74      private MavenProject topLevelProject;
75  
76      private ProjectDependencyGraph projectDependencyGraph;
77  
78      private boolean parallel;
79  
80      private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey =
81          new ConcurrentHashMap<String, Map<String, Map<String, Object>>>();
82  
83      @Deprecated
84      public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
85                           MavenProject project )
86      {
87          this( container, request, result, Arrays.asList( new MavenProject[]{project} ) );
88      }
89  
90      @Deprecated
91      public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
92                           EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
93                           String executionRootDir, Properties executionProperties, Date startTime )
94      {
95          this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
96                executionProperties, null, startTime );
97      }
98  
99      @Deprecated
100     public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
101                          EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
102                          String executionRootDir, Properties executionProperties, Properties userProperties,
103                          Date startTime )
104     {
105         this.container = container;
106         this.settings = settings;
107         this.executionProperties = executionProperties;
108         this.request = new DefaultMavenExecutionRequest();
109         this.request.setUserProperties( userProperties );
110         this.request.setLocalRepository( localRepository );
111         this.request.setGoals( goals );
112         this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
113         this.request.setStartTime( startTime );
114     }
115 
116     @Deprecated
117     public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
118                          List<MavenProject> projects )
119     {
120         this.container = container;
121         this.request = request;
122         this.result = result;
123         this.settings = new SettingsAdapter( request );
124         setProjects( projects );
125     }
126 
127     public MavenSession( PlexusContainer container, RepositorySystemSession repositorySession, MavenExecutionRequest request,
128                          MavenExecutionResult result )
129     {
130         this.container = container;
131         this.request = request;
132         this.result = result;
133         this.settings = new SettingsAdapter( request );
134         this.repositorySession = repositorySession;
135     }
136 
137     public void setProjects( List<MavenProject> projects )
138     {
139         if ( !projects.isEmpty() )
140         {
141             this.currentProject = projects.get( 0 );
142             this.topLevelProject = currentProject;
143             for ( MavenProject project : projects )
144             {
145                 if ( project.isExecutionRoot() )
146                 {
147                     topLevelProject = project;
148                     break;
149                 }
150             }
151         }
152         else
153         {
154             this.currentProject = null;
155             this.topLevelProject = null;
156         }
157         this.projects = projects;
158     }
159 
160     @Deprecated
161     public PlexusContainer getContainer()
162     {
163         return container;
164     }
165 
166     @Deprecated
167     public Object lookup( String role )
168         throws ComponentLookupException
169     {
170         return container.lookup( role );
171     }
172 
173     @Deprecated
174     public Object lookup( String role, String roleHint )
175         throws ComponentLookupException
176     {
177         return container.lookup( role, roleHint );
178     }
179 
180     @Deprecated
181     public List<Object> lookupList( String role )
182         throws ComponentLookupException
183     {
184         return container.lookupList( role );
185     }
186 
187     @Deprecated
188     public Map<String, Object> lookupMap( String role )
189         throws ComponentLookupException
190     {
191         return container.lookupMap( role );
192     }
193 
194     public ArtifactRepository getLocalRepository()
195     {
196         return request.getLocalRepository();
197     }
198 
199     public List<String> getGoals()
200     {
201         return request.getGoals();
202     }
203 
204     /**
205      * Gets the user properties to use for interpolation and profile activation. The user properties have been
206      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
207      * line.
208      *
209      * @return The user properties, never {@code null}.
210      */
211     public Properties getUserProperties()
212     {
213         return request.getUserProperties();
214     }
215 
216     /**
217      * Gets the system properties to use for interpolation and profile activation. The system properties are collected
218      * from the runtime environment like {@link System#getProperties()} and environment variables.
219      *
220      * @return The system properties, never {@code null}.
221      */
222     public Properties getSystemProperties()
223     {
224         return request.getSystemProperties();
225     }
226 
227     /**
228      * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
229      */
230     @Deprecated
231     public Properties getExecutionProperties()
232     {
233         if ( executionProperties == null )
234         {
235             executionProperties = new Properties();
236             executionProperties.putAll( request.getSystemProperties() );
237             executionProperties.putAll( request.getUserProperties() );
238         }
239 
240         return executionProperties;
241     }
242 
243     public Settings getSettings()
244     {
245         return settings;
246     }
247 
248     public List<MavenProject> getProjects()
249     {
250         return projects;
251     }
252 
253     @Deprecated
254     public List<MavenProject> getSortedProjects()
255     {
256         return getProjects();
257     }
258 
259     public String getExecutionRootDirectory()
260     {
261         return request.getBaseDirectory();
262     }
263 
264     @Deprecated
265     public boolean isUsingPOMsFromFilesystem()
266     {
267         return request.isProjectPresent();
268     }
269 
270     public MavenExecutionRequest getRequest()
271     {
272         return request;
273     }
274 
275     public void setCurrentProject( MavenProject currentProject )
276     {
277         this.currentProject = currentProject;
278     }
279 
280     public MavenProject getCurrentProject()
281     {
282         return currentProject;
283     }
284 
285     public ProjectBuildingRequest getProjectBuildingRequest()
286     {
287         return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
288     }
289 
290     public List<String> getPluginGroups()
291     {
292         return request.getPluginGroups();
293     }
294 
295     public boolean isOffline()
296     {
297         return request.isOffline();
298     }
299 
300     public MavenProject getTopLevelProject()
301     {
302         return topLevelProject;
303     }
304 
305     public MavenExecutionResult getResult()
306     {
307         return result;
308     }
309 
310     // Backward compat
311 
312     public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
313     {
314         String projectKey = project.getId();
315 
316         Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
317 
318         if ( pluginContextsByKey == null )
319         {
320             pluginContextsByKey = new ConcurrentHashMap<String, Map<String, Object>>();
321 
322             pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
323         }
324 
325         String pluginKey = plugin.getPluginLookupKey();
326 
327         Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
328 
329         if ( pluginContext == null )
330         {
331             pluginContext = new ConcurrentHashMap<String, Object>();
332 
333             pluginContextsByKey.put( pluginKey, pluginContext );
334         }
335 
336         return pluginContext;
337     }
338 
339     public ProjectDependencyGraph getProjectDependencyGraph()
340     {
341         return projectDependencyGraph;
342     }
343 
344     public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
345     {
346         this.projectDependencyGraph = projectDependencyGraph;
347     }
348 
349     public String getReactorFailureBehavior()
350     {
351         return request.getReactorFailureBehavior();
352     }
353 
354     @Override
355     public MavenSession clone()
356     {
357         try
358         {
359             return (MavenSession) super.clone();
360         }
361         catch ( CloneNotSupportedException e )
362         {
363             throw new RuntimeException( "Bug", e );
364         }
365     }
366 
367     @Deprecated
368     public EventDispatcher getEventDispatcher()
369     {
370         return null;
371     }
372 
373     public Date getStartTime()
374     {
375         return request.getStartTime();
376     }
377 
378     public boolean isParallel()
379     {
380         return parallel;
381     }
382 
383     public void setParallel( boolean parallel )
384     {
385         this.parallel = parallel;
386     }
387 
388     public RepositorySystemSession getRepositorySession()
389     {
390         return repositorySession;
391     }
392 
393     @Deprecated
394     //
395     // Used by Tycho and will break users and force them to upgrade to Maven 3.1 so we should really leave
396     // this here, possibly indefinitely.
397     //
398     public RepositoryCache getRepositoryCache()
399     {
400         return null;
401     }
402 
403     private Map<String, MavenProject> projectMap;
404     
405     public void setProjectMap( Map<String, MavenProject> projectMap )
406     {
407         this.projectMap = projectMap;
408     }
409     
410     public Map<String, MavenProject> getProjectMap() 
411     {
412         return projectMap;
413     }
414 
415     /** This is a provisional method and may be removed */
416     public List<MavenProject> getAllProjects()
417     {
418         return allProjects;
419     }
420 
421     /** This is a provisional method and may be removed */
422     public void setAllProjects( List<MavenProject> allProjects )
423     {
424         this.allProjects = allProjects;
425     }
426     
427     
428 }