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 TGF format for end user to review.
32   */
33  class VerboseGraphTgfSerializer extends AbstractVerboseGraphSerializer
34  {
35  
36      @Override
37      public String serialize( DependencyNode root )
38      {
39          Set<String> coordinateStrings = new HashSet<>();
40          Map<String, String> coordinateVersionMap = new HashMap<>();
41          Map<DependencyNode, String> nodeErrors = getNodeConflictMessagesBfs( root, coordinateStrings,
42                  coordinateVersionMap );
43  
44          StringBuilder nodes = new StringBuilder();
45          StringBuilder edges = new StringBuilder( "#" );
46          edges.append( "\n" );
47  
48          // deal with root first
49          Artifact rootArtifact = root.getArtifact();
50          nodes.append( root.hashCode() ).append( " " ).append( rootArtifact.getGroupId() ).append( ":" ).append(
51                  rootArtifact.getArtifactId() ).append( ":" ).append( rootArtifact.getExtension() ).append( ":" ).append(
52                  rootArtifact.getVersion() );
53  
54          if ( root.getData().containsKey( "ContainsModule" ) )
55          {
56              nodes.append( " WARNING: this tree contains a submodule. Once it reaches the submodule will print "
57                      + "in nonVerbose fashion, to see the actual submodule "
58                      + "verbose output refer to the rest of the output" );
59          }
60  
61          nodes.append( "\n" );
62  
63          for ( DependencyNode child : root.getChildren() )
64          {
65              edges.append( root.hashCode() ).append( " " ).append( child.hashCode() ).append( " " ).append(
66                      child.getDependency().getScope() ).append( "\n" );
67              serializeTgfDfs( child, nodeErrors, nodes, edges );
68          }
69  
70          return nodes.append( edges ).toString();
71      }
72  
73      private void serializeTgfDfs( DependencyNode node, Map<DependencyNode, String> nodeErrors, StringBuilder nodes,
74                                    StringBuilder edges )
75      {
76          nodes.append( node.hashCode() ).append( " " );
77          String coordString = "";
78          boolean messageAdded = false;
79  
80          if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_VERSION ) )
81          {
82              coordString += " - version managed from " + node.getArtifact().getProperties().get( PRE_MANAGED_VERSION );
83              messageAdded = true;
84          }
85          if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_SCOPE ) )
86          {
87              if ( messageAdded )
88              {
89                  coordString += "; ";
90              }
91              else
92              {
93                  coordString += " - ";
94                  messageAdded = true;
95              }
96              coordString +=
97                      "scope managed from " + node.getArtifact().getProperties().get( PRE_MANAGED_SCOPE );
98          }
99          coordString = getDependencyCoordinate( node ) + coordString;
100 
101 
102         if ( node.getArtifact().getProperties().containsKey( "Cycle" ) )
103         {
104             if ( messageAdded )
105             {
106                 coordString += "; ";
107             }
108             else
109             {
110                 coordString += " - ";
111             }
112             coordString += "omitted for cycle";
113             nodes.append( "(" ).append( coordString ).append( ")" ).append( "\n" );
114         }
115         else if ( nodeErrors.get( node ) != null )
116         {
117             nodes.append( "(" );
118             if ( messageAdded )
119             {
120                 nodes.append( coordString ).append( "; " ).append( nodeErrors.get( node ) );
121             }
122             else
123             {
124                 nodes.append( coordString ).append( " - " ).append( nodeErrors.get( node ) );
125             }
126             nodes.append( ")" ).append( "\n" );
127         }
128         else
129         {
130             nodes.append( coordString ).append( "\n" );
131             for ( DependencyNode child : node.getChildren() )
132             {
133                 edges.append( node.hashCode() ).append( " " ).append( child.hashCode() ).append( " " );
134                 if ( child.getArtifact().getProperties().get( MANAGED_SCOPE ) != null )
135                 {
136                     edges.append( child.getArtifact().getProperties().get( MANAGED_SCOPE ) ).append( " managed from " )
137                             .append( child.getArtifact().getProperties().get( PRE_MANAGED_SCOPE ) );
138                 }
139                 else
140                 {
141                     edges.append( child.getDependency().getScope() );
142                 }
143 
144                 if ( child.getArtifact().getProperties().containsKey( "Cycle" ) )
145                 {
146                     edges.append( " omitted for cycle" );
147                 }
148                 else if ( nodeErrors.get( child ) != null )
149                 {
150                     edges.append( " " ).append( nodeErrors.get( child ) );
151                 }
152                 edges.append( "\n" );
153                 serializeTgfDfs( child, nodeErrors, nodes, edges );
154             }
155         }
156     }
157 }