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