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
89 return request;
90 }
91
92 private ProjectBuildingRequest injectSession( ProjectBuildingRequest request )
93 {
94 MavenSession session = legacySupport.getSession();
95 if ( session != null )
96 {
97 request.setRepositorySession( session.getRepositorySession() );
98 request.setSystemProperties( session.getSystemProperties() );
99 if ( request.getUserProperties().isEmpty() )
100 {
101 request.setUserProperties( session.getUserProperties() );
102 }
103
104 MavenExecutionRequest req = session.getRequest();
105 if ( req != null )
106 {
107 request.setRemoteRepositories( req.getRemoteRepositories() );
108 }
109 }
110 else
111 {
112 Properties props = new Properties();
113 EnvironmentUtils.addEnvVars( props );
114 props.putAll( System.getProperties() );
115 request.setSystemProperties( props );
116 }
117
118 return request;
119 }
120
121 @SuppressWarnings( "unchecked" )
122 private List<ArtifactRepository> normalizeToArtifactRepositories( List<?> repositories,
123 ProjectBuildingRequest request )
124 throws ProjectBuildingException
125 {
126
127
128
129
130
131 if ( repositories != null )
132 {
133 boolean normalized = false;
134
135 List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>( repositories.size() );
136
137 for ( Object repository : repositories )
138 {
139 if ( repository instanceof Repository )
140 {
141 try
142 {
143 ArtifactRepository repo = repositorySystem.buildArtifactRepository( (Repository) repository );
144 repositorySystem.injectMirror( request.getRepositorySession(), Arrays.asList( repo ) );
145 repositorySystem.injectProxy( request.getRepositorySession(), Arrays.asList( repo ) );
146 repositorySystem.injectAuthentication( request.getRepositorySession(), Arrays.asList( repo ) );
147 repos.add( repo );
148 }
149 catch ( InvalidRepositoryException e )
150 {
151 throw new ProjectBuildingException( "", "Invalid remote repository " + repository, e );
152 }
153 normalized = true;
154 }
155 else
156 {
157 repos.add( (ArtifactRepository) repository );
158 }
159 }
160
161 if ( normalized )
162 {
163 return repos;
164 }
165 }
166
167 return (List<ArtifactRepository>) repositories;
168 }
169
170 private ProjectBuildingException transformError( ProjectBuildingException e )
171 {
172 if ( e.getCause() instanceof ModelBuildingException )
173 {
174 return new InvalidProjectModelException( e.getProjectId(), e.getMessage(), e.getPomFile() );
175 }
176
177 return e;
178 }
179
180 public MavenProject build( File pom, ProjectBuilderConfiguration configuration )
181 throws ProjectBuildingException
182 {
183 ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
184
185 try
186 {
187 return projectBuilder.build( pom, request ).getProject();
188 }
189 catch ( ProjectBuildingException e )
190 {
191 throw transformError( e );
192 }
193 }
194
195
196 public MavenProject build( File pom, ArtifactRepository localRepository, ProfileManager profileManager )
197 throws ProjectBuildingException
198 {
199 ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
200 configuration.setLocalRepository( localRepository );
201 configuration.setGlobalProfileManager( profileManager );
202
203 return build( pom, configuration );
204 }
205
206 public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
207 ProjectBuilderConfiguration configuration, boolean allowStubModel )
208 throws ProjectBuildingException
209 {
210 ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
211 request.setRemoteRepositories( normalizeToArtifactRepositories( remoteRepositories, request ) );
212 request.setProcessPlugins( false );
213 request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
214
215 try
216 {
217 return projectBuilder.build( artifact, allowStubModel, request ).getProject();
218 }
219 catch ( ProjectBuildingException e )
220 {
221 throw transformError( e );
222 }
223 }
224
225 public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
226 ArtifactRepository localRepository, boolean allowStubModel )
227 throws ProjectBuildingException
228 {
229 ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
230 configuration.setLocalRepository( localRepository );
231
232 return buildFromRepository( artifact, remoteRepositories, configuration, allowStubModel );
233 }
234
235 public MavenProject buildFromRepository( Artifact artifact, List<ArtifactRepository> remoteRepositories,
236 ArtifactRepository localRepository )
237 throws ProjectBuildingException
238 {
239 return buildFromRepository( artifact, remoteRepositories, localRepository, true );
240 }
241
242
243
244
245
246 public MavenProject buildStandaloneSuperProject( ProjectBuilderConfiguration configuration )
247 throws ProjectBuildingException
248 {
249 ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
250 request.setProcessPlugins( false );
251 request.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
252
253 ModelSource modelSource = new UrlModelSource( getClass().getResource( "standalone.xml" ) );
254
255 MavenProject project = projectBuilder.build( modelSource, request ).getProject();
256 project.setExecutionRoot( true );
257 return project;
258 }
259
260 public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
261 throws ProjectBuildingException
262 {
263 return buildStandaloneSuperProject( localRepository, null );
264 }
265
266 public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository, ProfileManager profileManager )
267 throws ProjectBuildingException
268 {
269 ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
270 configuration.setLocalRepository( localRepository );
271 configuration.setGlobalProfileManager( profileManager );
272
273 return buildStandaloneSuperProject( configuration );
274 }
275
276 public MavenProject buildWithDependencies( File pom, ArtifactRepository localRepository,
277 ProfileManager profileManager, TransferListener transferListener )
278 throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException
279 {
280 ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration();
281 configuration.setLocalRepository( localRepository );
282 configuration.setGlobalProfileManager( profileManager );
283
284 ProjectBuildingRequest request = injectSession( toRequest( configuration ) );
285
286 request.setResolveDependencies( true );
287
288 try
289 {
290 return projectBuilder.build( pom, request ).getProject();
291 }
292 catch ( ProjectBuildingException e )
293 {
294 throw transformError( e );
295 }
296 }
297
298 public MavenProject buildWithDependencies( File pom, ArtifactRepository localRepository,
299 ProfileManager profileManager )
300 throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException
301 {
302 return buildWithDependencies( pom, localRepository, profileManager, null );
303 }
304
305 }