3

I'm a huge fan of

mvn dependency:tree

and want to print a similar-looking tree as plain ascii text as output from my java program.

 com.totsp.gwt:maven-gwt-sample:war:1.0-SNAPSHOT
 +- com.google.gwt:gwt-servlet:jar:2.4.0:compile
 +- com.google.gwt:gwt-user:jar:2.4.0:provided
 |  +- javax.validation:validation-api:jar:1.0.0.GA:provided
 |  \- javax.validation:validation-api:jar:sources:1.0.0.GA:provided
 +- log4j:log4j:jar:1.2.14:compile
 \- junit:junit:jar:4.1:test

I was hoping that the library that achieves this would be easily usable but I can't find it.

The closest substitute I see is this: http://code.google.com/p/j-text-utils/ but it's not as nice as Maven's.

Where can I find a library that prints a tree structure as text almost identically to mvn dependency:tree?

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
  • http://stackoverflow.com/a/8948691/373151 – Hari Menon Jun 02 '12 at 11:17
  • Thanks, but I really don't want to write my own algorithm. I want to reuse the most mature API that surely exists for this. If anyone can tell me the class in the Maven jar that does this that would work too. – Sridhar Sarnobat Jun 02 '12 at 11:50
  • 1
    +1 for mentioning j-text-utils, it is absolutely sufficient for my similar needs. – thSoft Oct 18 '12 at 12:24

2 Answers2

2

I'm not an expert of creating/using MOJOs, but how about downloading and taking a look on the maven-dependency-plugin?

It's trivial to add it to your project as a dependency (I guess you're managing it by Maven), and on first sight, you should simply call TreeMojo.execute() directly or something like that.

Roughly it does something like this:

ArtifactFilter artifactFilter = createResolvingArtifactFilter();
rootNode = dependencyTreeBuilder.buildDependencyTree( project,
        localRepository, artifactFactory, artifactMetadataSource,
        artifactFilter, artifactCollector );
String dependencyTreeString = serializeDependencyTree( rootNode );
DependencyUtil.log( dependencyTreeString, getLog() );

Is that what you were searching for?

rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • That is EXACTLY what I was looking for. Thank you very much. Unfortunately, it looks like they haven't made the method generic enough to work on any tree model (what I'm actually trying to do is create a call graph of a specified jar file). I'd have to do some copy and paste programming :( – Sridhar Sarnobat Jun 02 '12 at 18:22
  • http://maven.apache.org/plugins/maven-dependency-plugin/xref/org/apache/maven/plugin/dependency/TreeMojo.html – Sridhar Sarnobat Jun 02 '12 at 18:27
  • Glad to hear it helped, gl hf! – rlegendi Jun 03 '12 at 13:12
1

Just in case someone comes here looking for a pure Java library solution - there is text-tree:

<dependency>
  <groupId>org.barfuin.texttree</groupId>
  <artifactId>text-tree</artifactId>
  <version>2.0.0</version>
</dependency>

You just make your tree nodes implement the Node interface, then you can

Node tree = ...;   // your tree
TreeOptions options = new TreeOptions();
options.setStyle(new TreeStyle("+- ", "|  ", "\\- "));
String rendered = TextTree.newInstance(options).render(tree);
System.out.println(rendered);

which produces the tree from your example. This code uses a custom tree style to match your example, but pre-defined tree styles exist.
Full disclosure: I am the author of text-tree. It's free and open source.

barfuin
  • 16,865
  • 10
  • 85
  • 132