View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * <p>
39   * Logs debug output from the various lifecycle phases.
40   * </p>
41   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
42   *
43   * @since 3.0
44   * @author Benjamin Bentmann
45   * @author Jason van Zyl
46   * @author Kristian Rosenvold (extracted class only)
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 }