View Javadoc

1   package org.apache.maven.project;
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.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.InvalidRepositoryException;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33  import org.apache.maven.execution.MavenExecutionRequest;
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.model.Repository;
36  import org.apache.maven.model.building.ModelBuildingException;
37  import org.apache.maven.model.building.ModelBuildingRequest;
38  import org.apache.maven.model.building.ModelSource;
39  import org.apache.maven.model.building.UrlModelSource;
40  import org.apache.maven.plugin.LegacySupport;
41  import org.apache.maven.profiles.ProfileManager;
42  import org.apache.maven.properties.internal.EnvironmentUtils;
43  import org.apache.maven.repository.RepositorySystem;
44  import org.apache.maven.wagon.events.TransferListener;
45  import org.codehaus.plexus.component.annotations.Component;
46  import org.codehaus.plexus.component.annotations.Requirement;
47  
48  /**
49   * @version $Id: DefaultMavenProjectBuilder.java 1036433 2010-11-18 12:25:06Z bentmann $
50   */
51  @Component( role = MavenProjectBuilder.class )
52  @Deprecated
53  public class DefaultMavenProjectBuilder
54      implements MavenProjectBuilder
55  {
56  
57      @Requirement
58      private ProjectBuilder projectBuilder;
59  
60      @Requirement
61      private RepositorySystem repositorySystem;
62  
63      @Requirement
64      private LegacySupport legacySupport;
65  
66      // ----------------------------------------------------------------------
67      // MavenProjectBuilder Implementation
68      // ----------------------------------------------------------------------
69  
70      private ProjectBuildingRequest toRequest( ProjectBuilderConfiguration configuration )
71      {
72          DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
73  
74          request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0 );
75          request.setResolveDependencies( false );
76  
77          request.setLocalRepository( configuration.getLocalRepository() );
78          request.setBuildStartTime( configuration.getBuildStartTime() );
79          request.setUserProperties( configuration.getUserProperties() );
80          request.setSystemProperties( configuration.getExecutionProperties() );
81  
82          ProfileManager profileManager = configuration.getGlobalProfileManager();
83          if ( profileManager != null )
84          {
85              request.setActiveProfileIds( profileManager.getExplicitlyActivatedIds() );
86              request.setInactiveProfileIds( profileManager.getExplicitlyDeactivatedIds() );
87          }
88          else
89          {
90              /*
91               * MNG-4900: Hack to workaround deficiency of legacy API which makes it impossible for plugins to access the
92               * global profile manager which is required to build a POM like a CLI invocation does. Failure to consider
93               * the activated profiles can cause repo declarations to be lost which in turn will result in artifact
94               * resolution failures, in particular when using the enhanced local repo which guards access to local files
95               * based on the configured remote repos.
96               */
97              MavenSession session = legacySupport.getSession();
98              if ( session != null )
99              {
100                 MavenExecutionRequest req = session.getRequest();
101                 if ( req != null )
102                 {
103                     request.setActiveProfileIds( req.getActiveProfiles() );
104                     request.setInactiveProfileIds( req.getInactiveProfiles() );
105                 }
106             }
107         }
108 
109         return request;
110     }
111 
112     private ProjectBuildingRequest injectSession( ProjectBuildingRequest request )
113     {
114         MavenSession session = legacySupport.getSession();
115         if ( session != null )
116         {
117             request.setRepositorySession( session.getRepositorySession() );
118             request.setSystemProperties( session.getSystemProperties() );
119             if ( request.getUserProperties().isEmpty() )
120             {
121                 request.setUserProperties( session.getUserProperties() );
122             }
123 
124             MavenExecutionRequest req = session.getRequest();
125             if ( req != null )
126             {
127                 request.setRemoteRepositories( req.getRemoteRepositories() );
128             }
129         }
130         else
131         {
132             Properties props = new Properties();
133             EnvironmentUtils.addEnvVars( props );
134             props.putAll( System.getProperties() );
135             request.setSystemProperties( props );
136         }
137 
138         return request;
139     }
140 
141     @SuppressWarnings( "unchecked" )
142     private List<ArtifactRepository> normalizeToArtifactRepositories( List<?> repositories,
143                                                                       ProjectBuildingRequest request )
144         throws ProjectBuildingException
145     {
146         /*
147          * This provides backward-compat with 2.x that allowed plugins like the maven-remote-resources-plugin:1.0 to
148          * populate the builder configuration with model repositories instead of artifact repositories.
149          */
150 
151         if ( repositories != null )
152         {
153             boolean normalized = false;
154 
155             List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>( repositories.size() );
156 
157             for ( Object repository : repositories )
158             {
159                 if ( repository instanceof Repository )
160                 {
161                     try
162                     {
163                         ArtifactRepository repo = repositorySystem.buildArtifactRepository( (Repository) repository );
164                         repositorySystem.injectMirror( request.getRepositorySession(), Arrays.asList( repo ) );
165                         repositorySystem.injectProxy( request.getRepositorySession(), Arrays.asList( repo ) );
166                         repositorySystem.injectAuthentication( request.getRepositorySession(), Arrays.asList( repo ) );
167                         repos.add( repo );
168                     }
169                     catch ( InvalidRepositoryException e )
170                     {
171                         throw new ProjectBuildingException( "", "Invalid remote repository " + repository, e );
172                     }
173                     normalized = true;
174                 }
175                 else
176                 {
177                     repos.add( (ArtifactRepository) repository );
178                 }
179             }
180 
181             if ( normalized )
182             {
183                 return repos;
184             }
185         }
186 
187         return (List<ArtifactRepository>) repositories;
188     }
189 
190     private ProjectBuildingException transformError( ProjectBuildingException e )
191     {
192         if ( e.getCause() instanceof ModelBuildingException )
193         {
194             return new InvalidProjectModelException( e.getProjectId(), e.getMessage(), e.getPomFile() );
195         }
196 
197         return e;
198     }
199 
200     public MavenProject build( File pom, ProjectBuilderConfiguration configuration )
201         throws ProjectBuildingException
202     {
203         ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
204 
205         try
206         {
207             return projectBuilder.build( pom, request ).getProject();
208         }
209         catch ( ProjectBuildingException e )
210         {
211             throw transformError( e );
212         }
213     }
214 
215     // This is used by the SITE plugin.
216     public MavenProject build( File pom, ArtifactRepository localRepository, ProfileManager profileManager )
217         throws ProjectBuildingException
218     {
219         ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
220         configuration.setLocalRepository( localRepository );
221         configuration.setGlobalProfileManager( profileManager );
222 
223         return build( pom, configuration );
224     }
225 
226     public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
227                                              ProjectBuilderConfiguration configuration, boolean allowStubModel )
228         throws ProjectBuildingException
229     {
230         ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
231         request.setRemoteRepositories( normalizeToArtifactRepositories( remoteRepositories, request ) );
232         request.setProcessPlugins( false );
233         request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
234 
235         try
236         {
237             return projectBuilder.build( artifact, allowStubModel, request ).getProject();
238         }
239         catch ( ProjectBuildingException e )
240         {
241             throw transformError( e );
242         }
243     }
244 
245     public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
246                                              ArtifactRepository localRepository, boolean allowStubModel )
247         throws ProjectBuildingException
248     {
249         ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
250         configuration.setLocalRepository( localRepository );
251 
252         return buildFromRepository( artifact, remoteRepositories, configuration, allowStubModel );
253     }
254 
255     public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
256                                              ArtifactRepository localRepository )
257         throws ProjectBuildingException
258     {
259         return buildFromRepository( artifact, remoteRepositories, localRepository, true );
260     }
261 
262     /**
263      * This is used for pom-less execution like running archetype:generate. I am taking out the profile handling and the
264      * interpolation of the base directory until we spec this out properly.
265      */
266     public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration )
267         throws ProjectBuildingException
268     {
269         ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
270         request.setProcessPlugins( false );
271         request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
272 
273         ModelSource modelSource = new UrlModelSource( getClass().getResource( "standalone.xml" ) );
274 
275         MavenProject project = projectBuilder.build( modelSource, request ).getProject();
276         project.setExecutionRoot( true );
277         return project;
278     }
279 
280     public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
281         throws ProjectBuildingException
282     {
283         return buildStandaloneSuperProject( localRepository, null );
284     }
285 
286     public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
287         throws ProjectBuildingException
288     {
289         ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
290         configuration.setLocalRepository( localRepository );
291         configuration.setGlobalProfileManager( profileManager );
292 
293         return buildStandaloneSuperProject( configuration );
294     }
295 
296     public MavenProject buildWithDependencies( File pom, ArtifactRepository localRepository,
297                                                ProfileManager profileManager, TransferListener transferListener )
298         throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException
299     {
300         ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
301         configuration.setLocalRepository( localRepository );
302         configuration.setGlobalProfileManager( profileManager );
303 
304         ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
305 
306         request.setResolveDependencies( true );
307 
308         try
309         {
310             return projectBuilder.build( pom, request ).getProject();
311         }
312         catch ( ProjectBuildingException e )
313         {
314             throw transformError( e );
315         }
316     }
317 
318     public MavenProject buildWithDependencies( File pom, ArtifactRepository localRepository,
319                                                ProfileManager profileManager )
320         throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException
321     {
322         return buildWithDependencies( pom, localRepository, profileManager, null );
323     }
324 
325 }