From a84888d8f973bb0e4030f9c417d4a629239e2a12 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Fri, 8 Nov 2019 14:13:07 +0100 Subject: [PATCH 1/7] [MDEP-435] - Added XML dependency node visitor --- .../tree/XMLDependencyNodeVisitor.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java new file mode 100644 index 000000000..3096dce7a --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java @@ -0,0 +1,149 @@ +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 javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +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 + * XML format + * + * @author Bogdan Sikora + * @since 3.1.2 + */ +public class XMLDependencyNodeVisitor + extends AbstractSerializingVisitor + implements DependencyNodeVisitor +{ + /** + * Constructor. + * + * @param writer the writer to write to. + */ + public XMLDependencyNodeVisitor( Writer writer ) + { + super( writer ); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean visit( DependencyNode node ) + { + try + { + if ( node.getParent() == null || node.getParent() == node ) + { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.newDocument(); + Element rootElement = getNode( doc, node ); + doc.appendChild( rootElement ); + + List children = node.getChildren(); + for ( DependencyNode child : children ) + { + handleChild( doc, child, rootElement ); + } + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); + transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" ); + DOMSource source = new DOMSource( doc ); + + StreamResult console = new StreamResult( writer ); + + transformer.transform( source, console ); + } + } + catch ( ParserConfigurationException | TransformerException e ) + { + e.printStackTrace(); + } + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean endVisit( DependencyNode node ) + { + return true; + } + + /** + * Render child with its children recursively + * + * @param doc Docuemnt to use + * @param node child node to handle + * @param depth depth of the child + */ + private void handleChild( Document doc, DependencyNode node, Element parent ) + { + Element element = getNode( doc, node ); + parent.appendChild( element ); + + List children = node.getChildren(); + for ( DependencyNode child : children ) + { + handleChild( doc, child, element ); + } + } + + /** + * Get element from node + * + * @param doc Docuemnt to use + * @param node Node to get data from + */ + private Element getNode( Document doc, DependencyNode node ) + { + Artifact artifact = node.getArtifact(); + Element element = doc.createElement( artifact.getArtifactId() ); + element.setAttribute( "groupId", artifact.getGroupId() ); + element.setAttribute( "version", artifact.getVersion() ); + element.setAttribute( "scope", artifact.getScope() ); + element.setAttribute( "type", artifact.getType() ); + + return element; + } +} From c780355cd514e39e73bf47229e3d34d6c5444785 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Fri, 8 Nov 2019 14:20:08 +0100 Subject: [PATCH 2/7] [MDEP-435] - Added xml option to outputType --- .../org/apache/maven/plugins/dependency/tree/TreeMojo.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java index 1acc162ef..b59745539 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java @@ -107,7 +107,8 @@ public class TreeMojo /** * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently - * supported format are: text (default), dot, graphml and tgf. + * supported format are: + * text (default), dot, graphml, tgf and xml. * These additional formats can be plotted to image files. * * @since 2.2 @@ -384,6 +385,10 @@ else if ( "dot".equals( outputType ) ) { return new DOTDependencyNodeVisitor( writer ); } + else if ( "xml".equals( outputType ) ) + { + return new XMLDependencyNodeVisitor( writer ); + } else { return new SerializingDependencyNodeVisitor( writer, toGraphTokens( tokens ) ); From fc74901167d949f3e915157948fbdcbe6761e20d Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Thu, 14 Nov 2019 16:36:11 +0100 Subject: [PATCH 3/7] [MDEP-435] - Update of XML dependency tree --- .../tree/XMLDependencyNodeVisitor.java | 51 ++++++++++++++----- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java index 3096dce7a..69959d975 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java @@ -29,13 +29,12 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.dependency.graph.DependencyNode; import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor; - +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; import java.io.Writer; import java.util.List; @@ -73,7 +72,7 @@ public boolean visit( DependencyNode node ) DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); - Element rootElement = getNode( doc, node ); + Element rootElement = getNode( doc, node, true ); doc.appendChild( rootElement ); List children = node.getChildren(); @@ -119,8 +118,9 @@ public boolean endVisit( DependencyNode node ) */ private void handleChild( Document doc, DependencyNode node, Element parent ) { - Element element = getNode( doc, node ); - parent.appendChild( element ); + Element element = getNode( doc, node, false ); + Node dependencies = parent.getElementsByTagName( "dependencies" ).item(0); + dependencies.appendChild( element ); List children = node.getChildren(); for ( DependencyNode child : children ) @@ -135,14 +135,39 @@ private void handleChild( Document doc, DependencyNode node, Element parent ) * @param doc Docuemnt to use * @param node Node to get data from */ - private Element getNode( Document doc, DependencyNode node ) + private Element getNode( Document doc, DependencyNode node, Boolean root ) { Artifact artifact = node.getArtifact(); - Element element = doc.createElement( artifact.getArtifactId() ); - element.setAttribute( "groupId", artifact.getGroupId() ); - element.setAttribute( "version", artifact.getVersion() ); - element.setAttribute( "scope", artifact.getScope() ); - element.setAttribute( "type", artifact.getType() ); + Element element = null; + + if (root) { + element = doc.createElement( "project" ); + } else { + element = doc.createElement( "dependency" ); + } + + Element groupId = doc.createElement( "groupId" ); + groupId.setTextContent(artifact.getGroupId()); + element.appendChild( groupId ); + + Element artifactId = doc.createElement( "artifactId" ); + artifactId.setTextContent(artifact.getArtifactId()); + element.appendChild( artifactId ); + + Element version = doc.createElement( "version" ); + version.setTextContent(artifact.getVersion()); + element.appendChild( version ); + + Element scope = doc.createElement( "scope" ); + scope.setTextContent(artifact.getScope()); + element.appendChild( scope ); + + Element type = doc.createElement( "type" ); + type.setTextContent(artifact.getType()); + element.appendChild( type ); + + Element dependencies = doc.createElement( "dependencies" ); + element.appendChild( dependencies ); return element; } From 88b36d5873c03915b7b689b2cc87cd103c27f5b5 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Fri, 15 Nov 2019 09:57:41 +0100 Subject: [PATCH 4/7] [MDEP-435] - Fixed code style issues --- .../tree/XMLDependencyNodeVisitor.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java index 69959d975..adbfe0218 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java @@ -119,7 +119,7 @@ public boolean endVisit( DependencyNode node ) private void handleChild( Document doc, DependencyNode node, Element parent ) { Element element = getNode( doc, node, false ); - Node dependencies = parent.getElementsByTagName( "dependencies" ).item(0); + Node dependencies = parent.getElementsByTagName( "dependencies" ).item( 0 ); dependencies.appendChild( element ); List children = node.getChildren(); @@ -140,30 +140,33 @@ private Element getNode( Document doc, DependencyNode node, Boolean root ) Artifact artifact = node.getArtifact(); Element element = null; - if (root) { + if ( root ) + { element = doc.createElement( "project" ); - } else { + } + else + { element = doc.createElement( "dependency" ); } Element groupId = doc.createElement( "groupId" ); - groupId.setTextContent(artifact.getGroupId()); + groupId.setTextContent( artifact.getGroupId() ); element.appendChild( groupId ); Element artifactId = doc.createElement( "artifactId" ); - artifactId.setTextContent(artifact.getArtifactId()); + artifactId.setTextContent( artifact.getArtifactId() ); element.appendChild( artifactId ); Element version = doc.createElement( "version" ); - version.setTextContent(artifact.getVersion()); + version.setTextContent( artifact.getVersion() ); element.appendChild( version ); Element scope = doc.createElement( "scope" ); - scope.setTextContent(artifact.getScope()); + scope.setTextContent( artifact.getScope() ); element.appendChild( scope ); Element type = doc.createElement( "type" ); - type.setTextContent(artifact.getType()); + type.setTextContent( artifact.getType() ); element.appendChild( type ); Element dependencies = doc.createElement( "dependencies" ); From c7ca99e35b8b2521f5342b1848fc596a00f57178 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Fri, 15 Nov 2019 10:45:10 +0100 Subject: [PATCH 5/7] [MDEP-435] - root node updated --- .../tree/XMLDependencyNodeVisitor.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java index adbfe0218..55533e405 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java @@ -161,13 +161,22 @@ private Element getNode( Document doc, DependencyNode node, Boolean root ) version.setTextContent( artifact.getVersion() ); element.appendChild( version ); - Element scope = doc.createElement( "scope" ); - scope.setTextContent( artifact.getScope() ); - element.appendChild( scope ); + if ( !root ) + { + Element scope = doc.createElement( "scope" ); + scope.setTextContent( artifact.getScope() ); + element.appendChild( scope ); - Element type = doc.createElement( "type" ); - type.setTextContent( artifact.getType() ); - element.appendChild( type ); + Element type = doc.createElement( "type" ); + type.setTextContent( artifact.getType() ); + element.appendChild( type ); + } + else + { + Element packaging = doc.createElement( "packaging" ); + packaging.setTextContent( artifact.getType() ); + element.appendChild( packaging ); + } Element dependencies = doc.createElement( "dependencies" ); element.appendChild( dependencies ); From 642b299c7cfb83be2bdca006746297696e9971e6 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Mon, 25 Nov 2019 12:04:36 +0100 Subject: [PATCH 6/7] [MDEP-435] Logger added to parse error --- .../plugins/dependency/tree/XMLDependencyNodeVisitor.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java index 55533e405..8bb18849c 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java @@ -37,6 +37,7 @@ import org.w3c.dom.Node; import java.io.Writer; import java.util.List; +import org.slf4j.LoggerFactory; /** * A dependency node visitor that serializes visited nodes to @@ -94,7 +95,7 @@ public boolean visit( DependencyNode node ) } catch ( ParserConfigurationException | TransformerException e ) { - e.printStackTrace(); + LoggerFactory.getLogger(XMLDependencyNodeVisitor.class).error(e.getMessage()); } return true; @@ -135,7 +136,7 @@ private void handleChild( Document doc, DependencyNode node, Element parent ) * @param doc Docuemnt to use * @param node Node to get data from */ - private Element getNode( Document doc, DependencyNode node, Boolean root ) + private Element getNode( Document doc, DependencyNode node, boolean root ) { Artifact artifact = node.getArtifact(); Element element = null; From 8150710c9e981407ce345a21c891e38f618e9dfa Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Fri, 29 Nov 2019 08:59:34 +0100 Subject: [PATCH 7/7] [MDEP-435] Javadoc updated with unordered list --- .../apache/maven/plugins/dependency/tree/TreeMojo.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java index b59745539..bce08a888 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java @@ -108,8 +108,13 @@ public class TreeMojo /** * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently * supported format are: - * text (default), dot, graphml, tgf and xml. - * These additional formats can be plotted to image files. + *
    + *
  • text (default)
  • + *
  • dot
  • + *
  • graphml
  • + *
  • tgf
  • + *
  • xml
  • + *
* * @since 2.2 */