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.lifecycle.MavenExecutionPlan;
023import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
024import org.apache.maven.plugin.MojoExecution;
025import org.apache.maven.plugin.descriptor.MojoDescriptor;
026import org.apache.maven.project.MavenProject;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029import org.codehaus.plexus.logging.Logger;
030import org.codehaus.plexus.util.StringUtils;
031
032import java.util.Iterator;
033import java.util.List;
034import java.util.Map;
035import java.util.Set;
036import java.util.TreeSet;
037
038/**
039 * Logs debug output from the various lifecycle phases.
040 *
041 * @since 3.0
042 * @author Benjamin Bentmann
043 * @author Jason van Zyl
044 * @author Kristian Rosenvold (extracted class only)
045 *         <p/>
046 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
047 */
048@Component( role = LifecycleDebugLogger.class )
049public class LifecycleDebugLogger
050{
051    @Requirement
052    private Logger logger;
053
054
055    public LifecycleDebugLogger()
056    {
057    }
058
059    public LifecycleDebugLogger( Logger logger )
060    {
061        this.logger = logger;
062    }
063
064
065    public void debug( String s )
066    {
067        logger.debug( s );
068    }
069
070    public void info( String s )
071    {
072        logger.info( s );
073    }
074
075    public void debugReactorPlan( ProjectBuildList projectBuilds )
076    {
077        if ( !logger.isDebugEnabled() )
078        {
079            return;
080        }
081
082        logger.debug( "=== REACTOR BUILD PLAN ================================================" );
083
084        for ( Iterator<ProjectSegment> it = projectBuilds.iterator(); it.hasNext(); )
085        {
086            ProjectSegment projectBuild = it.next();
087
088            logger.debug( "Project: " + projectBuild.getProject().getId() );
089            logger.debug( "Tasks:   " + projectBuild.getTaskSegment().getTasks() );
090            logger.debug( "Style:   " + ( projectBuild.getTaskSegment().isAggregating() ? "Aggregating" : "Regular" ) );
091
092            if ( it.hasNext() )
093            {
094                logger.debug( "-----------------------------------------------------------------------" );
095            }
096        }
097
098        logger.debug( "=======================================================================" );
099    }
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}