【问题标题】:sbt: How to get dependent jar files list by Scala codesbt:如何通过 Scala 代码获取依赖的 jar 文件列表
【发布时间】:2020-04-09 19:21:46
【问题描述】:

我是 sbt 的新手。我想知道如何通过Scala代码获取依赖的jar文件,而不是执行sbt插件。

Gradle支持通过Java代码获取依赖的jar文件如下(projectProject类的一个实例):

Configuration config = project.getRootProject().getBuildscript().getConfigurations().detachedConfiguration();
Set<File> jars = config.resolve();

我想知道在 sbt 和 Scala 中这样做的方法。有人知道吗?我尝试使用sbt.Project#dependencies,但似乎不符合此目的。

【问题讨论】:

    标签: scala sbt


    【解决方案1】:

    您正在寻找dependencyClasspathAsJars:

    sbt > inspect dependencyClasspathAsJars
    
    [info] Task: scala.collection.Seq[sbt.internal.util.Attributed[java.io.File]]
    [info] Description:
    [info]  The classpath consisting of internal and external, managed and unmanaged dependencies, all as JARs.
    [info] Provided by:
    [info]  ProjectRef(uri("file:/home/claudio/foo"), "foo") / Compile / dependencyClasspathAsJars
    [info] Defined at:
    [info]  (sbt.Classpaths.classpaths) Defaults.scala:1800
    [info] Dependencies:
    ...
    

    如您所见,这是一个返回 scala.collection.Seq[sbt.internal.util.Attributed[java.io.File]] 的任务,其中 Attributed 只是对任意数据的简单包装:https://www.scala-sbt.org/1.x/api/sbt/internal/util/Attributed.html

    sbt > show dependencyClasspathAsJars
    
    [info] List(Attributed(/home/claudio/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.13.1.jar),                                  
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/twirl-api_2.13/jars/twirl-api_2.13-1.5.0.jar),                                          
      Attributed(/home/claudio/.ivy2/cache/org.scala-lang.modules/scala-xml_2.13/bundles/scala-xml_2.13-1.2.0.jar),                                  
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-server_2.13/jars/play-server_2.13-2.8.1.jar),                                      
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play_2.13/jars/play_2.13-2.8.1.jar),                                                    
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/build-link/jars/build-link-2.8.1.jar),                                                  
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-exceptions/jars/play-exceptions-2.8.1.jar),                                        
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-streams_2.13/jars/play-streams_2.13-2.8.1.jar),                                    
      Attributed(/home/claudio/.ivy2/cache/org.reactivestreams/reactive-streams/jars/reactive-streams-1.0.3.jar),                                    
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.akka/akka-stream_2.13/jars/akka-stream_2.13-2.6.3.jar),                                      
      Attributed(/home/claudio/.ivy2/cache/com.typesafe.akka/akka-actor_2.13/jars/akka-actor_2.13-2.6.3.jar),                                        
      Attributed(/home/claudio/.ivy2/cache/com.typesafe/config/bundles/config-1.4.0.jar)
      ...)
    

    如果您想以任何方式处理该值,您可能需要编写一个自定义任务:https://www.scala-sbt.org/1.x/docs/Tasks.html

    【讨论】:

    • 非常感谢!我可以在我的自定义任务中使用 (dependencyClasspathAsJars in Compile).value 来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2012-02-01
    • 2017-04-28
    • 2019-05-22
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多