1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
92
93
94
95
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
148
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
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
264
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 }