001package org.apache.maven.artifact.resolver;
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 java.util.HashSet;
023import java.util.Set;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.artifact.versioning.VersionRange;
027import org.codehaus.plexus.logging.Logger;
028
029/**
030 * Send resolution events to the debug log.
031 *
032 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
033 */
034public class DebugResolutionListener
035    implements ResolutionListener, ResolutionListenerForDepMgmt
036{
037    private Logger logger;
038
039    private String indent = "";
040
041    private static Set<Artifact> ignoredArtifacts = new HashSet<Artifact>();
042
043    public DebugResolutionListener( Logger logger )
044    {
045        this.logger = logger;
046    }
047
048    public void testArtifact( Artifact node )
049    {
050    }
051
052    public void startProcessChildren( Artifact artifact )
053    {
054        indent += "  ";
055    }
056
057    public void endProcessChildren( Artifact artifact )
058    {
059        indent = indent.substring( 2 );
060    }
061
062    public void includeArtifact( Artifact artifact )
063    {
064        logger.debug( indent + artifact + " (selected for " + artifact.getScope() + ")" );
065    }
066
067    public void omitForNearer( Artifact omitted, Artifact kept )
068    {
069        String omittedVersion = omitted.getVersion();
070        String keptVersion = kept.getVersion();
071
072        if ( omittedVersion != null ? !omittedVersion.equals( keptVersion ) : keptVersion != null )
073        {
074            logger.debug( indent + omitted + " (removed - nearer found: " + kept.getVersion() + ")" );
075        }
076    }
077
078    public void omitForCycle( Artifact omitted )
079    {
080        logger.debug( indent + omitted + " (removed - causes a cycle in the graph)" );
081    }
082
083    public void updateScopeCurrentPom( Artifact artifact, String ignoredScope )
084    {
085        logger.debug( indent + artifact + " (not setting artifactScope to: " + ignoredScope + "; local artifactScope "
086            + artifact.getScope() + " wins)" );
087
088        // TODO: better way than static? this might hide messages in a reactor
089        if ( !ignoredArtifacts.contains( artifact ) )
090        {
091            logger.warn( "\n\tArtifact " + artifact + " retains local artifactScope '" + artifact.getScope()
092                + "' overriding broader artifactScope '" + ignoredScope + "'\n"
093                + "\tgiven by a dependency. If this is not intended, modify or remove the local artifactScope.\n" );
094            ignoredArtifacts.add( artifact );
095        }
096    }
097
098    public void updateScope( Artifact artifact, String scope )
099    {
100        logger.debug( indent + artifact + " (setting artifactScope to: " + scope + ")" );
101    }
102
103    public void selectVersionFromRange( Artifact artifact )
104    {
105        logger.debug( indent + artifact + " (setting version to: " + artifact.getVersion() + " from range: "
106            + artifact.getVersionRange() + ")" );
107    }
108
109    public void restrictRange( Artifact artifact, Artifact replacement, VersionRange newRange )
110    {
111        logger.debug( indent + artifact + " (range restricted from: " + artifact.getVersionRange() + " and: "
112            + replacement.getVersionRange() + " to: " + newRange + " )" );
113    }
114
115    /**
116     * The logic used here used to be a copy of the logic used in the DefaultArtifactCollector, and this method was
117     * called right before the actual version/artifactScope changes were done. However, a different set of conditionals (and
118     * more information) is needed to be able to determine when and if the version and/or artifactScope changes. See the two
119     * added methods, manageArtifactVersion and manageArtifactScope.
120     */
121    public void manageArtifact( Artifact artifact, Artifact replacement )
122    {
123        String msg = indent + artifact;
124        msg += " (";
125        if ( replacement.getVersion() != null )
126        {
127            msg += "applying version: " + replacement.getVersion() + ";";
128        }
129        if ( replacement.getScope() != null )
130        {
131            msg += "applying artifactScope: " + replacement.getScope();
132        }
133        msg += ")";
134        logger.debug( msg );
135    }
136
137    public void manageArtifactVersion( Artifact artifact, Artifact replacement )
138    {
139        // only show msg if a change is actually taking place
140        if ( !replacement.getVersion().equals( artifact.getVersion() ) )
141        {
142            String msg = indent + artifact + " (applying version: " + replacement.getVersion() + ")";
143            logger.debug( msg );
144        }
145    }
146
147    public void manageArtifactScope( Artifact artifact, Artifact replacement )
148    {
149        // only show msg if a change is actually taking place
150        if ( !replacement.getScope().equals( artifact.getScope() ) )
151        {
152            String msg = indent + artifact + " (applying artifactScope: " + replacement.getScope() + ")";
153            logger.debug( msg );
154        }
155    }
156
157    public void manageArtifactSystemPath( Artifact artifact, Artifact replacement )
158    {
159        // only show msg if a change is actually taking place
160        if ( !replacement.getScope().equals( artifact.getScope() ) )
161        {
162            String msg = indent + artifact + " (applying system path: " + replacement.getFile() + ")";
163            logger.debug( msg );
164        }
165    }
166
167}