[英]Print plain text tree from tree data structure (java)
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.并希望将外观相似的树作为纯 ascii 文本打印为我的 java 程序的输出。
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.我看到的最接近的替代品是: http : //code.google.com/p/j-text-utils/但它不如 Maven 好。
Where can I find a library that prints a tree structure as text almost identically to mvn dependency:tree?我在哪里可以找到一个库,该库将树结构打印为与 mvn dependency:tree 几乎相同的文本?
I'm not an expert of creating/using MOJOs, but how about downloading and taking a look on the maven-dependency-plugin ?我不是创建/使用 MOJO 的专家,但是下载并查看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.将它作为依赖项添加到您的项目中是微不足道的(我猜您是由 Maven 管理它的),乍一看,您应该直接调用TreeMojo.execute()或类似的东西。
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?这就是你要找的吗?
Just in case someone comes here looking for a pure Java library solution - there is text-tree :以防万一有人来这里寻找纯 Java 库解决方案 - 有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接口,然后你就可以
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.
它是免费和开源的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.