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