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     @SuppressWarnings( "checkstyle:parameternumber" )
326     public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
327                          EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
328                          String executionRootDir, Properties executionProperties, Date startTime )
329     {
330         this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
331               executionProperties, null, startTime );
332     }
333 
334     @Deprecated
335     @SuppressWarnings( "checkstyle:parameternumber" )
336     public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
337                          EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
338                          String executionRootDir, Properties executionProperties, Properties userProperties,
339                          Date startTime )
340     {
341         this.container = container;
342         this.settings = settings;
343         this.executionProperties = executionProperties;
344         this.request = new DefaultMavenExecutionRequest();
345         this.request.setUserProperties( userProperties );
346         this.request.setLocalRepository( localRepository );
347         this.request.setGoals( goals );
348         this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
349         this.request.setStartTime( startTime );
350     }
351 
352     @Deprecated
353     public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
354                          List<MavenProject> projects )
355     {
356         this.container = container;
357         this.request = request;
358         this.result = result;
359         this.settings = new SettingsAdapter( request );
360         setProjects( projects );
361     }
362 
363     @Deprecated
364     public List<MavenProject> getSortedProjects()
365     {
366         return getProjects();
367     }
368     
369     @Deprecated
370     //
371     // Used by Tycho and will break users and force them to upgrade to Maven 3.1 so we should really leave
372     // this here, possibly indefinitely.
373     //
374     public RepositoryCache getRepositoryCache()
375     {
376         return null;
377     }
378 
379     @Deprecated
380     public EventDispatcher getEventDispatcher()
381     {
382         return null;
383     }
384 
385     @Deprecated
386     public boolean isUsingPOMsFromFilesystem()
387     {
388         return request.isProjectPresent();
389     }
390 
391     /**
392      * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
393      */
394     @Deprecated
395     public Properties getExecutionProperties()
396     {
397         if ( executionProperties == null )
398         {
399             executionProperties = new Properties();
400             executionProperties.putAll( request.getSystemProperties() );
401             executionProperties.putAll( request.getUserProperties() );
402         }
403 
404         return executionProperties;
405     }
406     
407     @Deprecated
408     public PlexusContainer getContainer()
409     {
410         return container;
411     }
412 
413     @Deprecated
414     public Object lookup( String role )
415         throws ComponentLookupException
416     {
417         return container.lookup( role );
418     }
419 
420     @Deprecated
421     public Object lookup( String role, String roleHint )
422         throws ComponentLookupException
423     {
424         return container.lookup( role, roleHint );
425     }
426 
427     @Deprecated
428     public List<Object> lookupList( String role )
429         throws ComponentLookupException
430     {
431         return container.lookupList( role );
432     }
433 
434     @Deprecated
435     public Map<String, Object> lookupMap( String role )
436         throws ComponentLookupException
437     {
438         return container.lookupMap( role );
439     }   
440     
441     /*end[MAVEN4]*/
442 }