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.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * <p>
40   * Logs debug output from the various lifecycle phases.
41   * </p>
42   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
43   *
44   * @since 3.0
45   * @author Benjamin Bentmann
46   * @author Jason van Zyl
47   * @author Kristian Rosenvold (extracted class only)
48   */
49  @Named
50  @Singleton
51  public class LifecycleDebugLogger {
52      private final Logger logger = LoggerFactory.getLogger(getClass());
53  
54      public void debug(String s) {
55          logger.debug(s);
56      }
57  
58      public void info(String s) {
59          logger.info(s);
60      }
61  
62      public void debugReactorPlan(ProjectBuildList projectBuilds) {
63          if (!logger.isDebugEnabled()) {
64              return;
65          }
66  
67          logger.debug("=== REACTOR BUILD PLAN ================================================");
68  
69          for (Iterator<ProjectSegment> it = projectBuilds.iterator(); it.hasNext(); ) {
70              ProjectSegment projectBuild = it.next();
71  
72              logger.debug("Project: " + projectBuild.getProject().getId());
73              logger.debug("Tasks:   " + projectBuild.getTaskSegment().getTasks());
74              logger.debug("Style:   " + (projectBuild.getTaskSegment().isAggregating() ? "Aggregating" : "Regular"));
75  
76              if (it.hasNext()) {
77                  logger.debug("-----------------------------------------------------------------------");
78              }
79          }
80  
81          logger.debug("=======================================================================");
82      }
83  
84      public void debugProjectPlan(MavenProject currentProject, MavenExecutionPlan executionPlan) {
85          if (!logger.isDebugEnabled()) {
86              return;
87          }
88  
89          logger.debug("=== PROJECT BUILD PLAN ================================================");
90          logger.debug("Project:       " + BuilderCommon.getKey(currentProject));
91  
92          debugDependencyRequirements(executionPlan.getMojoExecutions());
93  
94          logger.debug("Repositories (dependencies): " + currentProject.getRemoteProjectRepositories());
95          logger.debug("Repositories (plugins)     : " + currentProject.getRemotePluginRepositories());
96  
97          for (ExecutionPlanItem mojoExecution : executionPlan) {
98              debugMojoExecution(mojoExecution.getMojoExecution());
99          }
100 
101         logger.debug("=======================================================================");
102     }
103 
104     private void debugMojoExecution(MojoExecution mojoExecution) {
105         String mojoExecId =
106                 mojoExecution.getGroupId() + ':' + mojoExecution.getArtifactId() + ':' + mojoExecution.getVersion()
107                         + ':' + mojoExecution.getGoal() + " (" + mojoExecution.getExecutionId() + ')';
108 
109         Map<String, List<MojoExecution>> forkedExecutions = mojoExecution.getForkedExecutions();
110         if (!forkedExecutions.isEmpty()) {
111             for (Map.Entry<String, List<MojoExecution>> fork : forkedExecutions.entrySet()) {
112                 logger.debug("--- init fork of " + fork.getKey() + " for " + mojoExecId + " ---");
113 
114                 debugDependencyRequirements(fork.getValue());
115 
116                 for (MojoExecution forkedExecution : fork.getValue()) {
117                     debugMojoExecution(forkedExecution);
118                 }
119 
120                 logger.debug("--- exit fork of " + fork.getKey() + " for " + mojoExecId + " ---");
121             }
122         }
123 
124         logger.debug("-----------------------------------------------------------------------");
125         logger.debug("Goal:          " + mojoExecId);
126         logger.debug(
127                 "Style:         " + (mojoExecution.getMojoDescriptor().isAggregator() ? "Aggregating" : "Regular"));
128         logger.debug("Configuration: " + mojoExecution.getConfiguration());
129     }
130 
131     private void debugDependencyRequirements(List<MojoExecution> mojoExecutions) {
132         Set<String> scopesToCollect = new TreeSet<>();
133         Set<String> scopesToResolve = new TreeSet<>();
134 
135         for (MojoExecution mojoExecution : mojoExecutions) {
136             MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
137 
138             String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
139             if (scopeToCollect != null && !scopeToCollect.isEmpty()) {
140                 scopesToCollect.add(scopeToCollect);
141             }
142 
143             String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
144             if (scopeToResolve != null && !scopeToResolve.isEmpty()) {
145                 scopesToResolve.add(scopeToResolve);
146             }
147         }
148 
149         logger.debug("Dependencies (collect): " + scopesToCollect);
150         logger.debug("Dependencies (resolve): " + scopesToResolve);
151     }
152 }