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.internal.impl.resolver;
20  
21  import java.util.stream.Collectors;
22  
23  import org.eclipse.aether.RequestTrace;
24  import org.eclipse.aether.collection.CollectRequest;
25  import org.eclipse.aether.collection.CollectStepData;
26  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
27  import org.eclipse.aether.resolution.ArtifactRequest;
28  import org.eclipse.aether.resolution.DependencyRequest;
29  
30  /**
31   * Helper class to manage {@link RequestTrace} for better error logging.
32   */
33  public final class RequestTraceHelper {
34  
35      /**
36       * Method that creates some informational string based on passed in {@link RequestTrace}. The contents of request
37       * trace can literally be anything, but this class tries to cover "most common" cases that are happening in Maven.
38       */
39      public static String interpretTrace(boolean detailed, RequestTrace requestTrace) {
40          while (requestTrace != null) {
41              Object data = requestTrace.getData();
42              if (data instanceof DependencyRequest request) {
43                  return "dependency resolution for " + request;
44              } else if (data instanceof CollectRequest request) {
45                  return "dependency collection for " + request;
46              } else if (data instanceof CollectStepData stepData) {
47                  String msg = "dependency collection step for " + stepData.getContext();
48                  if (detailed) {
49                      msg += ". Path to offending node from root:\n";
50                      msg += stepData.getPath().stream()
51                              .map(n -> " -> " + n.toString())
52                              .collect(Collectors.joining("\n"));
53                      msg += "\n => " + stepData.getNode();
54                  }
55                  return msg;
56              } else if (data instanceof ArtifactDescriptorRequest request) {
57                  return "artifact descriptor request for " + request.getArtifact();
58              } else if (data instanceof ArtifactRequest request) {
59                  return "artifact request for " + request.getArtifact();
60                  // TODO: this class is not reachable here!
61                  // } else if (data instanceof org.apache.maven.model.Plugin plugin) {
62                  //    return "plugin " + plugin.getId();
63              }
64              requestTrace = requestTrace.getParent();
65          }
66  
67          return "n/a";
68      }
69  }