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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import org.apache.maven.RepositoryUtils;
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.artifact.InvalidRepositoryException;
37 import org.apache.maven.artifact.repository.ArtifactRepository;
38 import org.apache.maven.artifact.repository.LegacyLocalRepositoryManager;
39 import org.apache.maven.bridge.MavenRepositorySystem;
40 import org.apache.maven.model.Build;
41 import org.apache.maven.model.Dependency;
42 import org.apache.maven.model.DependencyManagement;
43 import org.apache.maven.model.DeploymentRepository;
44 import org.apache.maven.model.Extension;
45 import org.apache.maven.model.Model;
46 import org.apache.maven.model.Plugin;
47 import org.apache.maven.model.Profile;
48 import org.apache.maven.model.ReportPlugin;
49 import org.apache.maven.model.building.DefaultModelBuildingRequest;
50 import org.apache.maven.model.building.DefaultModelProblem;
51 import org.apache.maven.model.building.FileModelSource;
52 import org.apache.maven.model.building.ModelBuilder;
53 import org.apache.maven.model.building.ModelBuildingException;
54 import org.apache.maven.model.building.ModelBuildingRequest;
55 import org.apache.maven.model.building.ModelBuildingResult;
56 import org.apache.maven.model.building.ModelProblem;
57 import org.apache.maven.model.building.ModelProcessor;
58 import org.apache.maven.model.building.ModelSource;
59 import org.apache.maven.model.building.StringModelSource;
60 import org.apache.maven.model.resolution.ModelResolver;
61 import org.apache.maven.repository.internal.ArtifactDescriptorUtils;
62 import org.codehaus.plexus.component.annotations.Component;
63 import org.codehaus.plexus.component.annotations.Requirement;
64 import org.codehaus.plexus.logging.Logger;
65 import org.codehaus.plexus.util.Os;
66 import org.codehaus.plexus.util.StringUtils;
67 import org.eclipse.aether.RepositorySystemSession;
68 import org.eclipse.aether.RequestTrace;
69 import org.eclipse.aether.impl.RemoteRepositoryManager;
70 import org.eclipse.aether.repository.LocalRepositoryManager;
71 import org.eclipse.aether.repository.RemoteRepository;
72 import org.eclipse.aether.repository.WorkspaceRepository;
73 import org.eclipse.aether.resolution.ArtifactRequest;
74 import org.eclipse.aether.resolution.ArtifactResult;
75
76
77
78 @Component( role = ProjectBuilder.class )
79 public class DefaultProjectBuilder
80 implements ProjectBuilder
81 {
82
83 @Requirement
84 private Logger logger;
85
86 @Requirement
87 private ModelBuilder modelBuilder;
88
89 @Requirement
90 private ModelProcessor modelProcessor;
91
92 @Requirement
93 private ProjectBuildingHelper projectBuildingHelper;
94
95 @Requirement
96 private MavenRepositorySystem repositorySystem;
97
98 @Requirement
99 private org.eclipse.aether.RepositorySystem repoSystem;
100
101 @Requirement
102 private RemoteRepositoryManager repositoryManager;
103
104 @Requirement
105 private ProjectDependenciesResolver dependencyResolver;
106
107
108
109
110
111 @Override
112 public ProjectBuildingResult build( File pomFile, ProjectBuildingRequest request )
113 throws ProjectBuildingException
114 {
115 return build( pomFile, new FileModelSource( pomFile ), new InternalConfig( request, null, null ) );
116 }
117
118 @Override
119 public ProjectBuildingResult build( ModelSource modelSource, ProjectBuildingRequest request )
120 throws ProjectBuildingException
121 {
122 return build( null, modelSource, new InternalConfig( request, null, null ) );
123 }
124
125 private ProjectBuildingResult build( File pomFile, ModelSource modelSource, InternalConfig config )
126 throws ProjectBuildingException
127 {
128 ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
129
130 try
131 {
132 ProjectBuildingRequest projectBuildingRequest = config.request;
133
134 MavenProject project = projectBuildingRequest.getProject();
135
136 List<ModelProblem> modelProblems = null;
137 Throwable error = null;
138
139 if ( project == null )
140 {
141 ModelBuildingRequest request = getModelBuildingRequest( config );
142
143 project = new MavenProject();
144 project.setFile( pomFile );
145
146 DefaultModelBuildingListener listener =
147 new DefaultModelBuildingListener( project, projectBuildingHelper, projectBuildingRequest );
148 request.setModelBuildingListener( listener );
149
150 request.setPomFile( pomFile );
151 request.setModelSource( modelSource );
152 request.setLocationTracking( true );
153
154 ModelBuildingResult result;
155 try
156 {
157 result = modelBuilder.build( request );
158 }
159 catch ( ModelBuildingException e )
160 {
161 result = e.getResult();
162 if ( result == null || result.getEffectiveModel() == null )
163 {
164 throw new ProjectBuildingException( e.getModelId(), e.getMessage(), pomFile, e );
165 }
166
167 error = e;
168 }
169
170 modelProblems = result.getProblems();
171
172 initProject( project, Collections.<String, MavenProject>emptyMap(), result,
173 new HashMap<File, Boolean>(), projectBuildingRequest );
174 }
175 else if ( projectBuildingRequest.isResolveDependencies() )
176 {
177 projectBuildingHelper.selectProjectRealm( project );
178 }
179
180 DependencyResolutionResult resolutionResult = null;
181
182 if ( projectBuildingRequest.isResolveDependencies() )
183 {
184 resolutionResult = resolveDependencies( project, config.session );
185 }
186
187 ProjectBuildingResult result = new DefaultProjectBuildingResult( project, modelProblems, resolutionResult );
188
189 if ( error != null )
190 {
191 ProjectBuildingException e = new ProjectBuildingException( Arrays.asList( result ) );
192 e.initCause( error );
193 throw e;
194 }
195
196 return result;
197 }
198 finally
199 {
200 Thread.currentThread().setContextClassLoader( oldContextClassLoader );
201 }
202 }
203
204 private DependencyResolutionResult resolveDependencies( MavenProject project, RepositorySystemSession session )
205 {
206 DependencyResolutionResult resolutionResult;
207
208 try
209 {
210 DefaultDependencyResolutionRequest resolution = new DefaultDependencyResolutionRequest( project, session );
211 resolutionResult = dependencyResolver.resolve( resolution );
212 }
213 catch ( DependencyResolutionException e )
214 {
215 resolutionResult = e.getResult();
216 }
217
218 Set<Artifact> artifacts = new LinkedHashSet<>();
219 if ( resolutionResult.getDependencyGraph() != null )
220 {
221 RepositoryUtils.toArtifacts( artifacts, resolutionResult.getDependencyGraph().getChildren(),
222 Collections.singletonList( project.getArtifact().getId() ), null );
223
224
225 LocalRepositoryManager lrm = session.getLocalRepositoryManager();
226 for ( Artifact artifact : artifacts )
227 {
228 if ( !artifact.isResolved() )
229 {
230 String path = lrm.getPathForLocalArtifact( RepositoryUtils.toArtifact( artifact ) );
231 artifact.setFile( new File( lrm.getRepository().getBasedir(), path ) );
232 }
233 }
234 }
235 project.setResolvedArtifacts( artifacts );
236 project.setArtifacts( artifacts );
237
238 return resolutionResult;
239 }
240
241 private List<String> getProfileIds( List<Profile> profiles )
242 {
243 List<String> ids = new ArrayList<>( profiles.size() );
244
245 for ( Profile profile : profiles )
246 {
247 ids.add( profile.getId() );
248 }
249
250 return ids;
251 }
252
253 private ModelBuildingRequest getModelBuildingRequest( InternalConfig config )
254 {
255 ProjectBuildingRequest configuration = config.request;
256
257 ModelBuildingRequest request = new DefaultModelBuildingRequest();
258
259 RequestTrace trace = RequestTrace.newChild( null, configuration ).newChild( request );
260
261 ModelResolver resolver =
262 new ProjectModelResolver( config.session, trace, repoSystem, repositoryManager, config.repositories,
263 configuration.getRepositoryMerging(), config.modelPool );
264
265 request.setValidationLevel( configuration.getValidationLevel() );
266 request.setProcessPlugins( configuration.isProcessPlugins() );
267 request.setProfiles( configuration.getProfiles() );
268 request.setActiveProfileIds( configuration.getActiveProfileIds() );
269 request.setInactiveProfileIds( configuration.getInactiveProfileIds() );
270 request.setSystemProperties( configuration.getSystemProperties() );
271 request.setUserProperties( configuration.getUserProperties() );
272 request.setBuildStartTime( configuration.getBuildStartTime() );
273 request.setModelResolver( resolver );
274 request.setModelCache( config.modelCache );
275
276 return request;
277 }
278
279 @Override
280 public ProjectBuildingResult build( Artifact artifact, ProjectBuildingRequest request )
281 throws ProjectBuildingException
282 {
283 return build( artifact, false, request );
284 }
285
286 @Override
287 public ProjectBuildingResult build( Artifact artifact, boolean allowStubModel, ProjectBuildingRequest request )
288 throws ProjectBuildingException
289 {
290 org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact( artifact );
291 pomArtifact = ArtifactDescriptorUtils.toPomArtifact( pomArtifact );
292
293 InternalConfig config = new InternalConfig( request, null, null );
294
295 boolean localProject;
296
297 try
298 {
299 ArtifactRequest pomRequest = new ArtifactRequest();
300 pomRequest.setArtifact( pomArtifact );
301 pomRequest.setRepositories( config.repositories );
302 ArtifactResult pomResult = repoSystem.resolveArtifact( config.session, pomRequest );
303
304 pomArtifact = pomResult.getArtifact();
305 localProject = pomResult.getRepository() instanceof WorkspaceRepository;
306 }
307 catch ( org.eclipse.aether.resolution.ArtifactResolutionException e )
308 {
309 if ( e.getResults().get( 0 ).isMissing() && allowStubModel )
310 {
311 return build( null, createStubModelSource( artifact ), config );
312 }
313 throw new ProjectBuildingException( artifact.getId(),
314 "Error resolving project artifact: " + e.getMessage(), e );
315 }
316
317 File pomFile = pomArtifact.getFile();
318
319 if ( "pom".equals( artifact.getType() ) )
320 {
321 artifact.selectVersion( pomArtifact.getVersion() );
322 artifact.setFile( pomFile );
323 artifact.setResolved( true );
324 }
325
326 return build( localProject ? pomFile : null, new FileModelSource( pomFile ), config );
327 }
328
329 private ModelSource createStubModelSource( Artifact artifact )
330 {
331 StringBuilder buffer = new StringBuilder( 1024 );
332
333 buffer.append( "<?xml version='1.0'?>" );
334 buffer.append( "<project>" );
335 buffer.append( "<modelVersion>4.0.0</modelVersion>" );
336 buffer.append( "<groupId>" ).append( artifact.getGroupId() ).append( "</groupId>" );
337 buffer.append( "<artifactId>" ).append( artifact.getArtifactId() ).append( "</artifactId>" );
338 buffer.append( "<version>" ).append( artifact.getBaseVersion() ).append( "</version>" );
339 buffer.append( "<packaging>" ).append( artifact.getType() ).append( "</packaging>" );
340 buffer.append( "</project>" );
341
342 return new StringModelSource( buffer, artifact.getId() );
343 }
344
345 @Override
346 public List<ProjectBuildingResult> build( List<File> pomFiles, boolean recursive, ProjectBuildingRequest request )
347 throws ProjectBuildingException
348 {
349 List<ProjectBuildingResult> results = new ArrayList<>();
350
351 List<InterimResult> interimResults = new ArrayList<>();
352
353 ReactorModelPool modelPool = new ReactorModelPool();
354
355 ReactorModelCache modelCache = new ReactorModelCache();
356
357 InternalConfig config = new InternalConfig( request, modelPool, modelCache );
358
359 Map<String, MavenProject> projectIndex = new HashMap<>( 256 );
360
361 boolean noErrors =
362 build( results, interimResults, projectIndex, pomFiles, new LinkedHashSet<File>(), true, recursive,
363 config );
364
365 populateReactorModelPool( modelPool, interimResults );
366
367 ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
368
369 try
370 {
371 noErrors =
372 build( results, new ArrayList<MavenProject>(), projectIndex, interimResults, request,
373 new HashMap<File, Boolean>() ) && noErrors;
374 }
375 finally
376 {
377 Thread.currentThread().setContextClassLoader( oldContextClassLoader );
378 }
379
380 if ( !noErrors )
381 {
382 throw new ProjectBuildingException( results );
383 }
384
385 return results;
386 }
387
388 private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults,
389 Map<String, MavenProject> projectIndex, List<File> pomFiles, Set<File> aggregatorFiles,
390 boolean isRoot, boolean recursive, InternalConfig config )
391 {
392 boolean noErrors = true;
393
394 for ( File pomFile : pomFiles )
395 {
396 aggregatorFiles.add( pomFile );
397
398 if ( !build( results, interimResults, projectIndex, pomFile, aggregatorFiles, isRoot, recursive, config ) )
399 {
400 noErrors = false;
401 }
402
403 aggregatorFiles.remove( pomFile );
404 }
405
406 return noErrors;
407 }
408
409 private boolean build( List<ProjectBuildingResult> results, List<InterimResult> interimResults,
410 Map<String, MavenProject> projectIndex, File pomFile, Set<File> aggregatorFiles,
411 boolean isRoot, boolean recursive, InternalConfig config )
412 {
413 boolean noErrors = true;
414
415 ModelBuildingRequest request = getModelBuildingRequest( config );
416
417 MavenProject project = new MavenProject();
418
419 request.setPomFile( pomFile );
420 request.setTwoPhaseBuilding( true );
421 request.setLocationTracking( true );
422
423 DefaultModelBuildingListener listener =
424 new DefaultModelBuildingListener( project, projectBuildingHelper, config.request );
425 request.setModelBuildingListener( listener );
426
427 try
428 {
429 ModelBuildingResult result = modelBuilder.build( request );
430
431 Model model = result.getEffectiveModel();
432
433 projectIndex.put( result.getModelIds().get( 0 ), project );
434
435 InterimResult interimResult = new InterimResult( pomFile, request, result, listener, isRoot );
436 interimResults.add( interimResult );
437
438 if ( recursive && !model.getModules().isEmpty() )
439 {
440 File basedir = pomFile.getParentFile();
441
442 List<File> moduleFiles = new ArrayList<>();
443
444 for ( String module : model.getModules() )
445 {
446 if ( StringUtils.isEmpty( module ) )
447 {
448 continue;
449 }
450
451 module = module.replace( '\\', File.separatorChar ).replace( '/', File.separatorChar );
452
453 File moduleFile = new File( basedir, module );
454
455 if ( moduleFile.isDirectory() )
456 {
457 moduleFile = modelProcessor.locatePom( moduleFile );
458 }
459
460 if ( !moduleFile.isFile() )
461 {
462 ModelProblem problem =
463 new DefaultModelProblem( "Child module " + moduleFile + " of " + pomFile
464 + " does not exist", ModelProblem.Severity.ERROR, ModelProblem.Version.BASE, model, -1,
465 -1, null );
466 result.getProblems().add( problem );
467
468 noErrors = false;
469
470 continue;
471 }
472
473 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
474 {
475
476 try
477 {
478 moduleFile = moduleFile.getCanonicalFile();
479 }
480 catch ( IOException e )
481 {
482 moduleFile = moduleFile.getAbsoluteFile();
483 }
484 }
485 else
486 {
487 moduleFile = new File( moduleFile.toURI().normalize() );
488 }
489
490 if ( aggregatorFiles.contains( moduleFile ) )
491 {
492 StringBuilder buffer = new StringBuilder( 256 );
493 for ( File aggregatorFile : aggregatorFiles )
494 {
495 buffer.append( aggregatorFile ).append( " -> " );
496 }
497 buffer.append( moduleFile );
498
499 ModelProblem problem =
500 new DefaultModelProblem( "Child module " + moduleFile + " of " + pomFile
501 + " forms aggregation cycle " + buffer, ModelProblem.Severity.ERROR,
502 ModelProblem.Version.BASE, model, -1, -1, null );
503 result.getProblems().add( problem );
504
505 noErrors = false;
506
507 continue;
508 }
509
510 moduleFiles.add( moduleFile );
511 }
512
513 interimResult.modules = new ArrayList<>();
514
515 if ( !build( results, interimResult.modules, projectIndex, moduleFiles, aggregatorFiles, false,
516 recursive, config ) )
517 {
518 noErrors = false;
519 }
520 }
521 }
522 catch ( ModelBuildingException e )
523 {
524 results.add( new DefaultProjectBuildingResult( e.getModelId(), pomFile, e.getProblems() ) );
525
526 noErrors = false;
527 }
528
529 return noErrors;
530 }
531
532 static class InterimResult
533 {
534
535 File pomFile;
536
537 ModelBuildingRequest request;
538
539 ModelBuildingResult result;
540
541 DefaultModelBuildingListener listener;
542
543 boolean root;
544
545 List<InterimResult> modules = Collections.emptyList();
546
547 InterimResult( File pomFile, ModelBuildingRequest request, ModelBuildingResult result,
548 DefaultModelBuildingListener listener, boolean root )
549 {
550 this.pomFile = pomFile;
551 this.request = request;
552 this.result = result;
553 this.listener = listener;
554 this.root = root;
555 }
556
557 }
558
559 private void populateReactorModelPool( ReactorModelPool reactorModelPool, List<InterimResult> interimResults )
560 {
561 for ( InterimResult interimResult : interimResults )
562 {
563 Model model = interimResult.result.getEffectiveModel();
564 reactorModelPool.put( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPomFile() );
565
566 populateReactorModelPool( reactorModelPool, interimResult.modules );
567 }
568 }
569
570 private boolean build( List<ProjectBuildingResult> results, List<MavenProject> projects,
571 Map<String, MavenProject> projectIndex, List<InterimResult> interimResults,
572 ProjectBuildingRequest request, Map<File, Boolean> profilesXmls )
573 {
574 boolean noErrors = true;
575
576 for ( InterimResult interimResult : interimResults )
577 {
578 try
579 {
580 ModelBuildingResult result = modelBuilder.build( interimResult.request, interimResult.result );
581
582 MavenProject project = interimResult.listener.getProject();
583 initProject( project, projectIndex, result, profilesXmls, request );
584
585 List<MavenProject> modules = new ArrayList<>();
586 noErrors =
587 build( results, modules, projectIndex, interimResult.modules, request, profilesXmls ) && noErrors;
588
589 projects.addAll( modules );
590 projects.add( project );
591
592 project.setExecutionRoot( interimResult.root );
593 project.setCollectedProjects( modules );
594
595 results.add( new DefaultProjectBuildingResult( project, result.getProblems(), null ) );
596 }
597 catch ( ModelBuildingException e )
598 {
599 results.add( new DefaultProjectBuildingResult( e.getModelId(), interimResult.pomFile,
600 e.getProblems() ) );
601
602 noErrors = false;
603 }
604 }
605
606 return noErrors;
607 }
608
609 private void initProject( MavenProject project, Map<String, MavenProject> projects, ModelBuildingResult result,
610 Map<File, Boolean> profilesXmls, ProjectBuildingRequest projectBuildingRequest )
611 {
612 Model model = result.getEffectiveModel();
613
614 project.setModel( model );
615 project.setOriginalModel( result.getRawModel() );
616 project.setFile( model.getPomFile() );
617
618 Model parentModel = result.getModelIds().size() > 1 && !result.getModelIds().get( 1 ).isEmpty()
619 ? result.getRawModel( result.getModelIds().get( 1 ) )
620 : null;
621
622 if ( parentModel != null )
623 {
624 final String parentGroupId = inheritedGroupId( result, 1 );
625 final String parentVersion = inheritedVersion( result, 1 );
626
627 project.setParentArtifact( repositorySystem.createProjectArtifact( parentGroupId,
628 parentModel.getArtifactId(),
629 parentVersion ) );
630
631
632 String parentModelId = result.getModelIds().get( 1 );
633 File parentPomFile = result.getRawModel( parentModelId ).getPomFile();
634 MavenProject parent = projects.get( parentModelId );
635 if ( parent == null )
636 {
637
638
639
640
641
642 projectBuildingRequest.setRemoteRepositories( project.getRemoteArtifactRepositories() );
643 if ( parentPomFile != null )
644 {
645 project.setParentFile( parentPomFile );
646 try
647 {
648 parent = build( parentPomFile, projectBuildingRequest ).getProject();
649 }
650 catch ( ProjectBuildingException e )
651 {
652
653 if ( logger.isDebugEnabled() )
654 {
655
656 logger.warn( "Failed to build parent project for " + project.getId(), e );
657 }
658 else
659 {
660
661 logger.warn( "Failed to build parent project for " + project.getId() );
662 }
663 }
664 }
665 else
666 {
667 Artifact parentArtifact = project.getParentArtifact();
668 try
669 {
670 parent = build( parentArtifact, projectBuildingRequest ).getProject();
671 }
672 catch ( ProjectBuildingException e )
673 {
674
675 if ( logger.isDebugEnabled() )
676 {
677
678 logger.warn( "Failed to build parent project for " + project.getId(), e );
679 }
680 else
681 {
682
683 logger.warn( "Failed to build parent project for " + project.getId() );
684 }
685 }
686 }
687 }
688 project.setParent( parent );
689 }
690
691 Artifact projectArtifact =
692 repositorySystem.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), null,
693 project.getPackaging() );
694 project.setArtifact( projectArtifact );
695
696 if ( project.getFile() != null )
697 {
698 Build build = project.getBuild();
699 project.addScriptSourceRoot( build.getScriptSourceDirectory() );
700 project.addCompileSourceRoot( build.getSourceDirectory() );
701 project.addTestCompileSourceRoot( build.getTestSourceDirectory() );
702 }
703
704 List<Profile> activeProfiles = new ArrayList<>();
705 activeProfiles.addAll( result.getActivePomProfiles( result.getModelIds().get( 0 ) ) );
706 activeProfiles.addAll( result.getActiveExternalProfiles() );
707 project.setActiveProfiles( activeProfiles );
708
709 project.setInjectedProfileIds( "external", getProfileIds( result.getActiveExternalProfiles() ) );
710 for ( String modelId : result.getModelIds() )
711 {
712 project.setInjectedProfileIds( modelId, getProfileIds( result.getActivePomProfiles( modelId ) ) );
713 }
714
715 String modelId = findProfilesXml( result, profilesXmls );
716 if ( modelId != null )
717 {
718 ModelProblem problem =
719 new DefaultModelProblem( "Detected profiles.xml alongside " + modelId
720 + ", this file is no longer supported and was ignored" + ", please use the settings.xml instead",
721 ModelProblem.Severity.WARNING, ModelProblem.Version.V30, model, -1, -1, null );
722 result.getProblems().add( problem );
723 }
724
725
726
727
728
729 project.setProjectBuildingRequest( projectBuildingRequest );
730
731
732 Set<Artifact> pluginArtifacts = new HashSet<>();
733 for ( Plugin plugin : project.getBuildPlugins() )
734 {
735 Artifact artifact = repositorySystem.createPluginArtifact( plugin );
736
737 if ( artifact != null )
738 {
739 pluginArtifacts.add( artifact );
740 }
741 }
742 project.setPluginArtifacts( pluginArtifacts );
743
744
745 Set<Artifact> reportArtifacts = new HashSet<>();
746 for ( ReportPlugin report : project.getReportPlugins() )
747 {
748 Plugin pp = new Plugin();
749 pp.setGroupId( report.getGroupId() );
750 pp.setArtifactId( report.getArtifactId() );
751 pp.setVersion( report.getVersion() );
752
753 Artifact artifact = repositorySystem.createPluginArtifact( pp );
754
755 if ( artifact != null )
756 {
757 reportArtifacts.add( artifact );
758 }
759 }
760 project.setReportArtifacts( reportArtifacts );
761
762
763 Set<Artifact> extensionArtifacts = new HashSet<>();
764 List<Extension> extensions = project.getBuildExtensions();
765 if ( extensions != null )
766 {
767 for ( Extension ext : extensions )
768 {
769 String version;
770 if ( StringUtils.isEmpty( ext.getVersion() ) )
771 {
772 version = "RELEASE";
773 }
774 else
775 {
776 version = ext.getVersion();
777 }
778
779 Artifact artifact =
780 repositorySystem.createArtifact( ext.getGroupId(), ext.getArtifactId(), version, null, "jar" );
781
782 if ( artifact != null )
783 {
784 extensionArtifacts.add( artifact );
785 }
786 }
787 }
788 project.setExtensionArtifacts( extensionArtifacts );
789
790
791 Map<String, Artifact> map = null;
792 if ( repositorySystem != null )
793 {
794 List<Dependency> deps;
795 DependencyManagement dependencyManagement = project.getDependencyManagement();
796 if ( ( dependencyManagement != null ) && ( ( deps = dependencyManagement.getDependencies() ) != null )
797 && ( deps.size() > 0 ) )
798 {
799 map = new HashMap<>();
800 for ( Dependency d : dependencyManagement.getDependencies() )
801 {
802 Artifact artifact = repositorySystem.createDependencyArtifact( d );
803
804 if ( artifact != null )
805 {
806 map.put( d.getManagementKey(), artifact );
807 }
808 }
809 }
810 else
811 {
812 map = Collections.emptyMap();
813 }
814 }
815 project.setManagedVersionMap( map );
816
817
818 if ( project.getDistributionManagement() != null
819 && project.getDistributionManagement().getRepository() != null )
820 {
821 try
822 {
823 DeploymentRepository r = project.getDistributionManagement().getRepository();
824 if ( !StringUtils.isEmpty( r.getId() ) && !StringUtils.isEmpty( r.getUrl() ) )
825 {
826 ArtifactRepository repo = repositorySystem.buildArtifactRepository( r );
827 repositorySystem.injectProxy( projectBuildingRequest.getRepositorySession(),
828 Arrays.asList( repo ) );
829 repositorySystem.injectAuthentication( projectBuildingRequest.getRepositorySession(),
830 Arrays.asList( repo ) );
831 project.setReleaseArtifactRepository( repo );
832 }
833 }
834 catch ( InvalidRepositoryException e )
835 {
836 throw new IllegalStateException( "Failed to create release distribution repository for "
837 + project.getId(), e );
838 }
839 }
840
841
842 if ( project.getDistributionManagement() != null
843 && project.getDistributionManagement().getSnapshotRepository() != null )
844 {
845 try
846 {
847 DeploymentRepository r = project.getDistributionManagement().getSnapshotRepository();
848 if ( !StringUtils.isEmpty( r.getId() ) && !StringUtils.isEmpty( r.getUrl() ) )
849 {
850 ArtifactRepository repo = repositorySystem.buildArtifactRepository( r );
851 repositorySystem.injectProxy( projectBuildingRequest.getRepositorySession(),
852 Arrays.asList( repo ) );
853 repositorySystem.injectAuthentication( projectBuildingRequest.getRepositorySession(),
854 Arrays.asList( repo ) );
855 project.setSnapshotArtifactRepository( repo );
856 }
857 }
858 catch ( InvalidRepositoryException e )
859 {
860 throw new IllegalStateException( "Failed to create snapshot distribution repository for "
861 + project.getId(), e );
862 }
863 }
864 }
865
866 private static String inheritedGroupId( final ModelBuildingResult result, final int modelIndex )
867 {
868 String groupId = null;
869 final String modelId = result.getModelIds().get( modelIndex );
870
871 if ( !modelId.isEmpty() )
872 {
873 final Model model = result.getRawModel( modelId );
874 groupId = model.getGroupId() != null
875 ? model.getGroupId()
876 : inheritedGroupId( result, modelIndex + 1 );
877
878 }
879
880 return groupId;
881 }
882
883 private static String inheritedVersion( final ModelBuildingResult result, final int modelIndex )
884 {
885 String version = null;
886 final String modelId = result.getModelIds().get( modelIndex );
887
888 if ( !modelId.isEmpty() )
889 {
890 final Model model = result.getRawModel( modelId );
891 version = model.getVersion() != null
892 ? model.getVersion()
893 : inheritedVersion( result, modelIndex + 1 );
894
895 }
896
897 return version;
898 }
899
900 private String findProfilesXml( ModelBuildingResult result, Map<File, Boolean> profilesXmls )
901 {
902 for ( String modelId : result.getModelIds() )
903 {
904 Model model = result.getRawModel( modelId );
905
906 File basedir = model.getProjectDirectory();
907 if ( basedir == null )
908 {
909 break;
910 }
911
912 Boolean profilesXml = profilesXmls.get( basedir );
913 if ( profilesXml == null )
914 {
915 profilesXml = new File( basedir, "profiles.xml" ).exists();
916 profilesXmls.put( basedir, profilesXml );
917 }
918 if ( profilesXml )
919 {
920 return modelId;
921 }
922 }
923
924 return null;
925 }
926
927 class InternalConfig
928 {
929
930 public final ProjectBuildingRequest request;
931
932 public final RepositorySystemSession session;
933
934 public final List<RemoteRepository> repositories;
935
936 public final ReactorModelPool modelPool;
937
938 public final ReactorModelCache modelCache;
939
940 InternalConfig( ProjectBuildingRequest request, ReactorModelPool modelPool, ReactorModelCache modelCache )
941 {
942 this.request = request;
943 this.modelPool = modelPool;
944 this.modelCache = modelCache;
945 session =
946 LegacyLocalRepositoryManager.overlay( request.getLocalRepository(), request.getRepositorySession(),
947 repoSystem );
948 repositories = RepositoryUtils.toRepos( request.getRemoteRepositories() );
949 }
950
951 }
952
953 }