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