ian-lavallee commented on a change in pull request #92: URL: https://github.com/apache/maven-dependency-plugin/pull/92#discussion_r465194646
########## File path: src/main/java/org/apache/maven/plugins/dependency/tree/verbose/VerboseGraphSerializer.java ########## @@ -0,0 +1,323 @@ +package org.apache.maven.plugins.dependency.tree.verbose; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.graph.DependencyNode; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; + +/** + * Parses dependency graph and outputs in text format for end user to review. + */ +public final class VerboseGraphSerializer +{ + private static final String LINE_START_LAST_CHILD = "\\- "; + private static final String LINE_START_CHILD = "+- "; + + public String serialize( DependencyNode root ) + { + Set<String> coordinateStrings = new HashSet<>(); + Map<String, String> coordinateVersionMap = new HashMap<>(); + StringBuilder builder = new StringBuilder(); + + // Use BFS to mirror how Maven resolves dependencies and use DFS to print the tree easily + Map<DependencyNode, String> nodeErrors = getNodeConflictMessagesBfs( root, coordinateStrings, + coordinateVersionMap ); + + // deal with root first + Artifact rootArtifact = root.getArtifact(); + builder.append( rootArtifact.getGroupId() ).append( ":" ).append( rootArtifact.getArtifactId() ).append( ":" ) + .append( rootArtifact.getExtension() ).append( ":" ).append( rootArtifact.getVersion() ).append( + System.lineSeparator() ); + + for ( int i = 0; i < root.getChildren().size(); i++ ) + { + if ( i == root.getChildren().size() - 1 ) + { + dfsPrint( root.getChildren().get( i ), LINE_START_LAST_CHILD, false, builder, nodeErrors ); + } + else + { + dfsPrint( root.getChildren().get( i ), LINE_START_CHILD, false, builder, nodeErrors ); + } + } + return builder.toString(); + } + + private static String getDependencyCoordinate( DependencyNode node ) + { + Artifact artifact = node.getArtifact(); + + if ( node.getDependency() == null ) + { + // should only get here if node is root + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getExtension() + ":" + + artifact.getVersion(); + } + + String scope; + if ( artifact.getProperties().containsKey( "managedScope" ) ) + { + scope = artifact.getProperties().get( "managedScope" ); + } + else + { + scope = node.getDependency().getScope(); + } + + String coords = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getExtension() + ":" + + artifact.getVersion(); + + if ( scope != null && !scope.isEmpty() ) + { + coords = coords.concat( ":" + scope ); + } + return coords; + } + + private static String getVersionlessScopelessCoordinate( DependencyNode node ) + { + Artifact artifact = node.getArtifact(); + // scope not included because we check for scope conflicts separately + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getExtension(); + } + + private static boolean isDuplicateDependencyCoordinate( DependencyNode node, Set<String> coordinateStrings ) + { + return coordinateStrings.contains( getDependencyCoordinate( node ) ); + } + + private static String versionConflict( DependencyNode node, Map<String, String> coordinateVersionMap ) + { + if ( coordinateVersionMap.containsKey( getVersionlessScopelessCoordinate( node ) ) ) + { + return coordinateVersionMap.get( getVersionlessScopelessCoordinate( node ) ); + } + return null; + } + + private static String scopeConflict( DependencyNode node, Set<String> coordinateStrings ) + { + Artifact artifact = node.getArtifact(); + List<String> scopes = Arrays.asList( "compile", "provided", "runtime", "test", "system" ); + + for ( String scope : scopes ) + { + String version; + if ( artifact.getProperties().containsKey( "version" ) ) + { + version = artifact.getProperties().get( "version" ); + } + else + { + version = artifact.getVersion(); + } + + String coordinate = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getExtension() + + ":" + version + ":" + scope; + if ( coordinateStrings.contains( coordinate ) ) + { + return scope; + } + } + return null; + } + + private Map<DependencyNode, String> getNodeConflictMessagesBfs( DependencyNode root, Set<String> coordinateStrings + , Map<String, String> coordinateVersionMap ) + { + Map<DependencyNode, String> nodeErrors = new HashMap<>(); + Set<DependencyNode> visitedNodes = new HashSet<>( 512 ); + Queue<DependencyNode> queue = new LinkedList<>(); + visitedNodes.add( root ); + queue.add( root ); + + while ( !queue.isEmpty() ) + { + DependencyNode node = queue.poll(); + + if ( node == null || node.getArtifact() == null ) + { + // Should never reach hit this condition with a proper graph sent in + nodeErrors.put( node, "Null Artifact Node" ); Review comment: My rationale for not throwing an exception is since this is the verbose option if a user is running this then something has probably gone wrong with their dependency resolution and we should show them if the graph has an invalid node somewhere so they can look into it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
