120

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?

1

8 Answers 8

170

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.

9
  • 21
    For library dependency info, there are also the detailed dependency reports generated by Ivy. After doing sbt update, open the XML reports in target/resolution-cache/reports/ in a browser like MSIE.
    – Rich
    Jan 4, 2017 at 11:54
  • 17
    no way without introducing a library? i start to miss maven :(
    – Jas
    Oct 1, 2017 at 8:43
  • 5
    @Jas Even Maven accomplishes this through a plugin, its just that maven grabs the plugin (org.apache.maven.plugins:maven-dependency-plugin) automatically when you run dependency:tree
    – JMess
    Oct 5, 2017 at 21:38
  • 1
    @DavidRabinowitz technically it doesn't answer the question, but most people who find this question actually have a different one.
    – OrangeDog
    Sep 19, 2019 at 16:07
  • 5
    For sbt 1.0 and up, you need this version instead: addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1") Or have a look at github.com/jrudolph/sbt-dependency-graph for the latest version and instructions.
    – silverbeak
    Mar 18, 2020 at 11:16
99

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:

sbt 1.4.0 brings in Johannes Rudolph’s sbt-dependency-graph plugin into the code base. Since it injects many tasks per subprojects, the plugin is split into two parts:

  • MiniDependencyTreePlugin that is enabled by default, bringing in dependencyTree task to Compile and Test configurations
  • Full strength DependencyTreePlugin that is enabled by putting the following to project/plugins.sbt:
addDependencyTreePlugin

See old README for the list of available tasks.

6
  • 9
    Thanks! Since sbt 1.4, this is the correct answer. All other answers are more or less outdated. (SO should provide a way to tag answers as outdated or something like that...) Dec 21, 2020 at 23:13
  • Based on the output it seems to have a dependency on JDK 11 [info] welcome to sbt 1.4.0 (Red Hat, Inc. Java 11.0.9). Not bad a thing, but is good to keep in mind.
    – vasigorc
    Apr 11, 2021 at 21:40
  • 1
    It's very annoying that they haven't included all comments in sbt though, I would like to be able to run dependencyBrowseTree, but seem to had to add the whole plugin to do that. May 11, 2021 at 12:40
  • Agree. This should be correct answer for SBT 1.4+ Jul 7, 2021 at 10:54
  • How to find dependencyTree of a specific jar file? May 26, 2023 at 10:15
90

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"
7
  • 29
    What it shows library dependencies?? Dont think so.
    – prayagupa
    Oct 15, 2016 at 5:56
  • 2
    It is working for me, First, I moved to sbt console and run the inspect tree clean command. I got big tree of dependencies.
    – Sun
    Oct 19, 2016 at 12:17
  • 2
    I tried this, It giving bit tree of things. But I did not fine any external libraries. ie I search for Akka in the output, I did not show any occurrence of Akka in the console. It showing some useless tree. It having all about my local and dependent modules and details only. This command useless. Please help me any command than this nonsense please.
    – Sun
    Mar 15, 2017 at 9:57
  • 3
    @Sun: see the second answer for how to get the maven/ivy library dependencies, which is what you are looking for. The inspect tree <foo> command is about inspecting the dependencies of the task <foo> in sbt's task & setting system, which is sometimes useful, but completely different. Mar 15, 2017 at 13:34
  • 4
    It prints something like a tree. shouldn't be the accepted answer Feb 13, 2018 at 7:04
23

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.

1
  • @mjjaniec thanks! I'll use it for now. By the time it changes, hopefully the web site will have a proper/stabilized entry for this. Nov 28, 2018 at 8:51
5

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".!
}
1
  • your code is really helpful, BTW if anyone trying to run this on windows then please do not forget to write the absolute path of mvn.cmd instead of only writing mvn
    – nomadSK25
    Jun 29, 2020 at 8:27
3

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
2

You can use intellij-sbt-dependency-analyzer plugin in IDEA now.

0
1

Starting with sbt 1.4 add to your project/plugins.sbt the following line as such:

addDependencyTreePlugin

then you have those features ready:

$ sbt dependencyTree
$ sbt dependencyBrowseTree
$ sbt dependencyBrowseGraph
... 

the latter two have graphical output in the browser, the ...Tree one has incremental search capabilities.

( The list of all tasks is still mentioned here even it is yet archived )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.