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
相关产品推荐
相关产品推荐

