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