View Javadoc
1   package org.apache.maven.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.Optional;
26  
27  import org.apache.maven.api.Dependency;
28  import org.apache.maven.api.Node;
29  import org.apache.maven.api.RemoteRepository;
30  import org.apache.maven.api.annotations.Nonnull;
31  import org.eclipse.aether.graph.DependencyNode;
32  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
33  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
34  
35  public class DefaultNode extends AbstractNode
36  {
37  
38      protected final @Nonnull AbstractSession session;
39      protected final @Nonnull org.eclipse.aether.graph.DependencyNode node;
40      protected final boolean verbose;
41  
42      public DefaultNode( @Nonnull AbstractSession session,
43                          @Nonnull org.eclipse.aether.graph.DependencyNode node,
44                          boolean verbose )
45      {
46          this.session = session;
47          this.node = node;
48          this.verbose = verbose;
49      }
50  
51      @Override
52      DependencyNode getDependencyNode()
53      {
54          return node;
55      }
56  
57      @Override
58      public Dependency getDependency()
59      {
60          return node.getDependency() != null ? session.getDependency( node.getDependency() ) : null;
61      }
62  
63      @Override
64      public List<Node> getChildren()
65      {
66          return new MappedList<>( node.getChildren(), n -> session.getNode( n, verbose ) );
67      }
68  
69      @Override
70      public List<RemoteRepository> getRemoteRepositories()
71      {
72          return new MappedList<>( node.getRepositories(), session::getRemoteRepository );
73      }
74  
75      @Override
76      public Optional<RemoteRepository> getRepository()
77      {
78          // TODO
79          throw new UnsupportedOperationException( "Not implemented yet" );
80      }
81  
82      @Override
83      public String asString()
84      {
85          String nodeString = super.asString();
86  
87          if ( !verbose )
88          {
89              return nodeString;
90          }
91  
92          org.eclipse.aether.graph.DependencyNode node = getDependencyNode();
93  
94          List<String> details = new ArrayList<>();
95  
96          org.eclipse.aether.graph.DependencyNode winner =
97                  (org.eclipse.aether.graph.DependencyNode) node.getData().get( ConflictResolver.NODE_DATA_WINNER );
98          String winnerVersion = winner != null ? winner.getArtifact().getBaseVersion() : null;
99          boolean included = ( winnerVersion == null );
100 
101         String preManagedVersion = DependencyManagerUtils.getPremanagedVersion( node );
102         if ( preManagedVersion != null )
103         {
104             details.add( "version managed from " + preManagedVersion );
105         }
106 
107         String preManagedScope = DependencyManagerUtils.getPremanagedScope( node );
108         if ( preManagedScope != null )
109         {
110             details.add( "scope managed from " + preManagedScope );
111         }
112 
113         String originalScope = (String) node.getData().get( ConflictResolver.NODE_DATA_ORIGINAL_SCOPE );
114         if ( originalScope != null && !originalScope.equals( node.getDependency().getScope() ) )
115         {
116             details.add( "scope updated from " + originalScope );
117         }
118 
119         if ( !included )
120         {
121             if ( Objects.equals( winnerVersion, node.getArtifact().getVersion() ) )
122             {
123                 details.add( "omitted for duplicate" );
124             }
125             else
126             {
127                 details.add( "omitted for conflict with " + winnerVersion );
128             }
129         }
130 
131         StringBuilder buffer = new StringBuilder();
132         if ( included )
133         {
134             buffer.append( nodeString );
135             if ( !details.isEmpty() )
136             {
137                 buffer.append( " (" );
138                 join( buffer, details, "; " );
139                 buffer.append( ")" );
140             }
141         }
142         else
143         {
144             buffer.append( "(" );
145             buffer.append( nodeString );
146             if ( !details.isEmpty() )
147             {
148                 buffer.append( " - " );
149                 join( buffer, details, "; " );
150             }
151             buffer.append( ")" );
152         }
153         return buffer.toString();
154     }
155 
156     private static void join( StringBuilder buffer, List<String> details, String separator )
157     {
158         boolean first = true;
159         for ( String detail : details )
160         {
161             if ( first )
162             {
163                 first = false;
164             }
165             else
166             {
167                 buffer.append( separator );
168             }
169             buffer.append( detail );
170         }
171     }
172 
173 }