1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.lifecycle.internal;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.TreeSet;
29  
30  import org.apache.maven.lifecycle.MavenExecutionPlan;
31  import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
32  import org.apache.maven.plugin.MojoExecution;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  @Named
51  @Singleton
52  public class LifecycleDebugLogger {
53      private final Logger logger = LoggerFactory.getLogger(getClass());
54  
55      public void debug(String s) {
56          logger.debug(s);
57      }
58  
59      public void info(String s) {
60          logger.info(s);
61      }
62  
63      public void debugReactorPlan(ProjectBuildList projectBuilds) {
64          if (!logger.isDebugEnabled()) {
65              return;
66          }
67  
68          logger.debug("=== REACTOR BUILD PLAN ================================================");
69  
70          for (Iterator<ProjectSegment> it = projectBuilds.iterator(); it.hasNext(); ) {
71              ProjectSegment projectBuild = it.next();
72  
73              logger.debug("Project: " + projectBuild.getProject().getId());
74              logger.debug("Tasks:   " + projectBuild.getTaskSegment().getTasks());
75              logger.debug("Style:   " + (projectBuild.getTaskSegment().isAggregating() ? "Aggregating" : "Regular"));
76  
77              if (it.hasNext()) {
78                  logger.debug("-----------------------------------------------------------------------");
79              }
80          }
81  
82          logger.debug("=======================================================================");
83      }
84  
85      public void debugProjectPlan(MavenProject currentProject, MavenExecutionPlan executionPlan) {
86          if (!logger.isDebugEnabled()) {
87              return;
88          }
89  
90          logger.debug("=== PROJECT BUILD PLAN ================================================");
91          logger.debug("Project:       " + BuilderCommon.getKey(currentProject));
92  
93          debugDependencyRequirements(executionPlan.getMojoExecutions());
94  
95          logger.debug("Repositories (dependencies): " + currentProject.getRemoteProjectRepositories());
96          logger.debug("Repositories (plugins)     : " + currentProject.getRemotePluginRepositories());
97  
98          for (ExecutionPlanItem mojoExecution : executionPlan) {
99              debugMojoExecution(mojoExecution.getMojoExecution());
100         }
101 
102         logger.debug("=======================================================================");
103     }
104 
105     private void debugMojoExecution(MojoExecution mojoExecution) {
106         String mojoExecId =
107                 mojoExecution.getGroupId() + ':' + mojoExecution.getArtifactId() + ':' + mojoExecution.getVersion()
108                         + ':' + mojoExecution.getGoal() + " (" + mojoExecution.getExecutionId() + ')';
109 
110         Map<String, List<MojoExecution>> forkedExecutions = mojoExecution.getForkedExecutions();
111         if (!forkedExecutions.isEmpty()) {
112             for (Map.Entry<String, List<MojoExecution>> fork : forkedExecutions.entrySet()) {
113                 logger.debug("--- init fork of " + fork.getKey() + " for " + mojoExecId + " ---");
114 
115                 debugDependencyRequirements(fork.getValue());
116 
117                 for (MojoExecution forkedExecution : fork.getValue()) {
118                     debugMojoExecution(forkedExecution);
119                 }
120 
121                 logger.debug("--- exit fork of " + fork.getKey() + " for " + mojoExecId + " ---");
122             }
123         }
124 
125         logger.debug("-----------------------------------------------------------------------");
126         logger.debug("Goal:          " + mojoExecId);
127         logger.debug(
128                 "Style:         " + (mojoExecution.getMojoDescriptor().isAggregator() ? "Aggregating" : "Regular"));
129         logger.debug("Configuration: " + mojoExecution.getConfiguration());
130     }
131 
132     private void debugDependencyRequirements(List<MojoExecution> mojoExecutions) {
133         Set<String> scopesToCollect = new TreeSet<>();
134         Set<String> scopesToResolve = new TreeSet<>();
135 
136         for (MojoExecution mojoExecution : mojoExecutions) {
137             MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
138 
139             String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
140             if (StringUtils.isNotEmpty(scopeToCollect)) {
141                 scopesToCollect.add(scopeToCollect);
142             }
143 
144             String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
145             if (StringUtils.isNotEmpty(scopeToResolve)) {
146                 scopesToResolve.add(scopeToResolve);
147             }
148         }
149 
150         logger.debug("Dependencies (collect): " + scopesToCollect);
151         logger.debug("Dependencies (resolve): " + scopesToResolve);
152     }
153 }