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