You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "MartinWitt (via GitHub)" <gi...@apache.org> on 2023/05/22 20:37:46 UTC

[GitHub] [maven-dependency-plugin] MartinWitt opened a new pull request, #325: WIP: [MDEP-799] tree: add optional output type json

MartinWitt opened a new pull request, #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325

   Following this checklist to help us incorporate your 
   contribution quickly and easily:
   
    - [X] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MDEP) filed 
          for the change (usually before you start working on it).  Trivial changes like typos do not 
          require a JIRA issue.  Your pull request should address just this issue, without 
          pulling in other changes.
    - [ ] Each commit in the pull request should have a meaningful subject line and body.
   I will squash and update the commit in the end. There is still open discussion. 
   
    - [X] Format the pull request title like `[MDEP-XXX] - Fixes bug in ApproximateQuantiles`,
          where you replace `MDEP-XXX` with the appropriate JIRA issue. Best practice
          is to use the JIRA issue title in the pull request title and in the first line of the 
          commit message.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [X] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will 
          be performed on your pull request automatically.
    - [ ] You have run the integration tests successfully (`mvn -Prun-its clean verify`).
   
   
   
   
   
    - [X] I hereby declare this contribution to be licensed under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [X] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   I sent the mail for this a few minutes ago so it should be signed soon.
   
   
   I tried to understand the way you write unit/integration tests but it looks a bit complicated. What is the correct way to add a testcase? Best case, I can add a pom and check if the model written as json -> read by any JSON parser is equal to the model before.
   
   


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


[GitHub] [maven-dependency-plugin] MartinWitt commented on a diff in pull request #325: [MDEP-799] tree: add optional output type json

Posted by "MartinWitt (via GitHub)" <gi...@apache.org>.
MartinWitt commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1209218850


##########
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:
   In the other PR someone asked for a switch case instead. Shall I convert this to a switch statement?



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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "pombredanne (via GitHub)" <gi...@apache.org>.
pombredanne commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1485889948


##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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("\n");
+        writeNode(indent, node, sb);
+        sb.append("}").append("\n");
+        try {
+            writer.write(sb.toString());
+        } catch (IOException e) {
+            throw new RuntimeException("Error while writing json output", e);

Review Comment:
   What about just not catch an exception here and leaving this business to the callers.... fixing the PrintWriter could then be done in another patch and not here.



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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "pombredanne (via GitHub)" <gi...@apache.org>.
pombredanne commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1485887930


##########
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:
   @elharo the cure may be to keep track of already visited nodes and either fail or keep trucking?



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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1590867632


##########
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:
   add a test for this case, see what works



##########
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:
   contains is clearer



##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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("\n");
+        writeNode(indent, node, sb);
+        sb.append("}").append("\n");
+        try {
+            writer.write(sb.toString());
+        } catch (IOException e) {
+            throw new RuntimeException("Error while writing json output", e);

Review Comment:
   Fixing PrintWriter should be a different PR but that might be a prerequisite for this PR. I'm not sure. It seems fine to let this method throw IOException and handle it further up the stack. RuntimeException is not OK.



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


[GitHub] [maven-dependency-plugin] elharo commented on a diff in pull request #325: [MDEP-799] tree: add optional output type json

Posted by "elharo (via GitHub)" <gi...@apache.org>.
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



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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "pombredanne (via GitHub)" <gi...@apache.org>.
pombredanne commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1938280244

   Looking at the details of this PR, it feels to me that crafting the JSON by hand as done here feels like problems in the making with encoding or else. Should not this use a proper JSON library of sorts? Is not there one bundled in the standard Maven or Java and this plugin may therefore always have have one on hand? 
   
   (Sorry for these likely dumb questions: my Java skills need to brushing off)


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


[GitHub] [maven-dependency-plugin] MartinWitt commented on pull request #325: [MDEP-799] tree: add optional output type json

Posted by "MartinWitt (via GitHub)" <gi...@apache.org>.
MartinWitt commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1592077654

   @elharo could you approve the worflow run?


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


[GitHub] [maven-dependency-plugin] MartinWitt commented on a diff in pull request #325: [MDEP-799] tree: add optional output type json

Posted by "MartinWitt (via GitHub)" <gi...@apache.org>.
MartinWitt commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1233338155


##########
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:
   I tried to keep the test case in the same style as the rest of the class. But I can change 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.

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

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


[GitHub] [maven-dependency-plugin] MartinWitt commented on pull request #325: [MDEP-799] tree: add optional output type json

Posted by "MartinWitt (via GitHub)" <gi...@apache.org>.
MartinWitt commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1559869241

   CLA is now signed and accepted. 


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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#discussion_r1484260841


##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 {

Review Comment:
   Does this need to be public? It's easier to evolve and iterate on if it's not.



##########
src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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("\n");
+        writeNode(indent, node, sb);
+        sb.append("}").append("\n");
+        try {
+            writer.write(sb.toString());
+        } catch (IOException e) {
+            throw new RuntimeException("Error while writing json output", e);

Review Comment:
   needs a more specific exception. Looking at this now I notice that the superclass and interface are badly designed. They use a PrintWriter which swallows exceptions. That might need to be fixed. 



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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "yahavi (via GitHub)" <gi...@apache.org>.
yahavi commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1972780341

   If it's useful to anyone, we've developed a Maven dependency tree plugin that generates a JSON dependency tree. Check it out at: https://github.com/jfrog/maven-dep-tree.


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


[GitHub] [maven-dependency-plugin] MartinWitt commented on pull request #325: [MDEP-799] tree: add optional output type json

Posted by "MartinWitt (via GitHub)" <gi...@apache.org>.
MartinWitt commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1567008233

   As I am a first-time contributor, someone has to approve the workflow run before we see CI results.


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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "pombredanne (via GitHub)" <gi...@apache.org>.
pombredanne commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-1935725046

   @MartinWitt what's left to do to get this through?
   FWIW, there is a cottage industry of smalls tools and scripts that are parsing this plugin output .... it would be awesome to have a structure JSON output! 
   Anything to help you need move this forward?


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


Re: [PR] [MDEP-799] tree: add optional output type json [maven-dependency-plugin]

Posted by "monperrus (via GitHub)" <gi...@apache.org>.
monperrus commented on PR #325:
URL: https://github.com/apache/maven-dependency-plugin/pull/325#issuecomment-2095222237

   @pombredanne @elharo if we are able to drive to merge, we can put more effort on this PR.


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