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