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