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. 064 */ 065 private List<MavenProject> projects; 066 067 private MavenProject topLevelProject; 068 069 private ProjectDependencyGraph projectDependencyGraph; 070 071 private boolean parallel; 072 073 private final Map<String, Map<String, Map<String, Object>>> pluginContextsByProjectAndPluginKey = 074 new ConcurrentHashMap<String, Map<String, Map<String, Object>>>(); 075 076 @Deprecated 077 public MavenSession( PlexusContainer container, MavenExecutionRequest request, MavenExecutionResult result, 078 MavenProject project ) 079 { 080 this( container, request, result, Arrays.asList( new MavenProject[]{project} ) ); 081 } 082 083 @Deprecated 084 public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository, 085 EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals, 086 String executionRootDir, Properties executionProperties, Date startTime ) 087 { 088 this( container, settings, localRepository, eventDispatcher, unused, goals, executionRootDir, 089 executionProperties, null, startTime ); 090 } 091 092 @Deprecated 093 public MavenSession( PlexusContainer container, Settings settings, ArtifactRepository localRepository, 094 EventDispatcher eventDispatcher, ReactorManager unused, List<String> goals, 095 String executionRootDir, Properties executionProperties, Properties userProperties, 096 Date startTime ) 097 { 098 this.container = container; 099 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}