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;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Objects;
24  import java.util.Optional;
25  import org.apache.maven.api.Dependency;
26  import org.apache.maven.api.Node;
27  import org.apache.maven.api.RemoteRepository;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.eclipse.aether.graph.DependencyNode;
30  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
31  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
32  
33  public class DefaultNode extends AbstractNode {
34  
35      protected final @Nonnull AbstractSession session;
36      protected final @Nonnull org.eclipse.aether.graph.DependencyNode node;
37      protected final boolean verbose;
38  
39      public DefaultNode(
40              @Nonnull AbstractSession session, @Nonnull org.eclipse.aether.graph.DependencyNode node, boolean verbose) {
41          this.session = session;
42          this.node = node;
43          this.verbose = verbose;
44      }
45  
46      @Override
47      DependencyNode getDependencyNode() {
48          return node;
49      }
50  
51      @Override
52      public Dependency getDependency() {
53          return node.getDependency() != null ? session.getDependency(node.getDependency()) : null;
54      }
55  
56      @Override
57      public List<Node> getChildren() {
58          return new MappedList<>(node.getChildren(), n -> session.getNode(n, verbose));
59      }
60  
61      @Override
62      public List<RemoteRepository> getRemoteRepositories() {
63          return new MappedList<>(node.getRepositories(), session::getRemoteRepository);
64      }
65  
66      @Override
67      public Optional<RemoteRepository> getRepository() {
68          // TODO
69          throw new UnsupportedOperationException("Not implemented yet");
70      }
71  
72      @Override
73      public String asString() {
74          String nodeString = super.asString();
75  
76          if (!verbose) {
77              return nodeString;
78          }
79  
80          org.eclipse.aether.graph.DependencyNode node = getDependencyNode();
81  
82          List<String> details = new ArrayList<>();
83  
84          org.eclipse.aether.graph.DependencyNode winner =
85                  (org.eclipse.aether.graph.DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER);
86          String winnerVersion = winner != null ? winner.getArtifact().getBaseVersion() : null;
87          boolean included = (winnerVersion == null);
88  
89          String preManagedVersion = DependencyManagerUtils.getPremanagedVersion(node);
90          if (preManagedVersion != null) {
91              details.add("version managed from " + preManagedVersion);
92          }
93  
94          String preManagedScope = DependencyManagerUtils.getPremanagedScope(node);
95          if (preManagedScope != null) {
96              details.add("scope managed from " + preManagedScope);
97          }
98  
99          String originalScope = (String) node.getData().get(ConflictResolver.NODE_DATA_ORIGINAL_SCOPE);
100         if (originalScope != null && !originalScope.equals(node.getDependency().getScope())) {
101             details.add("scope updated from " + originalScope);
102         }
103 
104         if (!included) {
105             if (Objects.equals(winnerVersion, node.getArtifact().getVersion())) {
106                 details.add("omitted for duplicate");
107             } else {
108                 details.add("omitted for conflict with " + winnerVersion);
109             }
110         }
111 
112         StringBuilder buffer = new StringBuilder();
113         if (included) {
114             buffer.append(nodeString);
115             if (!details.isEmpty()) {
116                 buffer.append(" (");
117                 join(buffer, details, "; ");
118                 buffer.append(")");
119             }
120         } else {
121             buffer.append("(");
122             buffer.append(nodeString);
123             if (!details.isEmpty()) {
124                 buffer.append(" - ");
125                 join(buffer, details, "; ");
126             }
127             buffer.append(")");
128         }
129         return buffer.toString();
130     }
131 
132     private static void join(StringBuilder buffer, List<String> details, String separator) {
133         boolean first = true;
134         for (String detail : details) {
135             if (first) {
136                 first = false;
137             } else {
138                 buffer.append(separator);
139             }
140             buffer.append(detail);
141         }
142     }
143 }