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   */
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.
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      * Gets the user properties to use for interpolation and profile activation. The user properties have been
205      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
206      * line.
207      *
208      * @return The user properties, never {@code null}.
209      */
210     public Properties getUserProperties()
211     {
212         return request.getUserProperties();
213     }
214 
215     /**
216      * Gets the system properties to use for interpolation and profile activation. The system properties are collected
217      * from the runtime environment like {@link System#getProperties()} and environment variables.
218      *
219      * @return The system properties, never {@code null}.
220      */
221     public Properties getSystemProperties()
222     {
223         return request.getSystemProperties();
224     }
225 
226     /**
227      * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
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     // Backward compat
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 }