001package org.apache.maven.lifecycle.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.execution.MavenSession;
023import org.apache.maven.execution.ProjectDependencyGraph;
024import org.apache.maven.lifecycle.MavenExecutionPlan;
025import org.apache.maven.plugin.MojoExecution;
026import org.apache.maven.plugin.descriptor.MojoDescriptor;
027import org.apache.maven.project.MavenProject;
028import org.codehaus.plexus.component.annotations.Component;
029import org.codehaus.plexus.component.annotations.Requirement;
030import org.codehaus.plexus.logging.Logger;
031import org.codehaus.plexus.util.StringUtils;
032
033import java.util.Iterator;
034import java.util.List;
035import java.util.Map;
036import java.util.Set;
037import java.util.TreeSet;
038
039/**
040 * Logs debug output from the various lifecycle phases.
041 * 
042 * @since 3.0
043 * @author Benjamin Bentmann
044 * @author Jason van Zyl
045 * @author Kristian Rosenvold (extracted class only)
046 *         <p/>
047 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
048 */
049@Component( role = LifecycleDebugLogger.class )
050public class LifecycleDebugLogger
051{
052    @Requirement
053    private Logger logger;
054
055
056    public LifecycleDebugLogger()
057    {
058    }
059
060    public LifecycleDebugLogger( Logger logger )
061    {
062        this.logger = logger;
063    }
064
065
066    public void debug( String s )
067    {
068        logger.debug( s );
069    }
070
071    public void info( String s )
072    {
073        logger.info( s );
074    }
075
076    public void debugReactorPlan( ProjectBuildList projectBuilds )
077    {
078        if ( !logger.isDebugEnabled() )
079        {
080            return;
081        }
082
083        logger.debug( "=== REACTOR BUILD PLAN ================================================" );
084
085        for ( Iterator<ProjectSegment> it = projectBuilds.iterator(); it.hasNext(); )
086        {
087            ProjectSegment projectBuild = it.next();
088
089            logger.debug( "Project: " + projectBuild.getProject().getId() );
090            logger.debug( "Tasks:   " + projectBuild.getTaskSegment().getTasks() );
091            logger.debug( "Style:   " + ( projectBuild.getTaskSegment().isAggregating() ? "Aggregating" : "Regular" ) );
092
093            if ( it.hasNext() )
094            {
095                logger.debug( "-----------------------------------------------------------------------" );
096            }
097        }
098
099        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<String>();
160        Set<String> scopesToResolve = new TreeSet<String>();
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    public void logWeavePlan( MavenSession session )
184    {
185        if ( !logger.isInfoEnabled() )
186        {
187            return;
188        }
189
190        final ProjectDependencyGraph dependencyGraph = session.getProjectDependencyGraph();
191        logger.info( "=== WEAVE CONCURRENCY BUILD PLAN ======================================" );
192        for ( MavenProject mavenProject : dependencyGraph.getSortedProjects() )
193        {
194
195            StringBuilder item = new StringBuilder();
196            item.append( "Project: " );
197            item.append( mavenProject.getArtifactId() );
198            final List<MavenProject> upstreamProjects = dependencyGraph.getUpstreamProjects( mavenProject, false );
199            if ( upstreamProjects.size() > 0 )
200            {
201                item.append( " ( " );
202                for ( Iterator<MavenProject> it = upstreamProjects.iterator(); it.hasNext(); )
203                {
204                    final MavenProject kid = it.next();
205                    item.append( kid.getArtifactId() );
206                    if ( it.hasNext() )
207                    {
208                        item.append( ", " );
209                    }
210                }
211                item.append( ")" );
212            }
213            logger.info( item.toString() );
214
215        }
216        logger.info( "=======================================================================" );
217    }
218}