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