View Javadoc
1   package org.apache.maven.plugins.dependency.tree;
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 org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.graph.DependencyNode;
24  
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  /**
31   * Parses dependency graph and outputs in text format for end user to review.
32   */
33  class VerboseGraphTextSerializer extends AbstractVerboseGraphSerializer
34  {
35      @Override
36      public String serialize( DependencyNode root )
37      {
38          Set<String> coordinateStrings = new HashSet<>();
39          Map<String, String> coordinateVersionMap = new HashMap<>();
40          Map<DependencyNode, String> nodeErrors = getNodeConflictMessagesBfs( root, coordinateStrings,
41                  coordinateVersionMap );
42  
43          StringBuilder builder = new StringBuilder();
44  
45          // deal with root first
46          Artifact rootArtifact = root.getArtifact();
47          builder.append( rootArtifact.getGroupId() ).append( ":" ).append( rootArtifact.getArtifactId() ).append(
48                  ":" ).append( rootArtifact.getExtension() ).append( ":" ).append( rootArtifact.getVersion() );
49  
50          if ( root.getData().containsKey( "ContainsModule" ) )
51          {
52              builder.append( " WARNING: this tree contains a submodule. Once it reaches the submodule will print "
53                      + "in nonVerbose fashion, to see the actual submodule verbose output refer to "
54                      + "the rest of the output" );
55          }
56  
57          builder.append( "\n" );
58  
59          for ( int i = 0; i < root.getChildren().size(); i++ )
60          {
61              if ( i == root.getChildren().size() - 1 )
62              {
63                  dfsPrint( root.getChildren().get( i ), LINE_START_LAST_CHILD, builder, nodeErrors );
64              }
65              else
66              {
67                  dfsPrint( root.getChildren().get( i ), LINE_START_CHILD, builder, nodeErrors );
68              }
69          }
70          return builder.toString();
71      }
72  
73      private void dfsPrint( DependencyNode node, String start, StringBuilder builder,
74                             Map<DependencyNode, String> nodeErrors )
75      {
76          builder.append( start );
77          if ( node.getArtifact() == null )
78          {
79              // Should never hit this condition with a proper graph sent in
80              builder.append( "Null Artifact Node" ).append( "\n" );
81              callDfsPrint( node, start, builder, nodeErrors );
82          }
83  
84          String coordString = "";
85          boolean messageAdded = false;
86  
87          if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_VERSION ) )
88          {
89              coordString += " - version managed from "
90                      + node.getArtifact().getProperties().get( PRE_MANAGED_VERSION );
91              messageAdded = true;
92          }
93          if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_SCOPE ) )
94          {
95              if ( messageAdded )
96              {
97                  coordString += "; ";
98              }
99              else
100             {
101                 coordString += " - ";
102                 messageAdded = true;
103             }
104             coordString +=
105                     "scope managed from " + node.getArtifact().getProperties().get( PRE_MANAGED_SCOPE );
106         }
107 
108         coordString = getDependencyCoordinate( node ) + coordString;
109 
110         if ( node.getArtifact().getProperties().containsKey( "Cycle" ) )
111         {
112             if ( messageAdded )
113             {
114                 coordString += "; ";
115             }
116             else
117             {
118                 coordString += " - ";
119             }
120             coordString += "omitted for cycle";
121             builder.append( "(" ).append( coordString ).append( ")" ).append( "\n" );
122         }
123         else if ( nodeErrors.get( node ) != null )
124         {
125             builder.append( "(" );
126             if ( messageAdded )
127             {
128                 builder.append( coordString ).append( "; " ).append( nodeErrors.get( node ) );
129             }
130             else
131             {
132                 builder.append( coordString ).append( " - " ).append( nodeErrors.get( node ) );
133             }
134             builder.append( ")" );
135             builder.append( "\n" );
136         }
137         else
138         {
139             builder.append( coordString ).append( "\n" );
140             callDfsPrint( node, start, builder, nodeErrors );
141         }
142     }
143 
144     private void callDfsPrint( DependencyNode node, String start, StringBuilder builder,
145                                Map<DependencyNode, String> nodeErrors )
146     {
147         for ( int i = 0; i < node.getChildren().size(); i++ )
148         {
149             if ( start.endsWith( LINE_START_CHILD ) )
150             {
151                 start = start.replace( LINE_START_CHILD, "|  " );
152             }
153             else if ( start.endsWith( LINE_START_LAST_CHILD ) )
154             {
155                 start = start.replace( LINE_START_LAST_CHILD, "   " );
156             }
157 
158             if ( i == node.getChildren().size() - 1 )
159             {
160                 dfsPrint( node.getChildren().get( i ), start.concat( LINE_START_LAST_CHILD ), builder, nodeErrors );
161             }
162             else
163             {
164                 dfsPrint( node.getChildren().get( i ), start.concat( LINE_START_CHILD ), builder, nodeErrors );
165             }
166         }
167     }
168 }