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 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   * <p>
41   * Logs debug output from the various lifecycle phases.
42   * </p>
43   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
44   *
45   * @since 3.0
46   * @author Benjamin Bentmann
47   * @author Jason van Zyl
48   * @author Kristian Rosenvold (extracted class only)
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 }