我想从 URL 下载 zip 文件(我的数据库)并将其解压缩到特定文件夹(例如资源)中。我想在我的项目构建 sbt 文件中做到这一点。
这样做的合适方法是什么?
我知道 sbt.IO 已经解压和下载。我找不到一个使用下载的好例子(我发现那些不起作用)。
是否有任何 sbt 插件可以为我执行此操作?
最佳答案
不清楚什么时候要下载解压,所以我打算用TaskKey
来做.这将创建一个您可以从名为 downloadFromZip
的 sbt 控制台运行的任务。 ,这将只下载 sbt zip 并将其解压缩到临时文件夹:
lazy val downloadFromZip = taskKey[Unit]("Download the sbt zip and extract it to ./temp")
downloadFromZip := {
IO.unzipURL(new URL("https://dl.bintray.com/sbt/native-packages/sbt/0.13.7/sbt-0.13.7.zip"), new File("temp"))
}
如果路径已存在,则可以将此任务修改为仅运行一次:
downloadFromZip := {
if(java.nio.file.Files.notExists(new File("temp").toPath())) {
println("Path does not exist, downloading...")
IO.unzipURL(new URL("https://dl.bintray.com/sbt/native-packages/sbt/0.13.7/sbt-0.13.7.zip"), new File("temp"))
} else {
println("Path exists, no need to download.")
}
}
并让它在编译时运行,将此行添加到
build.sbt
(或 Build.scala
中的项目设置)。compile in Compile <<= (compile in Compile).dependsOn(downloadFromZip)
关于scala - 从 url 下载 zip 并使用 SBT 将其解压缩到资源中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466869/