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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  import org.codehaus.plexus.logging.Logger;
25  
26  import java.util.Set;
27  import java.util.HashSet;
28  
29  /**
30   * Send resolution events to the debug log.
31   *
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   * @version $Id: DebugResolutionListener.java 640549 2008-03-24 20:05:11Z bentmann $
34   */
35  public class DebugResolutionListener
36      implements ResolutionListener, ResolutionListenerForDepMgmt
37  {
38      private Logger logger;
39  
40      private String indent = "";
41  
42      private static Set ignoredArtifacts = new HashSet();
43      
44      public DebugResolutionListener( Logger logger )
45      {
46          this.logger = logger;
47      }
48  
49      public void testArtifact( Artifact node )
50      {
51      }
52  
53      public void startProcessChildren( Artifact artifact )
54      {
55          indent += "  ";
56      }
57  
58      public void endProcessChildren( Artifact artifact )
59      {
60          indent = indent.substring( 2 );
61      }
62  
63      public void includeArtifact( Artifact artifact )
64      {
65          logger.debug( indent + artifact + " (selected for " + artifact.getScope() + ")" );
66      }
67  
68      public void omitForNearer( Artifact omitted, Artifact kept )
69      {
70          String omittedVersion = omitted.getVersion();
71          String keptVersion = kept.getVersion();
72  
73          if ( omittedVersion != null ? !omittedVersion.equals( keptVersion ) : keptVersion != null )
74          {
75              logger.debug( indent + omitted + " (removed - nearer found: " + kept.getVersion() + ")" );
76          }
77      }
78  
79      public void omitForCycle( Artifact omitted )
80      {
81          logger.debug( indent + omitted + " (removed - causes a cycle in the graph)" );
82      }
83  
84      public void updateScopeCurrentPom( Artifact artifact, String ignoredScope )
85      {
86          logger.debug( indent + artifact + " (not setting scope to: " + ignoredScope + "; local scope " + artifact.getScope() +
87              " wins)" );
88  
89          // TODO: better way than static? this might hide messages in a reactor
90          if ( !ignoredArtifacts.contains( artifact ) )
91          {
92              logger.warn( "\n\tArtifact " + artifact + " retains local scope '" + artifact.getScope() +
93                  "' overriding broader scope '" + ignoredScope + "'\n" +
94                  "\tgiven by a dependency. If this is not intended, modify or remove the local scope.\n" );
95              ignoredArtifacts.add( artifact );
96          }
97      }
98  
99      public void updateScope( Artifact artifact, String scope )
100     {
101         logger.debug( indent + artifact + " (setting scope to: " + scope + ")" );
102     }
103 
104     public void selectVersionFromRange( Artifact artifact )
105     {
106         logger.debug( indent + artifact + " (setting version to: " + artifact.getVersion() + " from range: " +
107             artifact.getVersionRange() + ")" );
108     }
109 
110     public void restrictRange( Artifact artifact, Artifact replacement, VersionRange newRange )
111     {
112         logger.debug( indent + artifact + " (range restricted from: " + artifact.getVersionRange() + " and: " +
113             replacement.getVersionRange() + " to: " + newRange + " )" );
114     }
115 
116     /**
117      * The logic used here used to be a copy of the logic used in the DefaultArtifactCollector, and this method was
118      * called right before the actual version/scope changes were done. However, a different set of conditionals (and
119      * more information) is needed to be able to determine when and if the version and/or scope changes. See the two
120      * added methods, manageArtifactVersion and manageArtifactScope.
121      */
122     public void manageArtifact( Artifact artifact, Artifact replacement )
123     {
124         String msg = indent + artifact;
125         msg += " (";
126         if ( replacement.getVersion() != null )
127         {
128             msg += "applying version: " + replacement.getVersion() + ";";
129         }
130         if ( replacement.getScope() != null )
131         {
132             msg += "applying scope: " + replacement.getScope();
133         }
134         msg += ")";
135         logger.debug( msg );
136     }
137 
138     public void manageArtifactVersion( Artifact artifact, Artifact replacement )
139     {
140         // only show msg if a change is actually taking place
141         if ( !replacement.getVersion().equals( artifact.getVersion() ) )
142         {
143             String msg = indent + artifact + " (applying version: " + replacement.getVersion() + ")";
144             logger.debug( msg );
145         }
146     }
147 
148     public void manageArtifactScope( Artifact artifact, Artifact replacement )
149     {
150         // only show msg if a change is actually taking place
151         if ( !replacement.getScope().equals( artifact.getScope() ) )
152         {
153             String msg = indent + artifact + " (applying scope: " + replacement.getScope() + ")";
154             logger.debug( msg );
155         }
156     }
157 
158 }