tutorial on viewing the dependency tree for a SBT project

Source: Internet
Author: User
Tags chmod

SBT is using Ivy to manage project dependencies, like the Maven project where you can use Dependency:tree to show the dependency tree, so how do you view project dependencies for a SBT project? This article mentions three ways to show project dependencies, which are Shell scripts, custom SBT tasks, and Sbt-dependency-plugin methods. The final approach allows us to use Dependencytree to show the dependency:tree effect of Maven, as well as the cooler.

> Dependencytree
[INFO] default:test_2.10:0.1-snapshot [S]
[INFO] +-ch.qos.logback:logback-classic:1.0.13
[INFO] +-ch.qos.logback:logback-core:1.0.13
[INFO] +-org.slf4j:slf4j-api:1.7.5
[INFO]
[Success] Total time:0 S, completed APR 5, 2016 12:29:53 AM

Here is the whole process of exploration.

Automatically through the tab of the SBT console or use the Help. *[dd]ependenc.* command to further filter out the dependencies closer to the SBT console task

Dependencyclasspath the classpath consisting of internal and external, managed unmanaged and dependencies.
Externaldependencyclasspath the classpath consisting of the library dependencies, both managed and unmanaged.
Internaldependencyclasspath the internal (inter-project) classpath.
Projectdependencies inter-project dependencies.
Excludedependencies declares managed dependency exclusions.
Dependencycachedirectory the base directory for cached dependencies.
Alldependencies Inter-project and library dependencies.
Librarydependencies declares managed dependencies.
Dependencyoverrides declares managed dependency overrides.
Trackinternaldependencies the level of tracking for the internal (inter-project) dependency.
Dependencypositions Source positions where the dependencies are defined.

To drill down, we build a simple project with the file directory as follows

Test
├──build.sbt
└──lib
└──guava-18.0.jar

The contents of the BUILD.SBT file are

Librarydependencies ++= Seq (
"Ch.qos.logback"% "logback-classic"% "1.0.13"
)

Let's look at the output of SBT's Dependencyclasspath, Externaldependencyclasspath, and Internaldependencyclasspath.

> Show Dependencyclasspath
[INFO] List (Attributed (/users/uqiu/test/lib/guava-18.0.jar), attributed (/users/uqiu/.sbt/boot/scala-2.10.6/lib/ Scala-library.jar), attributed (/users/uqiu/.ivy2/cache/ch.qos.logback/logback-classic/jars/ Logback-classic-1.0.13.jar), attributed (/users/uqiu/.ivy2/cache/ch.qos.logback/logback-core/jars/ Logback-core-1.0.13.jar), attributed (/users/uqiu/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.7.5.jar))
[Success] Total time:0 S, completed APR 4, 2016 11:54:57 PM
> Show Externaldependencyclasspath
[INFO] List (Attributed (/users/uqiu/test/lib/guava-18.0.jar), attributed (/users/uqiu/.sbt/boot/scala-2.10.6/lib/ Scala-library.jar), attributed (/users/uqiu/.ivy2/cache/ch.qos.logback/logback-classic/jars/ Logback-classic-1.0.13.jar), attributed (/users/uqiu/.ivy2/cache/ch.qos.logback/logback-core/jars/ Logback-core-1.0.13.jar), attributed (/users/uqiu/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.7.5.jar))
[Success] Total time:0 S, completed APR 4, 2016 11:55:03 PM
> Show Internaldependencyclasspath
[INFO] List ()
[Success] Total time:0 S, completed APR 4, 2016 11:55:10 PM
We don't define subprojects here, so Internaldependencyclasspath is empty, Dependencyclasspath and Externaldependencyclasspath are the same.

