Cherry
Cherry

Reputation: 33544

How to see dependency tree in sbt?

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?

Upvotes: 123

Views: 116628

Answers (8)

IceMimosa
IceMimosa

Reputation: 41

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

Upvotes: 2

Hartmut Pfarr
Hartmut Pfarr

Reputation: 6139

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 )

Upvotes: 3

TheKojuEffect
TheKojuEffect

Reputation: 21081

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.

Upvotes: 107

Runjhun
Runjhun

Reputation: 45

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

Upvotes: 3

OrangeDog
OrangeDog

Reputation: 38777

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.

Upvotes: 170

VasiliNovikov
VasiliNovikov

Reputation: 10236

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.

Upvotes: 24

MaxNevermind
MaxNevermind

Reputation: 2923

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

Upvotes: 5

gourlaysama
gourlaysama

Reputation: 11280

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"

Upvotes: 91

Related Questions