View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.resolver;
20  
21  import java.util.HashSet;
22  import java.util.Objects;
23  import java.util.Set;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.codehaus.plexus.logging.Logger;
27  
28  /**
29   * Send resolution events to the debug log.
30   *
31   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
32   */
33  public class DebugResolutionListener implements ResolutionListener, ResolutionListenerForDepMgmt {
34      private Logger logger;
35  
36      private String indent = "";
37  
38      private static Set<Artifact> ignoredArtifacts = new HashSet<>();
39  
40      public DebugResolutionListener(Logger logger) {
41          this.logger = logger;
42      }
43  
44      public void testArtifact(Artifact node) {}
45  
46      public void startProcessChildren(Artifact artifact) {
47          indent += "  ";
48      }
49  
50      public void endProcessChildren(Artifact artifact) {
51          indent = indent.substring(2);
52      }
53  
54      public void includeArtifact(Artifact artifact) {
55          logger.debug(indent + artifact + " (selected for " + artifact.getScope() + ")");
56      }
57  
58      public void omitForNearer(Artifact omitted, Artifact kept) {
59          String omittedVersion = omitted.getVersion();
60          String keptVersion = kept.getVersion();
61  
62          if (!Objects.equals(omittedVersion, keptVersion)) {
63              logger.debug(indent + omitted + " (removed - nearer found: " + keptVersion + ")");
64          }
65      }
66  
67      public void omitForCycle(Artifact omitted) {
68          logger.debug(indent + omitted + " (removed - causes a cycle in the graph)");
69      }
70  
71      public void updateScopeCurrentPom(Artifact artifact, String ignoredScope) {
72          logger.debug(indent + artifact + " (not setting artifactScope to: " + ignoredScope + "; local artifactScope "
73                  + artifact.getScope() + " wins)");
74  
75          // TODO better way than static? this might hide messages in a reactor
76          if (!ignoredArtifacts.contains(artifact)) {
77              logger.warn("\n\tArtifact " + artifact + " retains local artifactScope '" + artifact.getScope()
78                      + "' overriding broader artifactScope '" + ignoredScope + "'\n"
79                      + "\tgiven by a dependency. If this is not intended, modify or remove the local artifactScope.\n");
80              ignoredArtifacts.add(artifact);
81          }
82      }
83  
84      public void updateScope(Artifact artifact, String scope) {
85          logger.debug(indent + artifact + " (setting artifactScope to: " + scope + ")");
86      }
87  
88      public void selectVersionFromRange(Artifact artifact) {
89          logger.debug(indent + artifact + " (setting version to: " + artifact.getVersion() + " from range: "
90                  + artifact.getVersionRange() + ")");
91      }
92  
93      public void restrictRange(Artifact artifact, Artifact replacement, VersionRange newRange) {
94          logger.debug(indent + artifact + " (range restricted from: " + artifact.getVersionRange() + " and: "
95                  + replacement.getVersionRange() + " to: " + newRange + " )");
96      }
97  
98      /**
99       * The logic used here used to be a copy of the logic used in the DefaultArtifactCollector, and this method was
100      * called right before the actual version/artifactScope changes were done. However, a different set of conditionals
101      * (and more information) is needed to be able to determine when and if the version and/or artifactScope changes.
102      * See the two added methods, manageArtifactVersion and manageArtifactScope.
103      */
104     public void manageArtifact(Artifact artifact, Artifact replacement) {
105         String msg = indent + artifact;
106         msg += " (";
107         if (replacement.getVersion() != null) {
108             msg += "applying version: " + replacement.getVersion() + ";";
109         }
110         if (replacement.getScope() != null) {
111             msg += "applying artifactScope: " + replacement.getScope();
112         }
113         msg += ")";
114         logger.debug(msg);
115     }
116 
117     public void manageArtifactVersion(Artifact artifact, Artifact replacement) {
118         // only show msg if a change is actually taking place
119         if (!replacement.getVersion().equals(artifact.getVersion())) {
120             String msg = indent + artifact + " (applying version: " + replacement.getVersion() + ")";
121             logger.debug(msg);
122         }
123     }
124 
125     public void manageArtifactScope(Artifact artifact, Artifact replacement) {
126         // only show msg if a change is actually taking place
127         if (!replacement.getScope().equals(artifact.getScope())) {
128             String msg = indent + artifact + " (applying artifactScope: " + replacement.getScope() + ")";
129             logger.debug(msg);
130         }
131     }
132 
133     public void manageArtifactSystemPath(Artifact artifact, Artifact replacement) {
134         // only show msg if a change is actually taking place
135         if (!replacement.getScope().equals(artifact.getScope())) {
136             String msg = indent + artifact + " (applying system path: " + replacement.getFile() + ")";
137             logger.debug(msg);
138         }
139     }
140 }