scala 如何在 sbt 中查看依赖树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25519926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to see dependency tree in sbt?
提问by Cherry
I am trying to inspect the SBT dependency tree as described in the documentation:
我正在尝试按照文档中的描述检查 SBT 依赖关系树:
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?
怎么了?为什么 SBT 不构建树?
采纳答案by gourlaysama
When run from the command line, each argument sent to sbt is supposed to be a command, so sbt inspect tree cleanwill:
当从命令行运行时,发送到 sbt 的每个参数都应该是一个命令,因此sbt inspect tree clean:
- run the
inspectcommand, - then run the
treecommand, - then the
cleancommand
- 运行
inspect命令, - 然后运行
tree命令, - 然后
clean命令
This obviously fails, since inspectneeds an argument. This will do what you want:
这显然失败了,因为inspect需要争论。这将执行您想要的操作:
sbt "inspect tree clean"
回答by OrangeDog
If you want to actually view the library dependencies (as you would with Maven) rather than the task dependencies (which is what inspect treedisplays), then you'll want to use the sbt-dependency-graphplugin.
如果您想实际查看库依赖项(就像使用 Maven 一样)而不是任务依赖项(inspect tree显示的内容),那么您需要使用sbt-dependency-graph插件。
Add the following to your project/plugins.sbt (or the global plugins.sbt).
将以下内容添加到您的项目/plugins.sbt(或全局 plugins.sbt)。
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
Then you have access to the dependencyTreecommand, and others.
然后您就可以访问dependencyTree命令和其他命令。
回答by VasiliNovikov
If you want to view library dependencies, you can use the coursierplugin: https://github.com/coursier/coursier/blob/master/doc/FORMER-README.md#printing-trees
如果要查看库依赖,可以使用coursier插件:https: //github.com/coursier/coursier/blob/master/doc/FORMER-README.md#printing-trees
Output example:
text (without colors): https://gist.github.com/vn971/3086309e5b005576533583915d2fdec4
输出示例:
文本(无颜色):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.
请注意,该插件与打印树具有完全不同的性质。它专为快速并发的依赖下载而设计。但是它很好,几乎可以添加到任何项目中,所以我认为值得一提。
回答by MaxNevermind
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:treeoutput. So I wrote a sbt wrapper taskfor that Maven goal, an ugly hack but it works:
我尝试使用"net.virtual-void" % "sbt-dependency-graph"上面提到的插件并得到 9K 行作为输出(有很多空行和重复行),而 Maven 输出中的mvn dependency:tree输出为 ~180 行(我项目中的每个依赖项正好一行)。所以我为那个 Maven 目标写了一个 sbt 包装器任务,一个丑陋的 hack 但它有效:
// 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".!
}

