I am trying to inspect the SBT dependency tree as described in the documentation:
sbt inspect tree clean
But I get this error:
[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] ^
What is wrong? Why doesn't SBT build the tree?
When run from the command line, each argument sent to sbt is supposed to be a command, so sbt inspect tree clean
will:
- 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:
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".!
}