[
https://issues.apache.org/jira/browse/MDEP-799?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17732800#comment-17732800
]
ASF GitHub Bot commented on MDEP-799:
-------------------------------------
elharo commented on code in PR #325:
URL:
https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1230261635
##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.dependency.tree;
+
+import java.io.IOException;
+import java.io.Writer;
+
+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;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using
the JSON format.
+ */
+public class JsonDependencyNodeVisitor extends AbstractSerializingVisitor
implements DependencyNodeVisitor {
+
+ private static final String LINE_SEPARATOR = "\n";
+ private String indentChar = " ";
+
+ /**
+ * Creates a new instance of {@link JsonDependencyNodeVisitor}. The writer
will be used to write the output.
+ * @param writer the writer to write to
+ */
+ public JsonDependencyNodeVisitor(Writer writer) {
+ super(writer);
+ }
+
+ @Override
+ public boolean visit(DependencyNode node) {
Review Comment:
Is it possible for this to et stuck in an infinite recursion with a
maliciously hand-crafted dependency node? Or for that matter, with a
non-maliciious but buggy circular dependency tree (which does happen)?
##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.dependency.tree;
+
+import java.io.IOException;
+import java.io.Writer;
+
+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;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using
the JSON format.
+ */
+public class JsonDependencyNodeVisitor extends AbstractSerializingVisitor
implements DependencyNodeVisitor {
+
+ private static final String LINE_SEPARATOR = "\n";
Review Comment:
inline this, a simple "\n" is clearer
##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.dependency.tree;
+
+import java.io.IOException;
+import java.io.Writer;
+
+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;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using
the JSON format.
+ */
+public class JsonDependencyNodeVisitor extends AbstractSerializingVisitor
implements DependencyNodeVisitor {
+
+ private static final String LINE_SEPARATOR = "\n";
+ private String indentChar = " ";
+
+ /**
+ * Creates a new instance of {@link JsonDependencyNodeVisitor}. The writer
will be used to write the output.
+ * @param writer the writer to write to
+ */
+ public JsonDependencyNodeVisitor(Writer writer) {
+ super(writer);
+ }
+
+ @Override
+ public boolean visit(DependencyNode node) {
+ if (node.getParent() == null || node.getParent() == node) {
+ writeRootNode(node, writer);
+ }
+ return true;
+ }
+
+ /**
+ * Writes the node to the writer. This method is recursive and will write
all children nodes.
+ * @param node the node to write
+ * @param writer the writer to write to
+ */
+ private void writeRootNode(DependencyNode node, Writer writer) {
+ int indent = 2;
+ StringBuilder sb = new StringBuilder();
+ sb.append("{").append(LINE_SEPARATOR);
+ writeNode(indent, node, sb);
+ sb.append("}").append(LINE_SEPARATOR);
+ try {
+ writer.write(sb.toString());
+ } catch (IOException e) {
+ throw new RuntimeException("Error while writing json output", e);
+ // TODO: handle exception maybe throw runtime exception or mojo
exception?
+ }
+ }
+ /**
+ * Appends the node and it's children to the string builder.
+ * @param indent the current indent level
+ * @param node the node to write
+ * @param sb the string builder to append to
+ */
+ private void writeNode(int indent, DependencyNode node, StringBuilder sb) {
+ appendNodeValues(sb, indent, node.getArtifact(),
!node.getChildren().isEmpty());
+ if (!node.getChildren().isEmpty()) {
+ writeChildren(indent, node, sb);
+ }
+ }
+ /**
+ * Writes the children of the node to the string builder. And each
children of each node will be written recursively.
+ * @param indent the current indent level
+ * @param node the node to write
+ * @param sb the string builder to append to
+ */
+ private void writeChildren(int indent, DependencyNode node, StringBuilder
sb) {
+ sb.append(indent(indent)).append("\"children\":
[").append(LINE_SEPARATOR);
+ indent += 2;
+ for (int i = 0; i < node.getChildren().size(); i++) {
+ DependencyNode child = node.getChildren().get(i);
+ sb.append(indent(indent));
+ sb.append("{").append(LINE_SEPARATOR);
+ writeNode(indent + 2, child, sb);
+ sb.append(indent(indent)).append("}");
+ // we skip the comma for the last child
+ if (i != node.getChildren().size() - 1) {
+ sb.append(",");
+ }
+ sb.append(LINE_SEPARATOR);
+ }
+ sb.append(indent(indent)).append("]").append(LINE_SEPARATOR);
+ }
+
+ @Override
+ public boolean endVisit(DependencyNode node) {
+ return true;
+ }
+ /**
+ * Appends the artifact values to the string builder.
+ * @param sb the string builder to append to
+ * @param indent the current indent level
+ * @param artifact the artifact to write
+ * @param hasChildren true if the artifact has children
+ */
+ private void appendNodeValues(StringBuilder sb, int indent, Artifact
artifact, boolean hasChildren) {
+ appendKey(sb, indent, "groupId",
StringUtils.defaultString(artifact.getGroupId()));
+ appendKey(sb, indent, "artifactId",
StringUtils.defaultString(artifact.getArtifactId()));
+ appendKey(sb, indent, "version",
StringUtils.defaultString(artifact.getVersion()));
+ appendKey(sb, indent, "type",
StringUtils.defaultString(artifact.getType()));
+ appendKey(sb, indent, "scope",
StringUtils.defaultString(artifact.getScope()));
+ appendKey(sb, indent, "classifier",
StringUtils.defaultString(artifact.getClassifier()));
+ if (hasChildren) {
+ appendKey(sb, indent, "optional",
StringUtils.defaultString(String.valueOf(artifact.isOptional())));
+ } else {
+ appendKeyWithoutComma(
+ sb, indent, "optional",
StringUtils.defaultString(String.valueOf(artifact.isOptional())));
+ }
+ }
+ /**
+ * Appends a key value pair to the string builder.
+ * @param sb the string builder to append to
+ * @param indent the current indent level
+ * @param key the key used as json key
+ * @param value the value used as json value
+ */
+ private void appendKey(StringBuilder sb, int indent, String key, String
value) {
Review Comment:
appendKeyValue
##########
src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java:
##########
@@ -380,6 +380,8 @@ public DependencyNodeVisitor
getSerializingDependencyNodeVisitor(Writer writer)
return new TGFDependencyNodeVisitor(writer);
} else if ("dot".equals(outputType)) {
return new DOTDependencyNodeVisitor(writer);
+ } else if ("json".equals(outputType)) {
+ return new JsonDependencyNodeVisitor(writer);
} else {
return new SerializingDependencyNodeVisitor(writer,
toGraphTokens(tokens));
Review Comment:
no, this is fine
##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.dependency.tree;
+
+import java.io.IOException;
+import java.io.Writer;
+
+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;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer using
the JSON format.
+ */
+public class JsonDependencyNodeVisitor extends AbstractSerializingVisitor
implements DependencyNodeVisitor {
+
+ private static final String LINE_SEPARATOR = "\n";
+ private String indentChar = " ";
+
+ /**
+ * Creates a new instance of {@link JsonDependencyNodeVisitor}. The writer
will be used to write the output.
+ * @param writer the writer to write to
+ */
+ public JsonDependencyNodeVisitor(Writer writer) {
+ super(writer);
+ }
+
+ @Override
+ public boolean visit(DependencyNode node) {
+ if (node.getParent() == null || node.getParent() == node) {
+ writeRootNode(node, writer);
+ }
+ return true;
+ }
+
+ /**
+ * Writes the node to the writer. This method is recursive and will write
all children nodes.
+ * @param node the node to write
+ * @param writer the writer to write to
+ */
+ private void writeRootNode(DependencyNode node, Writer writer) {
+ int indent = 2;
+ StringBuilder sb = new StringBuilder();
+ sb.append("{").append(LINE_SEPARATOR);
+ writeNode(indent, node, sb);
+ sb.append("}").append(LINE_SEPARATOR);
+ try {
+ writer.write(sb.toString());
+ } catch (IOException e) {
+ throw new RuntimeException("Error while writing json output", e);
+ // TODO: handle exception maybe throw runtime exception or mojo
exception?
+ }
+ }
+ /**
+ * Appends the node and it's children to the string builder.
Review Comment:
its
##########
src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java:
##########
@@ -127,6 +127,24 @@ public void _testTreeTGFSerializing() throws Exception {
assertTrue(findString(contents,
"testGroupId:release:jar:1.0:compile"));
}
+ /**
+ * Test the JSON format serialization
+ */
+ public void _testTreeJsonSerialzing() throws Exception {
+ List<String> contents = runTreeMojo("tree1.json", "json");
+ assertTrue(findString(contents, "\"testGroupId\": \"project\""));
Review Comment:
not sure why findString is used here. contains is simple and more obvious
> improve mvn dependency:tree - add optional JSON output of the results
> ---------------------------------------------------------------------
>
> Key: MDEP-799
> URL: https://issues.apache.org/jira/browse/MDEP-799
> Project: Maven Dependency Plugin
> Issue Type: New Feature
> Components: tree
> Reporter: Zhenxu Ke
> Priority: Major
>
> I'd like to add an output type JSON, will open a pull request soon
--
This message was sent by Atlassian Jira
(v8.20.10#820010)