sbt:如何通过Scala代码获取依赖jar文件列表



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

在Gradle中,它支持通过Java代码获取依赖的jar文件,如下所示(project是Project类的一个实例(:

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

我想知道在sbt和Scala中如何做到这一点。有人知道吗?我试着使用sbt.Project#dependencies,但似乎没有达到这个目的。

您正在寻找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]]的Task,其中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

最新更新