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   * A Maven execution session.
43   *
44   * @author Jason van Zyl
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       * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before
61       * being passed into the session. This is also the potentially constrained set of projects by using --projects
62       * on the command line.
63       */
64      private List<MavenProject> projects;
65  
66      /**
67       * The full set of projects before any potential constraining by --projects. Useful in the case where you want to
68       * build a smaller set of projects but perform other operations in the context of your reactor.
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      * Gets the user properties to use for interpolation and profile activation. The user properties have been
117      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
118      * line.
119      *
120      * @return The user properties, never {@code null}.
121      */
122     public Properties getUserProperties()
123     {
124         return request.getUserProperties();
125     }
126 
127     /**
128      * Gets the system properties to use for interpolation and profile activation. The system properties are collected
129      * from the runtime environment like {@link System#getProperties()} and environment variables.
130      *
131      * @return The system properties, never {@code null}.
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     // Backward compat
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     /** This is a provisional method and may be removed */
278     public List<MavenProject> getAllProjects()
279     {
280         return allProjects;
281     }
282 
283     /** This is a provisional method and may be removed */
284     public void setAllProjects( List<MavenProject> allProjects )
285     {
286         this.allProjects = allProjects;
287     }
288     
289     /*if_not[MAVEN4]*/
290 
291     //
292     // Deprecated 
293     //
294         
295     private PlexusContainer container;    
296     
297     private final Settings settings;
298     
299     @Deprecated
300     /** @deprecated This appears to only be used in the ReactorReader and we can do any processing required there */
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     // Used by Tycho and will break users and force them to upgrade to Maven 3.1 so we should really leave
370     // this here, possibly indefinitely.
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      * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
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     /*end[MAVEN4]*/
440 }