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.sonatype.aether.RepositorySystemSession;
40  
41  /**
42   * @author Jason van Zyl
43   * @version $Id: MavenSession.java 988749 2010-08-24 22:46:07Z bentmann $
44   */
45  public class MavenSession
46      implements Cloneable
47  {
48      private PlexusContainer container;
49  
50      private MavenExecutionRequest request;
51  
52      private MavenExecutionResult result;
53  
54      private RepositorySystemSession repositorySession;
55  
56      private final Settings settings;
57  
58      private Properties executionProperties;
59  
60      private MavenProject currentProject;
61  
62      /**
63       * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before
64       * being passed into the session.
65       */
66      private List<MavenProject> projects;
67  
68      private MavenProject topLevelProject;
69  
70      private ProjectDependencyGraph projectDependencyGraph;
71  
72      private boolean parallel;
73  
74      private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey =
75          new ConcurrentHashMap<String, Map<String, Map<String, Object>>>();
76  
77      @Deprecated
78      public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
79                           MavenProject project )
80      {
81          this( container, request, result, Arrays.asList( new MavenProject[]{project} ) );
82      }
83  
84      @Deprecated
85      public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
86                           EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
87                           String executionRootDir, Properties executionProperties, Date startTime )
88      {
89          this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
90                executionProperties, null, startTime );
91      }
92  
93      @Deprecated
94      public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
95                           EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
96                           String executionRootDir, Properties executionProperties, Properties userProperties,
97                           Date startTime )
98      {
99          this.container = container;
100         this.settings = settings;
101         this.executionProperties = executionProperties;
102         this.request = new DefaultMavenExecutionRequest();
103         this.request.setUserProperties( userProperties );
104         this.request.setLocalRepository( localRepository );
105         this.request.setGoals( goals );
106         this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
107         this.request.setStartTime( startTime );
108     }
109 
110     @Deprecated
111     public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
112                          List<MavenProject> projects )
113     {
114         this.container = container;
115         this.request = request;
116         this.result = result;
117         this.settings = new SettingsAdapter( request );
118         setProjects( projects );
119     }
120 
121     public MavenSession( PlexusContainer container, RepositorySystemSession repositorySession, MavenExecutionRequest request,
122                          MavenExecutionResult result )
123     {
124         this.container = container;
125         this.request = request;
126         this.result = result;
127         this.settings = new SettingsAdapter( request );
128         this.repositorySession = repositorySession;
129     }
130 
131     public void setProjects( List<MavenProject> projects )
132     {
133         if ( !projects.isEmpty() )
134         {
135             this.currentProject = projects.get( 0 );
136             this.topLevelProject = currentProject;
137             for ( MavenProject project : projects )
138             {
139                 if ( project.isExecutionRoot() )
140                 {
141                     topLevelProject = project;
142                     break;
143                 }
144             }
145         }
146         else
147         {
148             this.currentProject = null;
149             this.topLevelProject = null;
150         }
151         this.projects = projects;
152     }
153 
154     @Deprecated
155     public PlexusContainer getContainer()
156     {
157         return container;
158     }
159 
160     @Deprecated
161     public Object lookup( String role )
162         throws ComponentLookupException
163     {
164         return container.lookup( role );
165     }
166 
167     @Deprecated
168     public Object lookup( String role, String roleHint )
169         throws ComponentLookupException
170     {
171         return container.lookup( role, roleHint );
172     }
173 
174     @Deprecated
175     public List<Object> lookupList( String role )
176         throws ComponentLookupException
177     {
178         return container.lookupList( role );
179     }
180 
181     @Deprecated
182     public Map<String, Object> lookupMap( String role )
183         throws ComponentLookupException
184     {
185         return container.lookupMap( role );
186     }
187 
188     @Deprecated
189     public RepositoryCache getRepositoryCache()
190     {
191         return null;
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     public boolean isUsingPOMsFromFilesystem()
265     {
266         return request.isProjectPresent();
267     }
268 
269     public MavenExecutionRequest getRequest()
270     {
271         return request;
272     }
273 
274     public void setCurrentProject( MavenProject currentProject )
275     {
276         this.currentProject = currentProject;
277     }
278 
279     public MavenProject getCurrentProject()
280     {
281         return currentProject;
282     }
283 
284     public ProjectBuildingRequest getProjectBuildingRequest()
285     {
286         return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
287     }
288 
289     public List<String> getPluginGroups()
290     {
291         return request.getPluginGroups();
292     }
293 
294     public boolean isOffline()
295     {
296         return request.isOffline();
297     }
298 
299     public MavenProject getTopLevelProject()
300     {
301         return topLevelProject;
302     }
303 
304     public MavenExecutionResult getResult()
305     {
306         return result;
307     }
308 
309     // Backward compat
310 
311     public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
312     {
313         String projectKey = project.getId();
314 
315         Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
316 
317         if ( pluginContextsByKey == null )
318         {
319             pluginContextsByKey = new ConcurrentHashMap<String, Map<String, Object>>();
320 
321             pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
322         }
323 
324         String pluginKey = plugin.getPluginLookupKey();
325 
326         Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
327 
328         if ( pluginContext == null )
329         {
330             pluginContext = new ConcurrentHashMap<String, Object>();
331 
332             pluginContextsByKey.put( pluginKey, pluginContext );
333         }
334 
335         return pluginContext;
336     }
337 
338     public ProjectDependencyGraph getProjectDependencyGraph()
339     {
340         return projectDependencyGraph;
341     }
342 
343     public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
344     {
345         this.projectDependencyGraph = projectDependencyGraph;
346     }
347 
348     public String getReactorFailureBehavior()
349     {
350         return request.getReactorFailureBehavior();
351     }
352 
353     @Override
354     public MavenSession clone()
355     {
356         try
357         {
358             return (MavenSession) super.clone();
359         }
360         catch ( CloneNotSupportedException e )
361         {
362             throw new RuntimeException( "Bug", e );
363         }
364     }
365 
366     private String getId( MavenProject project )
367     {
368         return project.getGroupId() + ':' + project.getArtifactId() + ':' + project.getVersion();
369     }
370 
371     @Deprecated
372     public EventDispatcher getEventDispatcher()
373     {
374         return null;
375     }
376 
377     public Date getStartTime()
378     {
379         return request.getStartTime();
380     }
381 
382     public boolean isParallel()
383     {
384         return parallel;
385     }
386 
387     public void setParallel( boolean parallel )
388     {
389         this.parallel = parallel;
390     }
391 
392     public RepositorySystemSession getRepositorySession()
393     {
394         return repositorySession;
395     }
396 
397 }