You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "michael-o (via GitHub)" <gi...@apache.org> on 2023/03/17 21:55:44 UTC

[GitHub] [maven-dependency-plugin] michael-o commented on a diff in pull request #207: [MDEP-799] - improve dependency:tree to add optional JSON output of the results

michael-o commented on code in PR #207:
URL: https://github.com/apache/maven-dependency-plugin/pull/207#discussion_r1140746483


##########
src/main/java/org/apache/maven/plugins/dependency/tree/JSONDependencyNodeVisitor.java:
##########
@@ -0,0 +1,125 @@
+package org.apache.maven.plugins.dependency.tree;
+
+/*
+ * 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.apache.commons.lang3.StringUtils;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+
+import java.io.Writer;
+import java.util.List;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using the
+ * JSON format.
+ *
+ * @author <a href="mailto:kezhenxu94@apache.org">Zhenxu Ke</a>
+ * @since 3.3.1
+ */
+public class JSONDependencyNodeVisitor
+    extends AbstractSerializingVisitor
+    implements DependencyNodeVisitor
+{
+
+    /**
+     * Constructor.
+     *
+     * @param writer the writer to write to.
+     */
+    public JSONDependencyNodeVisitor( Writer writer )
+    {
+        super( writer );
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean endVisit( DependencyNode node )
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean visit( DependencyNode node )
+    {
+        if ( node.getParent() == null || node.getParent() == node )
+        {
+            writeNode( 0, node, true );
+        }
+
+        return true;
+    }
+
+    private void writeNode( int indent, DependencyNode node, boolean root )
+    {
+        Artifact artifact = node.getArtifact();
+
+        writer.println( indentations( indent ) + "{" );
+        indent++;
+        String groupId = indentations( indent ) + "\"groupId\": \"" + artifact.getGroupId() + "\"";
+        String artifactId = indentations( indent ) + "\"artifactId\": \"" + artifact.getArtifactId() + "\"";
+        String version = indentations( indent ) + "\"version\": \"" + artifact.getVersion() + "\"";
+        String type = indentations( indent ) + "\"type\": \"" + artifact.getType() + "\"";
+        String scope = indentations( indent ) + "\"scope\": \"" + artifact.getScope() + "\"";
+        String[] elements = root ? new String[] { groupId, artifactId, version, type }
+                                 : new String[] { groupId, artifactId, version, type, scope };
+
+        writer.print( StringUtils.join( "," + System.lineSeparator(), elements ) );

Review Comment:
   Why, a `PrintWriter` will do the same?!



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org