View Javadoc
1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.lifecycle.MavenExecutionPlan;
23  import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
24  import org.apache.maven.plugin.MojoExecution;
25  import org.apache.maven.plugin.descriptor.MojoDescriptor;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.codehaus.plexus.component.annotations.Requirement;
29  import org.codehaus.plexus.logging.Logger;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.TreeSet;
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  @Component( role = LifecycleDebugLogger.class )
50  public class LifecycleDebugLogger
51  {
52      @Requirement
53      private Logger logger;
54  
55  
56      public LifecycleDebugLogger()
57      {
58      }
59  
60      public LifecycleDebugLogger( Logger logger )
61      {
62          this.logger = logger;
63      }
64  
65  
66      public void debug( String s )
67      {
68          logger.debug( s );
69      }
70  
71      public void info( String s )
72      {
73          logger.info( s );
74      }
75  
76      public void debugReactorPlan( ProjectBuildList projectBuilds )
77      {
78          if ( !logger.isDebugEnabled() )
79          {
80              return;
81          }
82  
83          logger.debug( "=== REACTOR BUILD PLAN ================================================" );
84  
85          for ( Iterator<ProjectSegment> it = projectBuilds.iterator(); it.hasNext(); )
86          {
87              ProjectSegment projectBuild = it.next();
88  
89              logger.debug( "Project: " + projectBuild.getProject().getId() );
90              logger.debug( "Tasks:   " + projectBuild.getTaskSegment().getTasks() );
91              logger.debug( "Style:   " + ( projectBuild.getTaskSegment().isAggregating() ? "Aggregating" : "Regular" ) );
92  
93              if ( it.hasNext() )
94              {
95                  logger.debug( "-----------------------------------------------------------------------" );
96              }
97          }
98  
99          logger.debug( "=======================================================================" );
100     }
101 
102 
103     public void debugProjectPlan( MavenProject currentProject, MavenExecutionPlan executionPlan )
104     {
105         if ( !logger.isDebugEnabled() )
106         {
107             return;
108         }
109 
110         logger.debug( "=== PROJECT BUILD PLAN ================================================" );
111         logger.debug( "Project:       " + BuilderCommon.getKey( currentProject ) );
112 
113         debugDependencyRequirements( executionPlan.getMojoExecutions() );
114 
115         logger.debug( "Repositories (dependencies): " + currentProject.getRemoteProjectRepositories() );
116         logger.debug( "Repositories (plugins)     : " + currentProject.getRemotePluginRepositories() );
117 
118         for ( ExecutionPlanItem mojoExecution : executionPlan )
119         {
120             debugMojoExecution( mojoExecution.getMojoExecution() );
121         }
122 
123         logger.debug( "=======================================================================" );
124     }
125 
126     private void debugMojoExecution( MojoExecution mojoExecution )
127     {
128         String mojoExecId =
129             mojoExecution.getGroupId() + ':' + mojoExecution.getArtifactId() + ':' + mojoExecution.getVersion() + ':'
130                 + mojoExecution.getGoal() + " (" + mojoExecution.getExecutionId() + ')';
131 
132         Map<String, List<MojoExecution>> forkedExecutions = mojoExecution.getForkedExecutions();
133         if ( !forkedExecutions.isEmpty() )
134         {
135             for ( Map.Entry<String, List<MojoExecution>> fork : forkedExecutions.entrySet() )
136             {
137                 logger.debug( "--- init fork of " + fork.getKey() + " for " + mojoExecId + " ---" );
138 
139                 debugDependencyRequirements( fork.getValue() );
140 
141                 for ( MojoExecution forkedExecution : fork.getValue() )
142                 {
143                     debugMojoExecution( forkedExecution );
144                 }
145 
146                 logger.debug( "--- exit fork of " + fork.getKey() + " for " + mojoExecId + " ---" );
147             }
148         }
149 
150         logger.debug( "-----------------------------------------------------------------------" );
151         logger.debug( "Goal:          " + mojoExecId );
152         logger.debug(
153             "Style:         " + ( mojoExecution.getMojoDescriptor().isAggregator() ? "Aggregating" : "Regular" ) );
154         logger.debug( "Configuration: " + mojoExecution.getConfiguration() );
155     }
156 
157     private void debugDependencyRequirements( List<MojoExecution> mojoExecutions )
158     {
159         Set<String> scopesToCollect = new TreeSet<>();
160         Set<String> scopesToResolve = new TreeSet<>();
161 
162         for ( MojoExecution mojoExecution : mojoExecutions )
163         {
164             MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
165 
166             String scopeToCollect = mojoDescriptor.getDependencyCollectionRequired();
167             if ( StringUtils.isNotEmpty( scopeToCollect ) )
168             {
169                 scopesToCollect.add( scopeToCollect );
170             }
171 
172             String scopeToResolve = mojoDescriptor.getDependencyResolutionRequired();
173             if ( StringUtils.isNotEmpty( scopeToResolve ) )
174             {
175                 scopesToResolve.add( scopeToResolve );
176             }
177         }
178 
179         logger.debug( "Dependencies (collect): " + scopesToCollect );
180         logger.debug( "Dependencies (resolve): " + scopesToResolve );
181     }
182 
183 }