sbt dependencyTree
概要
sbt dependencyTree [subcommand] [options]
sbt dependencyTree list [options]
sbt dependencyTree graph [options]
sbt dependencyTree html [options]
sbt dependencyTree html-graph [options]
sbt dependencyTree json [options]
sbt dependencyTree xml [options]
sbt dependencyTree stats [options]
描述
dependencyTree 任务以多种格式显示您项目的依赖图,帮助您可视化和分析传递依赖。由 DependencyTreePlugin 提供,该插件在 sbt 1.4.0 中从 Johannes Rudolph 的 sbt-dependency-graph 内源化而来。该任务在生成输出前会解析依赖,因此必要时可能触发下载。
用法
在 sbt shell 中运行。默认子命令为 tree,输出 ASCII 树形图。
子命令
tree(默认):输出依赖的 ASCII 树形图,显示层级关系。适用于快速概览。list:输出所有依赖的扁平列表,每行一个。适用于脚本或 grep。graph/dot:输出用于可视化的 GraphViz DOT 文件。提供dot别名以保持兼容。html:创建以文本形式渲染依赖图的独立 HTML 页面。html-graph:创建嵌入 GraphViz 图的 HTML 页面(系统需安装 GraphViz)。json:以 JSON 格式输出依赖,便于程序化使用。xml:以 GraphML 格式输出依赖,适用于图分析工具。stats:输出汇总统计,如依赖总数和版本数。help:显示用法帮助(与不带参数运行dependencyTree相同)。
选项
--quiet:抑制控制台输出,将结果作为任务值返回(例如供其他任务使用)。--out <file>:将输出写入指定文件而非 stdout。文件扩展名决定默认子命令:.txt:tree.dot:graph.html:html.json:json.xml:xml
--browse:在默认浏览器中自动打开输出文件(仅对graph或html子命令有效)。
示例
ASCII 树形图(默认)
显示依赖的层级视图,[S] 表示 Scala 库依赖。
> Compile/dependencyTree
[info] default:example_3:0.1.0-SNAPSHOT
[info] +-org.scala-lang:scala3-library_3:3.3.1 [S]
[info] +-com.example:library_3:1.0.0
[info] +-org.typelevel:cats-core_3:2.9.0
[info] | +-org.typelevel:cats-kernel_3:2.9.0
[info] | +-org.scala-lang:scala3-library_3:3.1.3 (evicted by: 3.3.1)
[info] +-org.typelevel:cats-effect_3:3.4.0
被驱逐的依赖(被较新版本替换的旧版本)显示在括号中。
依赖列表
> Compile/dependencyTree list
org.scala-lang:scala3-library_3:3.3.1
com.example:library_3:1.0.0
org.typelevel:cats-core_3:2.9.0
org.typelevel:cats-kernel_3:2.9.0
org.typelevel:cats-effect_3:3.4.0
GraphViz DOT 文件
生成用于渲染图的 DOT 文件(例如使用 dot 命令)。
> Compile/dependencyTree graph --out dependencies.dot
dependencies.dot 内容:
digraph "dependency-graph" {
graph[rankdir="LR"; splines=polyline]
edge [arrowtail="none"]
"default:example_3:0.1.0-SNAPSHOT" -> "org.scala-lang:scala3-library_3:3.3.1"
"default:example_3:0.1.0-SNAPSHOT" -> "com.example:library_3:1.0.0"
"com.example:library_3:1.0.0" -> "org.typelevel:cats-core_3:2.9.0"
// ... more edges
}
渲染为 PNG:dot -Tpng dependencies.dot -o dependencies.png
HTML 输出
> Compile/dependencyTree html --out deps.html --browse
创建包含文本图的 deps.html 并在浏览器中打开。
嵌入图的 HTML
> Compile/dependencyTree html-graph --browse
需要安装 GraphViz。在 HTML 中嵌入可视化图。
JSON 输出
> Compile/dependencyTree json --out deps.json
示例输出:
{
"organization": "default",
"name": "example_3",
"version": "0.1.0-SNAPSHOT",
"dependencies": [
{
"organization": "org.scala-lang",
"name": "scala3-library_3",
"version": "3.3.1",
"configurations": ["compile"],
"evicted": false
},
// ... more
]
}
XML (GraphML) 输出
> Compile/dependencyTree xml --out deps.graphml
适用于导入 yEd 等图可视化工具。
统计
> Compile/dependencyTree stats
Total dependencies: 15
Unique organizations: 5
Versions: 10 distinct
Evictions: 2
配置键
在 build.sbt 中使用以下设置自定义行为:
dependencyTreeIncludeScalaLibrary := true:在输出中包含 Scala 库依赖(默认:false)。dependencyDotNodeColors := false:禁用 DOT 节点标签中的颜色(默认:true)。dependencyDotNodeLabel := (org: String, name: String, version: String) => s"$org:$name:$version":自定义 DOT 节点标签。dependencyDotHeader := """digraph "custom" { rankdir="TB"; }""":设置自定义 DOT 头部。
相关任务
whatDependsOn <module>:显示哪些模块依赖指定模块(例如whatDependsOn org.example:lib:1.0)。dependencyLicenseInfo:显示依赖的许可证信息。
Scopes
在 Compile 和 Test 配置中可用。如需跨配置视图,使用 Global/。