Print plain text tree from tree data structure (ja

2019-05-21 03:04发布

问题:

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?

回答1:

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?