You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sbt项目中如何获取带scope的全量依赖列表(含传递依赖)

sbt项目获取带scope标识的全量传递依赖列表方法

方法1:临时查询无需修改项目配置

直接在项目根目录下启动sbt shell,执行以下命令即可直接输出符合要求格式的结果:

eval Seq(Compile, Provided, Test).flatMap(conf => (conf / fullClasspath).value.map(f => s"${f.data.getName}(${conf.name})")).distinct.foreach(println)

说明:返回结果默认已经包含所有传递依赖,distinct用于过滤同一个jar在多个scope重复出现的条目,不需要去重则可删除该关键字

方法2:添加自定义任务永久可用

如果需要频繁查询,可将任务配置到build.sbt中:

  • 打开项目根目录的build.sbt文件,添加如下代码:
// 新增打印带scope的全量依赖任务
lazy val printAllDependenciesWithScope = taskKey[Unit]("输出所有含传递依赖、带scope标识的依赖列表")
printAllDependenciesWithScope := {
  // 可按需增减你需要查询的scope列表
  val scopeList = List(
    Compile -> "Compile",
    Provided -> "Provided",
    Test -> "Test"
  )
  scopeList.foreach { case (conf, scopeName) =>
    (conf / fullClasspath).value.foreach { fileAttr =>
      println(s"${fileAttr.data.getName}($scopeName)")
    }
  }
}
  • 重启sbt shell后,执行以下命令即可得到输出:
printAllDependenciesWithScope
  • 如果需要将结果导出到本地文件,可直接执行:
sbt printAllDependenciesWithScope > dependency_list.txt

内容的提问来源于stack exchange,提问作者partoneplay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 21:06:02