View Javadoc
1   package org.apache.maven.artifact.resolver;
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.HashSet;
23  import java.util.Set;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  import org.codehaus.plexus.logging.Logger;
28  
29  /**
30   * Send resolution events to the debug log.
31   *
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   */
34  public class DebugResolutionListener
35      implements ResolutionListener, ResolutionListenerForDepMgmt
36  {
37      private Logger logger;
38  
39      private String indent = "";
40  
41      private static Set<Artifact> ignoredArtifacts = new HashSet<Artifact>();
42  
43      public DebugResolutionListener( Logger logger )
44      {
45          this.logger = logger;
46      }
47  
48      public void testArtifact( Artifact node )
49      {
50      }
51  
52      public void startProcessChildren( Artifact artifact )
53      {
54          indent += "  ";
55      }
56  
57      public void endProcessChildren( Artifact artifact )
58      {
59          indent = indent.substring( 2 );
60      }
61  
62      public void includeArtifact( Artifact artifact )
63      {
64          logger.debug( indent + artifact + " (selected for " + artifact.getScope() + ")" );
65      }
66  
67      public void omitForNearer( Artifact omitted, Artifact kept )
68      {
69          String omittedVersion = omitted.getVersion();
70          String keptVersion = kept.getVersion();
71  
72          if ( omittedVersion != null ? !omittedVersion.equals( keptVersion ) : keptVersion != null )
73          {
74              logger.debug( indent + omitted + " (removed - nearer found: " + kept.getVersion() + ")" );
75          }
76      }
77  
78      public void omitForCycle( Artifact omitted )
79      {
80          logger.debug( indent + omitted + " (removed - causes a cycle in the graph)" );
81      }
82  
83      public void updateScopeCurrentPom( Artifact artifact, String ignoredScope )
84      {
85          logger.debug( indent + artifact + " (not setting artifactScope to: " + ignoredScope + "; local artifactScope "
86              + artifact.getScope() + " wins)" );
87  
88          // TODO: better way than static? this might hide messages in a reactor
89          if ( !ignoredArtifacts.contains( artifact ) )
90          {
91              logger.warn( "\n\tArtifact " + artifact + " retains local artifactScope '" + artifact.getScope()
92                  + "' overriding broader artifactScope '" + ignoredScope + "'\n"
93                  + "\tgiven by a dependency. If this is not intended, modify or remove the local artifactScope.\n" );
94              ignoredArtifacts.add( artifact );
95          }
96      }
97  
98      public void updateScope( Artifact artifact, String scope )
99      {
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 }