How to see dependency tree in sbt?

ScalaDependenciesSbt

Scala Problem Overview


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?

Scala Solutions


Solution 1 - Scala

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.

Solution 2 - Scala

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"

Solution 3 - Scala

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

Solution 4 - Scala

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.

Solution 5 - Scala

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".!
}

Solution 6 - Scala

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCherryView Question on Stackoverflow
Solution 1 - ScalaOrangeDogView Answer on Stackoverflow
Solution 2 - ScalagourlaysamaView Answer on Stackoverflow
Solution 3 - ScalaTheKojuEffectView Answer on Stackoverflow
Solution 4 - ScalaVasiliNovikovView Answer on Stackoverflow
Solution 5 - ScalaMaxNevermindView Answer on Stackoverflow
Solution 6 - ScalaRunjhunView Answer on Stackoverflow