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.
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     public ArtifactRepository getLocalRepository()
188     {
189         return request.getLocalRepository();
190     }
191 
192     public List<String> getGoals()
193     {
194         return request.getGoals();
195     }
196 
197     /**
198      * Gets the user properties to use for interpolation and profile activation. The user properties have been
199      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
200      * line.
201      *
202      * @return The user properties, never {@code null}.
203      */
204     public Properties getUserProperties()
205     {
206         return request.getUserProperties();
207     }
208 
209     /**
210      * Gets the system properties to use for interpolation and profile activation. The system properties are collected
211      * from the runtime environment like {@link System#getProperties()} and environment variables.
212      *
213      * @return The system properties, never {@code null}.
214      */
215     public Properties getSystemProperties()
216     {
217         return request.getSystemProperties();
218     }
219 
220     /**
221      * @deprecated Use either {@link #getUserProperties()} or {@link #getSystemProperties()}.
222      */
223     @Deprecated
224     public Properties getExecutionProperties()
225     {
226         if ( executionProperties == null )
227         {
228             executionProperties = new Properties();
229             executionProperties.putAll( request.getSystemProperties() );
230             executionProperties.putAll( request.getUserProperties() );
231         }
232 
233         return executionProperties;
234     }
235 
236     public Settings getSettings()
237     {
238         return settings;
239     }
240 
241     public List<MavenProject> getProjects()
242     {
243         return projects;
244     }
245 
246     @Deprecated
247     public List<MavenProject> getSortedProjects()
248     {
249         return getProjects();
250     }
251 
252     public String getExecutionRootDirectory()
253     {
254         return request.getBaseDirectory();
255     }
256 
257     public boolean isUsingPOMsFromFilesystem()
258     {
259         return request.isProjectPresent();
260     }
261 
262     public MavenExecutionRequest getRequest()
263     {
264         return request;
265     }
266 
267     public void setCurrentProject( MavenProject currentProject )
268     {
269         this.currentProject = currentProject;
270     }
271 
272     public MavenProject getCurrentProject()
273     {
274         return currentProject;
275     }
276 
277     public ProjectBuildingRequest getProjectBuildingRequest()
278     {
279         return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
280     }
281 
282     public List<String> getPluginGroups()
283     {
284         return request.getPluginGroups();
285     }
286 
287     public boolean isOffline()
288     {
289         return request.isOffline();
290     }
291 
292     public MavenProject getTopLevelProject()
293     {
294         return topLevelProject;
295     }
296 
297     public MavenExecutionResult getResult()
298     {
299         return result;
300     }
301 
302     // Backward compat
303 
304     public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
305     {
306         String projectKey = project.getId();
307 
308         Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
309 
310         if ( pluginContextsByKey == null )
311         {
312             pluginContextsByKey = new ConcurrentHashMap<String, Map<String, Object>>();
313 
314             pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
315         }
316 
317         String pluginKey = plugin.getPluginLookupKey();
318 
319         Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
320 
321         if ( pluginContext == null )
322         {
323             pluginContext = new ConcurrentHashMap<String, Object>();
324 
325             pluginContextsByKey.put( pluginKey, pluginContext );
326         }
327 
328         return pluginContext;
329     }
330 
331     public ProjectDependencyGraph getProjectDependencyGraph()
332     {
333         return projectDependencyGraph;
334     }
335 
336     public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
337     {
338         this.projectDependencyGraph = projectDependencyGraph;
339     }
340 
341     public String getReactorFailureBehavior()
342     {
343         return request.getReactorFailureBehavior();
344     }
345 
346     @Override
347     public MavenSession clone()
348     {
349         try
350         {
351             return (MavenSession) super.clone();
352         }
353         catch ( CloneNotSupportedException e )
354         {
355             throw new RuntimeException( "Bug", e );
356         }
357     }
358 
359     @Deprecated
360     public EventDispatcher getEventDispatcher()
361     {
362         return null;
363     }
364 
365     public Date getStartTime()
366     {
367         return request.getStartTime();
368     }
369 
370     public boolean isParallel()
371     {
372         return parallel;
373     }
374 
375     public void setParallel( boolean parallel )
376     {
377         this.parallel = parallel;
378     }
379 
380     public RepositorySystemSession getRepositorySession()
381     {
382         return repositorySession;
383     }
384 
385     @Deprecated
386     //
387     // Used by Tycho and will break users and force them to upgrade to Maven 3.1 so we should really leave
388     // this here, possibly indefinitely.
389     //
390     public RepositoryCache getRepositoryCache()
391     {
392         return null;
393     }
394 
395 }