1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.execution.MavenSession;
23 import org.apache.maven.project.MavenProject;
24 import org.codehaus.plexus.component.annotations.Component;
25 import org.codehaus.plexus.component.annotations.Requirement;
26 import org.codehaus.plexus.logging.Logger;
27
28 import java.util.List;
29 import java.util.Map;
30 import java.util.concurrent.Callable;
31 import java.util.concurrent.CompletionService;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.Future;
34
35
36
37
38
39
40
41
42
43
44 @Component( role = LifecycleThreadedBuilder.class )
45 public class LifecycleThreadedBuilder
46 {
47
48 @Requirement
49 private Logger logger;
50
51 @Requirement
52 private LifecycleModuleBuilder lifecycleModuleBuilder;
53
54
55 public LifecycleThreadedBuilder()
56 {
57 }
58
59 public void build( MavenSession session, ReactorContext reactorContext, ProjectBuildList projectBuilds,
60 List<TaskSegment> currentTaskSegment, ConcurrencyDependencyGraph analyzer,
61 CompletionService<ProjectSegment> service )
62 {
63
64
65 ThreadOutputMuxer muxer = null;
66
67 for ( TaskSegment taskSegment : currentTaskSegment )
68 {
69 Map<MavenProject, ProjectSegment> projectBuildMap = projectBuilds.selectSegment( taskSegment );
70 try
71 {
72 multiThreadedProjectTaskSegmentBuild( analyzer, reactorContext, session, service, taskSegment,
73 projectBuildMap, muxer );
74 if ( reactorContext.getReactorBuildStatus().isHalted() )
75 {
76 break;
77 }
78 }
79 catch ( Exception e )
80 {
81 session.getResult().addException( e );
82 break;
83 }
84
85 }
86 }
87
88 private void multiThreadedProjectTaskSegmentBuild( ConcurrencyDependencyGraph analyzer,
89 ReactorContext reactorContext, MavenSession rootSession,
90 CompletionService<ProjectSegment> service,
91 TaskSegment taskSegment,
92 Map<MavenProject, ProjectSegment> projectBuildList,
93 ThreadOutputMuxer muxer )
94 {
95
96
97 for ( MavenProject mavenProject : analyzer.getRootSchedulableBuilds() )
98 {
99 ProjectSegment projectSegment = projectBuildList.get( mavenProject );
100 logger.debug( "Scheduling: " + projectSegment.getProject() );
101 Callable<ProjectSegment> cb =
102 createBuildCallable( rootSession, projectSegment, reactorContext, taskSegment, muxer );
103 service.submit( cb );
104 }
105
106
107 for ( int i = 0; i < analyzer.getNumberOfBuilds(); i++ )
108 {
109 try
110 {
111 ProjectSegment projectBuild = service.take().get();
112 if ( reactorContext.getReactorBuildStatus().isHalted() )
113 {
114 break;
115 }
116 final List<MavenProject> newItemsThatCanBeBuilt =
117 analyzer.markAsFinished( projectBuild.getProject() );
118 for ( MavenProject mavenProject : newItemsThatCanBeBuilt )
119 {
120 ProjectSegment scheduledDependent = projectBuildList.get( mavenProject );
121 logger.debug( "Scheduling: " + scheduledDependent );
122 Callable<ProjectSegment> cb =
123 createBuildCallable( rootSession, scheduledDependent, reactorContext, taskSegment, muxer );
124 service.submit( cb );
125 }
126 }
127 catch ( InterruptedException e )
128 {
129 rootSession.getResult().addException( e );
130 break;
131 }
132 catch ( ExecutionException e )
133 {
134 rootSession.getResult().addException( e );
135 break;
136 }
137 }
138
139
140
141 Future<ProjectSegment> unprocessed;
142 while ( ( unprocessed = service.poll() ) != null )
143 {
144 try
145 {
146 unprocessed.get();
147 }
148 catch ( InterruptedException e )
149 {
150 throw new RuntimeException( e );
151 }
152 catch ( ExecutionException e )
153 {
154 throw new RuntimeException( e );
155 }
156 }
157 }
158
159 private Callable<ProjectSegment> createBuildCallable( final MavenSession rootSession,
160 final ProjectSegment projectBuild,
161 final ReactorContext reactorContext,
162 final TaskSegment taskSegment, final ThreadOutputMuxer muxer )
163 {
164 return new Callable<ProjectSegment>()
165 {
166 public ProjectSegment call()
167 {
168
169 lifecycleModuleBuilder.buildProject( projectBuild.getSession(), rootSession, reactorContext,
170 projectBuild.getProject(), taskSegment );
171
172
173 return projectBuild;
174 }
175 };
176 }
177 }