View Javadoc
1   package org.apache.maven.resolver.examples.util;
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.io.PrintStream;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.graph.Dependency;
29  import org.eclipse.aether.graph.DependencyNode;
30  import org.eclipse.aether.graph.DependencyVisitor;
31  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
32  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
33  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
34  
35  /**
36   * A dependency visitor that dumps the graph to the console.
37   */
38  public class ConsoleDependencyGraphDumper
39      implements DependencyVisitor
40  {
41  
42      private PrintStream out;
43  
44      private List<ChildInfo> childInfos = new ArrayList<>();
45  
46      public ConsoleDependencyGraphDumper()
47      {
48          this( null );
49      }
50  
51      public ConsoleDependencyGraphDumper( PrintStream out )
52      {
53          this.out = ( out != null ) ? out : System.out;
54      }
55  
56      public boolean visitEnter( DependencyNode node )
57      {
58          out.println( formatIndentation() + formatNode( node ) );
59          childInfos.add( new ChildInfo( node.getChildren().size() ) );
60          return true;
61      }
62  
63      private String formatIndentation()
64      {
65          StringBuilder buffer = new StringBuilder( 128 );
66          for ( Iterator<ChildInfo> it = childInfos.iterator(); it.hasNext(); )
67          {
68              buffer.append( it.next().formatIndentation( !it.hasNext() ) );
69          }
70          return buffer.toString();
71      }
72  
73      private String formatNode( DependencyNode node )
74      {
75          StringBuilder buffer = new StringBuilder( 128 );
76          Artifact a = node.getArtifact();
77          Dependency d = node.getDependency();
78          buffer.append( a );
79          if ( d != null && d.getScope().length() > 0 )
80          {
81              buffer.append( " [" ).append( d.getScope() );
82              if ( d.isOptional() )
83              {
84                  buffer.append( ", optional" );
85              }
86              buffer.append( "]" );
87          }
88          String premanaged = DependencyManagerUtils.getPremanagedVersion( node );
89          if ( premanaged != null && !premanaged.equals( a.getBaseVersion() ) )
90          {
91              buffer.append( " (version managed from " ).append( premanaged ).append( ")" );
92          }
93  
94          premanaged = DependencyManagerUtils.getPremanagedScope( node );
95          if ( premanaged != null && !premanaged.equals( d.getScope() ) )
96          {
97              buffer.append( " (scope managed from " ).append( premanaged ).append( ")" );
98          }
99          DependencyNode winner = (DependencyNode) node.getData().get( ConflictResolver.NODE_DATA_WINNER );
100         if ( winner != null && !ArtifactIdUtils.equalsId( a, winner.getArtifact() ) )
101         {
102             Artifact w = winner.getArtifact();
103             buffer.append( " (conflicts with " );
104             if ( ArtifactIdUtils.toVersionlessId( a ).equals( ArtifactIdUtils.toVersionlessId( w ) ) )
105             {
106                 buffer.append( w.getVersion() );
107             }
108             else
109             {
110                 buffer.append( w );
111             }
112             buffer.append( ")" );
113         }
114         return buffer.toString();
115     }
116 
117     public boolean visitLeave( DependencyNode node )
118     {
119         if ( !childInfos.isEmpty() )
120         {
121             childInfos.remove( childInfos.size() - 1 );
122         }
123         if ( !childInfos.isEmpty() )
124         {
125             childInfos.get( childInfos.size() - 1 ).index++;
126         }
127         return true;
128     }
129 
130     private static class ChildInfo
131     {
132 
133         final int count;
134 
135         int index;
136 
137         ChildInfo( int count )
138         {
139             this.count = count;
140         }
141 
142         public String formatIndentation( boolean end )
143         {
144             boolean last = index + 1 >= count;
145             if ( end )
146             {
147                 return last ? "\\- " : "+- ";
148             }
149             return last ? "   " : "|  ";
150         }
151 
152     }
153 
154 }