1 package com.soebes.maven.extensions;
2
3 import java.util.Map;
4 import java.util.Map.Entry;
5 import java.util.concurrent.ConcurrentHashMap;
6
7 import org.apache.maven.execution.ExecutionEvent;
8 import org.apache.maven.plugin.MojoExecution;
9 import org.apache.maven.project.MavenProject;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13
14
15
16 class GoalTimer
17 {
18 private final Logger LOGGER = LoggerFactory.getLogger( getClass() );
19
20 private Map<ProjectGoal, SystemTime> timerEvents;
21
22 public GoalTimer()
23 {
24 this.timerEvents = new ConcurrentHashMap<>();
25 }
26
27 public boolean hasEvents()
28 {
29 return !timerEvents.isEmpty();
30 }
31
32 private ProjectKey createProjectKey( MavenProject project )
33 {
34 return new ProjectKey( project.getGroupId(), project.getArtifactId(), project.getVersion() );
35 }
36
37 private GoalKey createGoalKey( MojoExecution mojo )
38 {
39 return new GoalKey( mojo.getGroupId(), mojo.getArtifactId(), mojo.getVersion(), mojo.getGoal(),
40 mojo.getExecutionId() );
41 }
42
43 public void mojoStart( ExecutionEvent event )
44 {
45 ProjectGoal pm =
46 new ProjectGoal( createProjectKey( event.getProject() ), createGoalKey( event.getMojoExecution() ) );
47 timerEvents.put( pm, new SystemTime().start() );
48 }
49
50 public void mojoStop( ExecutionEvent event )
51 {
52 ProjectGoal pm =
53 new ProjectGoal( createProjectKey( event.getProject() ), createGoalKey( event.getMojoExecution() ) );
54 if ( !timerEvents.containsKey( pm ) )
55 {
56 throw new IllegalArgumentException( "Unknown mojoId (" + pm + ")" );
57 }
58 timerEvents.get( pm ).stop();
59 }
60
61 public void report()
62 {
63 for ( Entry<ProjectGoal, SystemTime> item : this.timerEvents.entrySet() )
64 {
65 LOGGER.info( "{} ms : {}", String.format( "%8d", item.getValue().getElapsedTime() ),
66 item.getKey().getId() );
67 }
68 }
69 }