In general we are concerned with the content of the Externaldependencyclasspath, which shows the content of the list[attribute[string] type, which, for readability, can be formatted with a Shell script or custom SBT task.

The Shell script shows that the dependencies.sh is created in the project directory, which reads as follows:

#!/bin/bash

echo "Direct dependencies"
SBT ' Show all-dependencies ' | Gawk ' Match ($,/list\ ((. *) \)/, a) {print a[1]} ' | Tr-d ' | Tr ', ' \ n ' | Sort-t ': ' | \
Tr ': ' \ t ' | Expand-t 30

Echo-e "\nall dependencies, including transitive dependencies"
SBT ' Show Managed-classpath ' | Tr-d ' | Tr ', ' \ n ' | Gawk ' Match ($,/attributed\ ((. *) \)/, a) {print a[1]} ' | \
Tr-d ' () ' | Sed "s^ $HOME/.ivy2/cache/^^ g" | Sed "s^/jars^^" | \
Gawk-f/' {print $, $} ' | Sort | Tr ' ' \ t ' | Expand-t 30
Install gawk under MAC, use the command brew install gawk installation, and chmod +x dependencies.sh plus executable properties, complete with the following command:

Brew Install Gawk
chmod +x dependencies.sh
./dependencies.sh
The results of the display are roughly as follows:

Direct dependencies
Ch.qos.logback Logback-classic 1.0.13
Optional (default)
Optional (default)
Org.scala-lang Scala-compiler 2.10.6 Scala-tool->default
Org.scala-lang scala-library 2.10.6
Org.scala-lang scala-library 2.10.6 Scala-tool->default

All dependencies, including transitive dependencies
Uqiu
Ch.qos.logback Logback-classic-1.0.13.jar
Ch.qos.logback Logback-core-1.0.13.jar
ORG.SLF4J Slf4j-api-1.7.5.jar
Customize the SBT task by adding the following in BUILD.SBT:

This article original link http://unmi.cc/show-sbt-dependency-tree/, from Yehuang Unmi Blog
Lazy val versionreport = taskkey[string] ("Version-report")

Add This setting to your project.
Versionreport <<= (Externaldependencyclasspath in Compile, streams) map {
(Cp:seq[attributed[file]], streams) =>
Val Cp.map = {
Attributed =>
Attributed.get (Keys.moduleID.key) match {
Case Some (ModuleID) => "%40s%20s%10s". Format (
Moduleid.organization,
Moduleid.name,
Moduleid.revision,
ModuleId.configurations.getOrElse ("")
)
Case None =>
unmanaged JAR, just
Attributed.data.getAbsolutePath
}
}.mkstring ("\ n")
Streams.log.info (The)
The
}
After you rerun SBT or reload perform the Versionreport task, the output is as follows:

> Versionreport
[INFO]/users/uqiu/test/lib/guava-18.0.jar
[INFO] Org.scala-lang scala-library 2.10.6
[INFO] ch.qos.logback logback-classic 1.0.13
[INFO] ch.qos.logback Logback-core 1.0.13
[INFO] org.slf4j Slf4j-api 1.7.5
This allows us to see all the dependencies, but not the tree graph, has not yet reached the goal described in the title of this article, so the ultimate solution is also the simplest way is not to reinvent the wheel, using the existing Plug-ins https://github.com/jrudolph/ Sbt-dependency-graph.

SBT Plug-in Display dependency tree

You can add a row to a new PROJECT/PLUGINS.SBT in a ~/.SBT/0.13/PLUGINS/PLUGINS.SBT or project

Addsbtplugin ("net.virtual-void"% "sbt-dependency-graph"% "0.8.2")
At this point, a lot of tasks were added under the SBT console,

Dependencytree:shows an ASCII tree representation of the project ' s dependencies
Dependencybrowsegraph:opens a browser window with a visualization of the dependency graph (courtesy of Graphlib-dot + dag RE-D3).
Dependencygraph:shows an ASCII graph of the project's dependencies on the SBT console
Dependencylist:shows A flat list of all transitive dependencies on the SBT console (sorted by organization and name)
Whatdependson <organization> <module> <revision&gt: Find out what depends on a artifact. Shows a reverse dependency for the selected module.
Dependencylicenseinfo:show dependencies grouped by declared license
Dependencystats:shows a table with the each module a row with (transitive) Jar sizes and number of dependencies
Dependencygraphml:generates A. graphml file with the project's dependencies to Target/dependencies-<config>. Graphml. Use e.g. yed to format the graph to your needs.
Dependencydot:generates A. dot file with the project's dependencies to Target/dependencies-<config>.dot. Use Graphviz to render it to your preferred graphic format.
Ivyreport:let ' s ivy generate the resolution for your project. Use show Ivyreport for the filename of the generated
I'm more interested in Dependencytree (showing the dependency:tree like Maven) and Dependencybrowsegraph (open the browser to display a dependency diagram)

> Dependencytree
[INFO] default:test_2.10:0.1-snapshot [S]
[INFO] +-ch.qos.logback:logback-classic:1.0.13
[INFO] +-ch.qos.logback:logback-core:1.0.13
[INFO] +-org.slf4j:slf4j-api:1.7.5
[INFO]
[Success] Total time:0 S, completed APR 5, 2016 12:29:53 AM
or open a browser after executing dependencybrowsegraph

There are dependencygraph tasks output as text graphics, Dependencydot can generate dot files, and so on.

Using the Sbt-dependency-graph plug-in is the most powerful and simple, and if you load the plug-in in ~/.SBT/0.13/PLUGINS/PLUGINS.SBT it is once and for all.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.