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