1 package org.apache.maven.plugin.invoker;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugin.invoker.model.BuildJob;
28 import org.apache.maven.plugin.logging.Log;
29
30
31
32
33
34
35 class InvokerSession
36 {
37
38 private List<BuildJob> buildJobs;
39
40 private List<BuildJob> failedJobs;
41
42 private List<BuildJob> errorJobs;
43
44 private List<BuildJob> successfulJobs;
45
46 private List<BuildJob> skippedJobs;
47
48
49
50
51 public InvokerSession()
52 {
53 buildJobs = new ArrayList<BuildJob>();
54 }
55
56
57
58
59
60
61 public InvokerSession( BuildJob[] buildJobs )
62 {
63 this.buildJobs = new ArrayList<BuildJob>( Arrays.asList( buildJobs ) );
64 }
65
66
67
68
69
70
71 public void addJob( BuildJob buildJob )
72 {
73 buildJobs.add( buildJob );
74
75 resetStats();
76 }
77
78
79
80
81
82
83 public void setJobs( List<? extends BuildJob> buildJobs )
84 {
85 this.buildJobs = new ArrayList<BuildJob>( buildJobs );
86
87 resetStats();
88 }
89
90
91
92
93
94
95 public List<BuildJob> getJobs()
96 {
97 return buildJobs;
98 }
99
100
101
102
103
104
105 public List<BuildJob> getSuccessfulJobs()
106 {
107 updateStats();
108
109 return successfulJobs;
110 }
111
112
113
114
115
116
117 public List<BuildJob> getFailedJobs()
118 {
119 updateStats();
120
121 return failedJobs;
122 }
123
124
125
126
127
128
129 public List<BuildJob> getErrorJobs()
130 {
131 updateStats();
132
133 return errorJobs;
134 }
135
136
137
138
139
140
141 public List<BuildJob> getSkippedJobs()
142 {
143 updateStats();
144
145 return skippedJobs;
146 }
147
148 private void resetStats()
149 {
150 successfulJobs = null;
151 failedJobs = null;
152 skippedJobs = null;
153 errorJobs = null;
154 }
155
156 private void updateStats()
157 {
158 if ( successfulJobs != null && skippedJobs != null && failedJobs != null && errorJobs != null )
159 {
160 return;
161 }
162
163 successfulJobs = new ArrayList<BuildJob>();
164 failedJobs = new ArrayList<BuildJob>();
165 skippedJobs = new ArrayList<BuildJob>();
166 errorJobs = new ArrayList<BuildJob>();
167
168 for ( BuildJob buildJob : buildJobs )
169 {
170 if ( BuildJob.Result.SUCCESS.equals( buildJob.getResult() ) )
171 {
172 successfulJobs.add( buildJob );
173 }
174 else if ( BuildJob.Result.SKIPPED.equals( buildJob.getResult() ) )
175 {
176 skippedJobs.add( buildJob );
177 }
178 else if ( BuildJob.Result.ERROR.equals( buildJob.getResult() ) )
179 {
180 errorJobs.add( buildJob );
181 }
182 else if ( buildJob.getResult() != null )
183 {
184 failedJobs.add( buildJob );
185 }
186 }
187 }
188
189
190
191
192
193
194
195 public void logSummary( Log logger, boolean ignoreFailures )
196 {
197 updateStats();
198
199 String separator = "-------------------------------------------------";
200
201 logger.info( separator );
202 logger.info( "Build Summary:" );
203 logger.info( " Passed: " + successfulJobs.size() + ", Failed: " + failedJobs.size() + ", Errors: "
204 + errorJobs.size() + ", Skipped: " + skippedJobs.size() );
205 logger.info( separator );
206
207 if ( !failedJobs.isEmpty() )
208 {
209 String heading = "The following builds failed:";
210 if ( ignoreFailures )
211 {
212 logger.warn( heading );
213 }
214 else
215 {
216 logger.error( heading );
217 }
218
219 for ( BuildJob buildJob : failedJobs )
220 {
221 String item = "* " + buildJob.getProject();
222 if ( ignoreFailures )
223 {
224 logger.warn( item );
225 }
226 else
227 {
228 logger.error( item );
229 }
230 }
231
232 logger.info( separator );
233 }
234 }
235
236
237
238
239
240
241
242
243 public void handleFailures( Log logger, boolean ignoreFailures )
244 throws MojoFailureException
245 {
246 updateStats();
247
248 if ( !failedJobs.isEmpty() )
249 {
250 String message = failedJobs.size() + " build" + ( failedJobs.size() == 1 ? "" : "s" ) + " failed.";
251
252 if ( ignoreFailures )
253 {
254 logger.warn( "Ignoring that " + message );
255 }
256 else
257 {
258 throw new MojoFailureException( message + " See console output above for details." );
259 }
260 }
261
262 if ( !errorJobs.isEmpty() )
263 {
264 String message = errorJobs.size() + " build" + ( errorJobs.size() == 1 ? "" : "s" ) + " in error.";
265
266 if ( ignoreFailures )
267 {
268 logger.warn( "Ignoring that " + message );
269 }
270 else
271 {
272 throw new MojoFailureException( message + " See console output above for details." );
273 }
274 }
275 }
276
277 }