Skip to content

Commit a6bf220

Browse files
committed
[MDEP-799] tree: add optional output type json
1 parent 6a587d3 commit a6bf220

File tree

3 files changed

+163
-10
lines changed

3 files changed

+163
-10
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.apache.maven.plugins.dependency.tree;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
24+
import org.apache.maven.artifact.Artifact;
25+
import org.apache.maven.shared.dependency.graph.DependencyNode;
26+
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
27+
28+
import java.io.Writer;
29+
import java.util.List;
30+
import com.google.common.base.Joiner;
31+
32+
/**
33+
* A dependency node visitor that serializes visited nodes to a writer using the
34+
* JSON format.
35+
*
36+
* @author <a href="mailto:kezhenxu94@apache.org">Zhenxu Ke</a>
37+
* @since 3.3.1
38+
*/
39+
public class JSONDependencyNodeVisitor
40+
extends AbstractSerializingVisitor
41+
implements DependencyNodeVisitor
42+
{
43+
44+
/**
45+
* Constructor.
46+
*
47+
* @param writer the writer to write to.
48+
*/
49+
public JSONDependencyNodeVisitor( Writer writer )
50+
{
51+
super( writer );
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
@Override
58+
public boolean endVisit( DependencyNode node )
59+
{
60+
return true;
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
@Override
67+
public boolean visit( DependencyNode node )
68+
{
69+
if ( node.getParent() == null || node.getParent() == node )
70+
{
71+
writeNode( 0, node, true );
72+
}
73+
74+
return true;
75+
}
76+
77+
private void writeNode( int indent, DependencyNode node, boolean root )
78+
{
79+
Artifact artifact = node.getArtifact();
80+
81+
writer.println( indentations( indent ) + "{" );
82+
indent++;
83+
String groupId = indentations( indent ) + "\"groupId\": \"" + artifact.getGroupId() + "\"";
84+
String artifactId = indentations( indent ) + "\"artifactId\": \"" + artifact.getArtifactId() + "\"";
85+
String version = indentations( indent ) + "\"version\": \"" + artifact.getVersion() + "\"";
86+
String type = indentations( indent ) + "\"type\": \"" + artifact.getType() + "\"";
87+
String scope = root ? null : indentations( indent ) + "\"scope\": \"" + artifact.getScope() + "\"";
88+
89+
writer.print( Joiner.on( "," + System.lineSeparator() )
90+
.skipNulls()
91+
.join( groupId, artifactId, version, type, scope ) );
92+
93+
List<DependencyNode> children = node.getChildren();
94+
if ( children.size() > 0 )
95+
{
96+
writer.println( "," );
97+
writer.println( indentations( indent ) + "\"dependencies\": [" );
98+
indent++;
99+
for ( int i = 0; i < children.size(); i++ )
100+
{
101+
writeNode( indent, children.get( i ), false );
102+
if ( i < children.size() - 1 )
103+
{
104+
writer.println( "," );
105+
}
106+
else
107+
{
108+
writer.println();
109+
}
110+
}
111+
indent--;
112+
writer.println( indentations( indent ) + "]" );
113+
}
114+
else
115+
{
116+
writer.println();
117+
}
118+
indent--;
119+
writer.print( indentations( indent ) + "}" );
120+
}
121+
122+
private static String indentations( int indent )
123+
{
124+
return StringUtils.repeat( "\t", indent );
125+
}
126+
}

src/main/java/org/apache/maven/plugins/dependency/tree/TreeMojo.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class TreeMojo
8888

8989
@Parameter( defaultValue = "${session}", readonly = true, required = true )
9090
private MavenSession session;
91-
91+
9292
@Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
9393
private String outputEncoding;
9494

@@ -179,18 +179,18 @@ public class TreeMojo
179179
/**
180180
* A comma-separated list of artifacts to filter the serialized dependency tree by, or <code>null</code> not to
181181
* filter the dependency tree. The filter syntax is:
182-
*
182+
*
183183
* <pre>
184184
* [groupId]:[artifactId]:[type]:[version]
185185
* </pre>
186-
*
186+
*
187187
* where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
188188
* segment is treated as an implicit wildcard.
189189
* <p>
190190
* For example, <code>org.apache.*</code> will match all artifacts whose group id starts with
191191
* <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
192192
* </p>
193-
*
193+
*
194194
* @see StrictPatternIncludesArtifactFilter
195195
* @since 2.0-alpha-6
196196
*/
@@ -200,11 +200,11 @@ public class TreeMojo
200200
/**
201201
* A comma-separated list of artifacts to filter from the serialized dependency tree, or <code>null</code> not to
202202
* filter any artifacts from the dependency tree. The filter syntax is:
203-
*
203+
*
204204
* <pre>
205205
* [groupId]:[artifactId]:[type]:[version]
206206
* </pre>
207-
*
207+
*
208208
* where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
209209
* segment is treated as an implicit wildcard.
210210
* <p>
@@ -416,6 +416,10 @@ else if ( "dot".equals( outputType ) )
416416
{
417417
return new DOTDependencyNodeVisitor( writer );
418418
}
419+
else if ( "json".equals( outputType ) )
420+
{
421+
return new JSONDependencyNodeVisitor( writer );
422+
}
419423
else
420424
{
421425
return new SerializingDependencyNodeVisitor( writer, toGraphTokens( tokens ) );

src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void _testTreeDotSerializing()
110110

111111
/**
112112
* Test the GraphML format serialization
113-
*
113+
*
114114
* @throws Exception in case of an error.
115115
*/
116116
public void _testTreeGraphMLSerializing()
@@ -129,7 +129,7 @@ public void _testTreeGraphMLSerializing()
129129

130130
/**
131131
* Test the TGF format serialization
132-
*
132+
*
133133
* @throws Exception in case of an error.
134134
*/
135135
public void _testTreeTGFSerializing()
@@ -141,9 +141,32 @@ public void _testTreeTGFSerializing()
141141
assertTrue( findString( contents, "testGroupId:release:jar:1.0:compile" ) );
142142
}
143143

144+
/**
145+
* Test the JSON format serialization
146+
*
147+
* @throws Exception in case of an error.
148+
*/
149+
public void _testTreeJSONSerializing()
150+
throws Exception
151+
{
152+
List<String> contents = runTreeMojo( "tree1.json", "json" );
153+
assertTrue( findString( contents, "\"testGroupId\": \"project\"" ) );
154+
assertTrue( findString( contents, "\"type: \"jar\"" ) );
155+
assertTrue( findString( contents, "\"version\": \"1.0\"" ) );
156+
assertTrue( findString( contents, "\"scope\": \"compile\"" ) );
157+
assertTrue( findString( contents, "\"testGroupId\": \"snapshot\"" ) );
158+
assertTrue( findString( contents, "\"type: \"jar\"" ) );
159+
assertTrue( findString( contents, "\"version\": \"2.0-SNAPSHOT\"" ) );
160+
assertTrue( findString( contents, "\"scope\": \"compile\"" ) );
161+
assertTrue( findString( contents, "\"testGroupId\": \"release\"" ) );
162+
assertTrue( findString( contents, "\"type: \"jar\"" ) );
163+
assertTrue( findString( contents, "\"version\": \"1.0\"" ) );
164+
assertTrue( findString( contents, "\"scope\": \"compile\"" ) );
165+
}
166+
144167
/**
145168
* Help finding content in the given list of string
146-
*
169+
*
147170
* @param outputFile the outputFile.
148171
* @param format The format.
149172
* @throws Exception in case of an error.
@@ -187,7 +210,7 @@ private List<String> runTreeMojo( String outputFile, String format )
187210

188211
/**
189212
* Help finding content in the given list of string
190-
*
213+
*
191214
* @param contents The contents.
192215
* @param str The content which should be checked for.
193216
*/

0 commit comments

Comments
 (0)