File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\util\ExecutionEventLogger.java.html

1 package hudson.maven.util;
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 hudson.tasks._maven.Maven3MojoNote;
23
24 import java.io.IOException;
25 import java.util.Date;
26
27 import org.apache.maven.execution.AbstractExecutionListener;
28 import org.apache.maven.execution.BuildFailure;
29 import org.apache.maven.execution.BuildSuccess;
30 import org.apache.maven.execution.BuildSummary;
31 import org.apache.maven.execution.ExecutionEvent;
32 import org.apache.maven.execution.MavenExecutionResult;
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.plugin.MojoExecution;
35 import org.apache.maven.project.MavenProject;
36 import org.codehaus.plexus.logging.Logger;
37
38 /**
39 * Logs execution events to a user-supplied logger.
40 *
41 * @author Benjamin Bentmann
42 */
43 // Note: copied from package org.apache.maven.cli with just one minor adaption for Maven3MojoNote
44 public class ExecutionEventLogger
45 extends AbstractExecutionListener
46 {
47 private final Logger logger;
48
49 private static final int LINE_LENGTH = 72;
50
51 public ExecutionEventLogger( Logger logger )
52 {
53 if ( logger == null )
54 {
55 throw new IllegalArgumentException( "logger missing" );
56 }
57
58 this.logger = logger;
59 }
60
61 private static String chars( char c, int count )
62 {
63 StringBuilder buffer = new StringBuilder( count );
64
65 for ( int i = count; i > 0; i-- )
66 {
67 buffer.append( c );
68 }
69
70 return buffer.toString();
71 }
72
73 private static String getFormattedTime( long time )
74 {
75 // NOTE: DateFormat is not suitable to format timespans of 24h+
76
77 long h = time / ( 60 * 60 * 1000 );
78 long m = ( time - h * 60 * 60 * 1000 ) / ( 60 * 1000 );
79 long s = ( time - h * 60 * 60 * 1000 - m * 60 * 1000 ) / 1000;
80 long ms = time % 1000;
81
82 String format;
83 if ( h > 0 )
84 {
85 format = "%1$d:%2$02d:%3$02d.%4$03ds";
86 }
87 else if ( m > 0 )
88 {
89 format = "%2$d:%3$02d.%4$03ds";
90 }
91 else
92 {
93 format = "%3$d.%4$03ds";
94 }
95
96 return String.format( format, h, m, s, ms );
97 }
98
99 @Override
100 public void projectDiscoveryStarted( ExecutionEvent event )
101 {
102 if ( logger.isInfoEnabled() )
103 {
104 logger.info( "Scanning for projects..." );
105 }
106 }
107
108 @Override
109 public void sessionStarted( ExecutionEvent event )
110 {
111 if ( logger.isInfoEnabled() && event.getSession().getProjects().size() > 1 )
112 {
113 logger.info( chars( '-', LINE_LENGTH ) );
114
115 logger.info( "Reactor Build Order:" );
116
117 logger.info( "" );
118
119 for ( MavenProject project : event.getSession().getProjects() )
120 {
121 logger.info( project.getName() );
122 }
123 }
124 }
125
126 @Override
127 public void sessionEnded( ExecutionEvent event )
128 {
129 if ( logger.isInfoEnabled() )
130 {
131 if ( event.getSession().getProjects().size() > 1 )
132 {
133 logReactorSummary( event.getSession() );
134 }
135
136 logResult( event.getSession() );
137
138 logStats( event.getSession() );
139
140 logger.info( chars( '-', LINE_LENGTH ) );
141 }
142 }
143
144 private void logReactorSummary( MavenSession session )
145 {
146 logger.info( chars( '-', LINE_LENGTH ) );
147
148 logger.info( "Reactor Summary:" );
149
150 logger.info( "" );
151
152 MavenExecutionResult result = session.getResult();
153
154 for ( MavenProject project : session.getProjects() )
155 {
156 StringBuilder buffer = new StringBuilder( 128 );
157
158 buffer.append( project.getName() );
159
160 buffer.append( ' ' );
161 while ( buffer.length() < LINE_LENGTH - 21 )
162 {
163 buffer.append( '.' );
164 }
165 buffer.append( ' ' );
166
167 BuildSummary buildSummary = result.getBuildSummary( project );
168
169 if ( buildSummary == null )
170 {
171 buffer.append( "SKIPPED" );
172 }
173 else if ( buildSummary instanceof BuildSuccess )
174 {
175 buffer.append( "SUCCESS [" );
176 buffer.append( getFormattedTime( buildSummary.getTime() ) );
177 buffer.append( "]" );
178 }
179 else if ( buildSummary instanceof BuildFailure )
180 {
181 buffer.append( "FAILURE [" );
182 buffer.append( getFormattedTime( buildSummary.getTime() ) );
183 buffer.append( "]" );
184 }
185
186 logger.info( buffer.toString() );
187 }
188 }
189
190 private void logResult( MavenSession session )
191 {
192 logger.info( chars( '-', LINE_LENGTH ) );
193
194 if ( session.getResult().hasExceptions() )
195 {
196 logger.info( "BUILD FAILURE" );
197 }
198 else
199 {
200 logger.info( "BUILD SUCCESS" );
201 }
202 }
203
204 private void logStats( MavenSession session )
205 {
206 logger.info( chars( '-', LINE_LENGTH ) );
207
208 Date finish = new Date();
209
210 long time = finish.getTime() - session.getRequest().getStartTime().getTime();
211
212 String wallClock = session.getRequest().isThreadConfigurationPresent() ? " (Wall Clock)" : "";
213
214 logger.info( "Total time: " + getFormattedTime( time ) + wallClock );
215
216 logger.info( "Finished at: " + finish );
217
218 System.gc();
219
220 Runtime r = Runtime.getRuntime();
221
222 long MB = 1024 * 1024;
223
224 logger.info( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / MB + "M/" + r.totalMemory() / MB + "M" );
225 }
226
227 @Override
228 public void projectSkipped( ExecutionEvent event )
229 {
230 if ( logger.isInfoEnabled() )
231 {
232 logger.info( chars( ' ', LINE_LENGTH ) );
233 logger.info( chars( '-', LINE_LENGTH ) );
234
235 logger.info( "Skipping " + event.getProject().getName() );
236 logger.info( "This project has been banned from the build due to previous failures." );
237
238 logger.info( chars( '-', LINE_LENGTH ) );
239 }
240 }
241
242 @Override
243 public void projectStarted( ExecutionEvent event )
244 {
245 if ( logger.isInfoEnabled() )
246 {
247 logger.info( chars( ' ', LINE_LENGTH ) );
248 logger.info( chars( '-', LINE_LENGTH ) );
249
250 logger.info( "Building " + event.getProject().getName() + " " + event.getProject().getVersion() );
251
252 logger.info( chars( '-', LINE_LENGTH ) );
253 }
254 }
255
256 @Override
257 public void mojoSkipped( ExecutionEvent event )
258 {
259 if ( logger.isWarnEnabled() )
260 {
261 logger.warn( "Goal " + event.getMojoExecution().getGoal()
262 + " requires online mode for execution but Maven is currently offline, skipping" );
263 }
264 }
265
266 /**
267 * <pre>--- mojo-artifactId:version:goal (mojo-executionId) @ project-artifactId ---</pre>
268 */
269 @Override
270 public void mojoStarted( ExecutionEvent event )
271 {
272 if ( logger.isInfoEnabled() )
273 {
274 final Maven3MojoNote note = new Maven3MojoNote();
275 final StringBuilder buffer = new StringBuilder( 128 );
276 try {
277 buffer.append( note.encode() );
278 } catch ( IOException e ) {
279 // As we use only memory buffers this should not happen, ever.
280 throw new RuntimeException( "Could not encode note?", e );
281 }
282 buffer.append( "--- " );
283 append( buffer, event.getMojoExecution() );
284 append( buffer, event.getProject() );
285 buffer.append( " ---" );
286
287 logger.info( "" );
288 logger.info( buffer.toString() );
289 }
290 }
291
292 /**
293 * <pre>>>> mojo-artifactId:version:goal (mojo-executionId) @ project-artifactId >>></pre>
294 */
295 @Override
296 public void forkStarted( ExecutionEvent event )
297 {
298 if ( logger.isInfoEnabled() )
299 {
300 StringBuilder buffer = new StringBuilder( 128 );
301
302 buffer.append( ">>> " );
303 append( buffer, event.getMojoExecution() );
304 append( buffer, event.getProject() );
305 buffer.append( " >>>" );
306
307 logger.info( "" );
308 logger.info( buffer.toString() );
309 }
310 }
311
312 /**
313 * <pre>&lt;&lt;&lt; mojo-artifactId:version:goal (mojo-executionId) @ project-artifactId &lt;&lt;&lt;</pre>
314 */
315 @Override
316 public void forkSucceeded( ExecutionEvent event )
317 {
318 if ( logger.isInfoEnabled() )
319 {
320 StringBuilder buffer = new StringBuilder( 128 );
321
322 buffer.append( "<<< " );
323 append( buffer, event.getMojoExecution() );
324 append( buffer, event.getProject() );
325 buffer.append( " <<<" );
326
327 logger.info( "" );
328 logger.info( buffer.toString() );
329 }
330 }
331
332 private void append( StringBuilder buffer, MojoExecution me )
333 {
334 buffer.append( me.getArtifactId() ).append( ':' ).append( me.getVersion() );
335 buffer.append( ':' ).append( me.getGoal() );
336 if ( me.getExecutionId() != null )
337 {
338 buffer.append( " (" ).append( me.getExecutionId() ).append( ')' );
339 }
340 }
341
342 private void append( StringBuilder buffer, MavenProject project )
343 {
344 buffer.append( " @ " ).append( project.getArtifactId() );
345 }
346
347 @Override
348 public void forkedProjectStarted( ExecutionEvent event )
349 {
350 if ( logger.isInfoEnabled() && event.getMojoExecution().getForkedExecutions().size() > 1 )
351 {
352 logger.info( chars( ' ', LINE_LENGTH ) );
353 logger.info( chars( '>', LINE_LENGTH ) );
354
355 logger.info( "Forking " + event.getProject().getName() + " " + event.getProject().getVersion() );
356
357 logger.info( chars( '>', LINE_LENGTH ) );
358 }
359 }
360
361 }
362