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