如何在 sbt 中看到依赖树?

我正在尝试检查 文件中描述的 SBT 依赖树:

sbt inspect tree clean

但我得到了这个错误:

[error] inspect usage:
[error]   inspect [uses|tree|definitions] <key>   Prints the value for 'key', the defining scope, delegates, related definitions, and dependencies.
[error]
[error] inspect
[error]        ^

怎么了? 为什么 SBT 不构建树?

99140 次浏览

When run from the command line, each argument sent to sbt is supposed to be a command, so sbt inspect tree cleanwill:

  • run the inspect command,
  • then run the tree command,
  • then the clean command

This obviously fails, since inspect needs an argument. This will do what you want:

sbt "inspect tree clean"

If you want to actually view the library dependencies (as you would with Maven) rather than the task dependencies (which is what inspect tree displays), then you'll want to use the sbt-dependency-graph plugin.

Add the following to your project/plugins.sbt (or the global plugins.sbt).

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")

Then you have access to the dependencyTree command, and others.

If you want to view library dependencies, you can use the coursier plugin: https://github.com/coursier/coursier/blob/master/doc/FORMER-README.md#printing-trees

Output example: image text (without colors): https://gist.github.com/vn971/3086309e5b005576533583915d2fdec4

Note that the plugin has a completely different nature than printing trees. It's designed for fast and concurrent dependency downloads. But it's nice and can be added to almost any project, so I think it's worth mentioning.

I tried using "net.virtual-void" % "sbt-dependency-graph" plugin mentioned above and got 9K lines as the output(there are many empty lines and duplicates) in comparison to ~180 lines(exactly one line for each dependency in my project) as the output in Maven's mvn dependency:tree output. So I wrote a sbt wrapper task for that Maven goal, an ugly hack but it works:

// You need Maven installed to run it.
lazy val mavenDependencyTree = taskKey[Unit]("Prints a Maven dependency tree")
mavenDependencyTree := {
val scalaReleaseSuffix = "_" + scalaVersion.value.split('.').take(2).mkString(".")
val pomXml =
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<dependencies>
{
libraryDependencies.value.map(moduleId => {
val suffix = moduleId.crossVersion match {
case binary: sbt.librarymanagement.Binary => scalaReleaseSuffix
case _ => ""
}
<dependency>
<groupId>{moduleId.organization}</groupId>
<artifactId>{moduleId.name + suffix}</artifactId>
<version>{moduleId.revision}</version>
</dependency>
})
}
</dependencies>
</project>


val printer = new scala.xml.PrettyPrinter(160, 2)
val pomString = printer.format(pomXml)


val pomPath = java.nio.file.Files.createTempFile("", ".xml").toString
val pw = new java.io.PrintWriter(new File(pomPath))
pw.write(pomString)
pw.close()


println(s"Formed pom file: $pomPath")


import sys.process._
s"mvn -f $pomPath dependency:tree".!
}

With sbt 1.4.0, dependencyTree task is available in sbt without using plugins.

sbt dependencyTree

sbt-dependency-graph is included in sbt 1.4.0: https://www.scala-sbt.org/1.x/docs/sbt-1.4-Release-Notes.html#sbt-dependency-graph+is+in-sourced

Full strength dependency tree can be enabled by adding addDependencyTreePlugin to project/plugins.sbt.

List of available commands: https://github.com/sbt/sbt-dependency-graph#usage-instructions

This worked for me. Reference here For sbt < 1.3 use:

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1")

and then

sbt compile:dependencyTree