Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
InvokerSession |
|
| 2.0833333333333335;2,083 |
1 | package org.apache.maven.plugin.invoker; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * Tracks a set of build jobs and their results. | |
33 | * | |
34 | * @author Benjamin Bentmann | |
35 | */ | |
36 | class InvokerSession | |
37 | { | |
38 | ||
39 | private List buildJobs; | |
40 | ||
41 | private List failedJobs; | |
42 | ||
43 | private List successfulJobs; | |
44 | ||
45 | private List skippedJobs; | |
46 | ||
47 | /** | |
48 | * Creates a new empty session. | |
49 | */ | |
50 | public InvokerSession() | |
51 | 0 | { |
52 | 0 | buildJobs = new ArrayList(); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Creates a session that initially contains the specified build jobs. | |
57 | * | |
58 | * @param buildJobs The build jobs to set, must not be <code>null</code>. | |
59 | */ | |
60 | public InvokerSession( BuildJob[] buildJobs ) | |
61 | 0 | { |
62 | 0 | this.buildJobs = new ArrayList( Arrays.asList( buildJobs ) ); |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * Adds the specified build job to this session. | |
67 | * | |
68 | * @param buildJob The build job to add, must not be <code>null</code>. | |
69 | */ | |
70 | public void addJob( BuildJob buildJob ) | |
71 | { | |
72 | 0 | buildJobs.add( buildJob ); |
73 | ||
74 | 0 | resetStats(); |
75 | 0 | } |
76 | ||
77 | /** | |
78 | * Sets the build jobs of this session. | |
79 | * | |
80 | * @param buildJobs The build jobs to set, must not be <code>null</code>. | |
81 | */ | |
82 | public void setJobs( List buildJobs ) | |
83 | { | |
84 | 0 | this.buildJobs = new ArrayList( buildJobs ); |
85 | ||
86 | 0 | resetStats(); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Gets the build jobs in this session. | |
91 | * | |
92 | * @return The build jobs in this session, can be empty but never <code>null</code>. | |
93 | */ | |
94 | public List getJobs() | |
95 | { | |
96 | 0 | return buildJobs; |
97 | } | |
98 | ||
99 | /** | |
100 | * Gets the successful build jobs in this session. | |
101 | * | |
102 | * @return The successful build jobs in this session, can be empty but never <code>null</code>. | |
103 | */ | |
104 | public List getSuccessfulJobs() | |
105 | { | |
106 | 0 | updateStats(); |
107 | ||
108 | 0 | return successfulJobs; |
109 | } | |
110 | ||
111 | /** | |
112 | * Gets the failed build jobs in this session. | |
113 | * | |
114 | * @return The failed build jobs in this session, can be empty but never <code>null</code>. | |
115 | */ | |
116 | public List getFailedJobs() | |
117 | { | |
118 | 0 | updateStats(); |
119 | ||
120 | 0 | return failedJobs; |
121 | } | |
122 | ||
123 | /** | |
124 | * Gets the skipped build jobs in this session. | |
125 | * | |
126 | * @return The skipped build jobs in this session, can be empty but never <code>null</code>. | |
127 | */ | |
128 | public List getSkippedJobs() | |
129 | { | |
130 | 0 | updateStats(); |
131 | ||
132 | 0 | return skippedJobs; |
133 | } | |
134 | ||
135 | private void resetStats() | |
136 | { | |
137 | 0 | successfulJobs = null; |
138 | 0 | failedJobs = null; |
139 | 0 | skippedJobs = null; |
140 | 0 | } |
141 | ||
142 | private void updateStats() | |
143 | { | |
144 | 0 | if ( successfulJobs != null && skippedJobs != null && failedJobs != null ) |
145 | { | |
146 | 0 | return; |
147 | } | |
148 | ||
149 | 0 | successfulJobs = new ArrayList(); |
150 | 0 | failedJobs = new ArrayList(); |
151 | 0 | skippedJobs = new ArrayList(); |
152 | ||
153 | 0 | for ( Iterator iterator = buildJobs.iterator(); iterator.hasNext(); ) |
154 | { | |
155 | 0 | BuildJob buildJob = (BuildJob) iterator.next(); |
156 | ||
157 | 0 | if ( BuildJob.Result.SUCCESS.equals( buildJob.getResult() ) ) |
158 | { | |
159 | 0 | successfulJobs.add( buildJob ); |
160 | } | |
161 | 0 | else if ( BuildJob.Result.SKIPPED.equals( buildJob.getResult() ) ) |
162 | { | |
163 | 0 | skippedJobs.add( buildJob ); |
164 | } | |
165 | 0 | else if ( buildJob.getResult() != null ) |
166 | { | |
167 | 0 | failedJobs.add( buildJob ); |
168 | } | |
169 | } | |
170 | 0 | } |
171 | ||
172 | /** | |
173 | * Prints a summary of this session to the specified logger. | |
174 | * | |
175 | * @param logger The mojo logger to output messages to, must not be <code>null</code>. | |
176 | * @param ignoreFailures A flag whether failures should be ignored or whether a build failure should be signaled. | |
177 | */ | |
178 | public void logSummary( Log logger, boolean ignoreFailures ) | |
179 | { | |
180 | 0 | updateStats(); |
181 | ||
182 | 0 | String separator = "---------------------------------------"; |
183 | ||
184 | 0 | logger.info( separator ); |
185 | 0 | logger.info( "Execution Summary:" ); |
186 | 0 | logger.info( " Builds Passing: " + successfulJobs.size() ); |
187 | 0 | logger.info( " Builds Failing: " + failedJobs.size() ); |
188 | 0 | logger.info( " Builds Skipped: " + skippedJobs.size() ); |
189 | 0 | logger.info( separator ); |
190 | ||
191 | 0 | if ( !failedJobs.isEmpty() ) |
192 | { | |
193 | 0 | String heading = "The following builds failed:"; |
194 | 0 | if ( ignoreFailures ) |
195 | { | |
196 | 0 | logger.warn( heading ); |
197 | } | |
198 | else | |
199 | { | |
200 | 0 | logger.error( heading ); |
201 | } | |
202 | ||
203 | 0 | for ( Iterator it = failedJobs.iterator(); it.hasNext(); ) |
204 | { | |
205 | 0 | BuildJob buildJob = (BuildJob) it.next(); |
206 | ||
207 | 0 | String item = "* " + buildJob.getProject(); |
208 | 0 | if ( ignoreFailures ) |
209 | { | |
210 | 0 | logger.warn( item ); |
211 | } | |
212 | else | |
213 | { | |
214 | 0 | logger.error( item ); |
215 | } | |
216 | } | |
217 | ||
218 | 0 | logger.info( separator ); |
219 | } | |
220 | 0 | } |
221 | ||
222 | /** | |
223 | * Handles the build failures in this session. | |
224 | * | |
225 | * @param logger The mojo logger to output messages to, must not be <code>null</code>. | |
226 | * @param ignoreFailures A flag whether failures should be ignored or whether a build failure should be signaled. | |
227 | * @throws MojoFailureException If failures are present and not ignored. | |
228 | */ | |
229 | public void handleFailures( Log logger, boolean ignoreFailures ) | |
230 | throws MojoFailureException | |
231 | { | |
232 | 0 | updateStats(); |
233 | ||
234 | 0 | if ( !failedJobs.isEmpty() ) |
235 | { | |
236 | 0 | String message = failedJobs.size() + " build" + ( failedJobs.size() == 1 ? "" : "s" ) + " failed."; |
237 | ||
238 | 0 | if ( ignoreFailures ) |
239 | { | |
240 | 0 | logger.warn( "Ignoring that " + message ); |
241 | } | |
242 | else | |
243 | { | |
244 | 0 | throw new MojoFailureException( this, message, message ); |
245 | } | |
246 | } | |
247 | 0 | } |
248 | ||
249 | } |