001package org.apache.maven.execution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Arrays;
024import java.util.Date;
025import java.util.List;
026import java.util.Map;
027import java.util.Properties;
028import java.util.concurrent.ConcurrentHashMap;
029
030import org.apache.maven.artifact.repository.ArtifactRepository;
031import org.apache.maven.artifact.repository.RepositoryCache;
032import org.apache.maven.monitor.event.EventDispatcher;
033import org.apache.maven.plugin.descriptor.PluginDescriptor;
034import org.apache.maven.project.MavenProject;
035import org.apache.maven.project.ProjectBuildingRequest;
036import org.apache.maven.settings.Settings;
037import org.codehaus.plexus.PlexusContainer;
038import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
039import org.eclipse.aether.RepositorySystemSession;
040
041/**
042 * @author Jason van Zyl
043 */
044public class MavenSession
045    implements Cloneable
046{
047    private PlexusContainer container;
048
049    private MavenExecutionRequest request;
050
051    private MavenExecutionResult result;
052
053    private RepositorySystemSession repositorySession;
054
055    private final Settings settings;
056
057    private Properties executionProperties;
058
059    private MavenProject currentProject;
060
061    /**
062     * These projects have already been topologically sorted in the {@link org.apache.maven.Maven} component before
063     * being passed into the session. This is also the potentially constrained set of projects by using --projects
064     * on the command line.
065     */
066    private List<MavenProject> projects;
067
068    /**
069     * The full set of projects before any potential constraining by --projects. Useful in the case where you want to
070     * build a smaller set of projects but perform other operations in the context of your reactor.
071     */
072    private List<MavenProject> allProjects;
073
074    private MavenProject topLevelProject;
075
076    private ProjectDependencyGraph projectDependencyGraph;
077
078    private boolean parallel;
079
080    private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey =
081        new ConcurrentHashMap<String, Map<String, Map<String, Object>>>();
082
083    @Deprecated
084    public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
085                         MavenProject project )
086    {
087        this( container, request, result, Arrays.asList( new MavenProject[]{project} ) );
088    }
089
090    @Deprecated
091    public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
092                         EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
093                         String executionRootDir, Properties executionProperties, Date startTime )
094    {
095        this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir,
096              executionProperties, null, startTime );
097    }
098
099    @Deprecated
100    public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository,
101                         EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals,
102                         String executionRootDir, Properties executionProperties, Properties userProperties,
103                         Date startTime )
104    {
105        this.container = container;
106        this.settings = settings;
107        this.executionProperties = executionProperties;
108        this.request = new DefaultMavenExecutionRequest();
109        this.request.setUserProperties( userProperties );
110        this.request.setLocalRepository( localRepository );
111        this.request.setGoals( goals );
112        this.request.setBaseDirectory( ( executionRootDir != null ) ? new File( executionRootDir ) : null );
113        this.request.setStartTime( startTime );
114    }
115
116    @Deprecated
117    public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result,
118                         List<MavenProject> projects )
119    {
120        this.container = container;
121        this.request = request;
122        this.result = result;
123        this.settings = new SettingsAdapter( request );
124        setProjects( projects );
125    }
126
127    public MavenSession( PlexusContainer container, RepositorySystemSession repositorySession, MavenExecutionRequest request,
128                         MavenExecutionResult result )
129    {
130        this.container = container;
131        this.request = request;
132        this.result = result;
133        this.settings = new SettingsAdapter( request );
134        this.repositorySession = repositorySession;
135    }
136
137    public void setProjects( List<MavenProject> projects )
138    {
139        if ( !projects.isEmpty() )
140        {
141            this.currentProject = projects.get( 0 );
142            this.topLevelProject = currentProject;
143            for ( MavenProject project : projects )
144            {
145                if ( project.isExecutionRoot() )
146                {
147                    topLevelProject = project;
148                    break;
149                }
150            }
151        }
152        else
153        {
154            this.currentProject = null;
155            this.topLevelProject = null;
156        }
157        this.projects = projects;
158    }
159
160    @Deprecated
161    public PlexusContainer getContainer()
162    {
163        return container;
164    }
165
166    @Deprecated
167    public Object lookup( String role )
168        throws ComponentLookupException
169    {
170        return container.lookup( role );
171    }
172
173    @Deprecated
174    public Object lookup( String role, String roleHint )
175        throws ComponentLookupException
176    {
177        return container.lookup( role, roleHint );
178    }
179
180    @Deprecated
181    public List<Object> lookupList( String role )
182        throws ComponentLookupException
183    {
184        return container.lookupList( role );
185    }
186
187    @Deprecated
188    public Map<String, Object> lookupMap( String role )
189        throws ComponentLookupException
190    {
191        return container.lookupMap( role );
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    @Deprecated
265    public boolean isUsingPOMsFromFilesystem()
266    {
267        return request.isProjectPresent();
268    }
269
270    public MavenExecutionRequest getRequest()
271    {
272        return request;
273    }
274
275    public void setCurrentProject( MavenProject currentProject )
276    {
277        this.currentProject = currentProject;
278    }
279
280    public MavenProject getCurrentProject()
281    {
282        return currentProject;
283    }
284
285    public ProjectBuildingRequest getProjectBuildingRequest()
286    {
287        return request.getProjectBuildingRequest().setRepositorySession( getRepositorySession() );
288    }
289
290    public List<String> getPluginGroups()
291    {
292        return request.getPluginGroups();
293    }
294
295    public boolean isOffline()
296    {
297        return request.isOffline();
298    }
299
300    public MavenProject getTopLevelProject()
301    {
302        return topLevelProject;
303    }
304
305    public MavenExecutionResult getResult()
306    {
307        return result;
308    }
309
310    // Backward compat
311
312    public Map<String, Object> getPluginContext( PluginDescriptor plugin, MavenProject project )
313    {
314        String projectKey = project.getId();
315
316        Map<String, Map<String, Object>> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( projectKey );
317
318        if ( pluginContextsByKey == null )
319        {
320            pluginContextsByKey = new ConcurrentHashMap<String, Map<String, Object>>();
321
322            pluginContextsByProjectAndPluginKey.put( projectKey, pluginContextsByKey );
323        }
324
325        String pluginKey = plugin.getPluginLookupKey();
326
327        Map<String, Object> pluginContext = pluginContextsByKey.get( pluginKey );
328
329        if ( pluginContext == null )
330        {
331            pluginContext = new ConcurrentHashMap<String, Object>();
332
333            pluginContextsByKey.put( pluginKey, pluginContext );
334        }
335
336        return pluginContext;
337    }
338
339    public ProjectDependencyGraph getProjectDependencyGraph()
340    {
341        return projectDependencyGraph;
342    }
343
344    public void setProjectDependencyGraph( ProjectDependencyGraph projectDependencyGraph )
345    {
346        this.projectDependencyGraph = projectDependencyGraph;
347    }
348
349    public String getReactorFailureBehavior()
350    {
351        return request.getReactorFailureBehavior();
352    }
353
354    @Override
355    public MavenSession clone()
356    {
357        try
358        {
359            return (MavenSession) super.clone();
360        }
361        catch ( CloneNotSupportedException e )
362        {
363            throw new RuntimeException( "Bug", e );
364        }
365    }
366
367    @Deprecated
368    public EventDispatcher getEventDispatcher()
369    {
370        return null;
371    }
372
373    public Date getStartTime()
374    {
375        return request.getStartTime();
376    }
377
378    public boolean isParallel()
379    {
380        return parallel;
381    }
382
383    public void setParallel( boolean parallel )
384    {
385        this.parallel = parallel;
386    }
387
388    public RepositorySystemSession getRepositorySession()
389    {
390        return repositorySession;
391    }
392
393    @Deprecated
394    //
395    // Used by Tycho and will break users and force them to upgrade to Maven 3.1 so we should really leave
396    // this here, possibly indefinitely.
397    //
398    public RepositoryCache getRepositoryCache()
399    {
400        return null;
401    }
402
403    private Map<String, MavenProject> projectMap;
404
405    public void setProjectMap( Map<String, MavenProject> projectMap )
406    {
407        this.projectMap = projectMap;
408    }
409
410    public Map<String, MavenProject> getProjectMap()
411    {
412        return projectMap;
413    }
414
415    /** This is a provisional method and may be removed */
416    public List<MavenProject> getAllProjects()
417    {
418        return allProjects;
419    }
420
421    /** This is a provisional method and may be removed */
422    public void setAllProjects( List<MavenProject> allProjects )
423    {
424        this.allProjects = allProjects;
425    }
426
427
428}