问题 从url下载zip并使用SBT在资源中提取它


我想从URL下载zip文件(我的数据库)并将其提取到特定文件夹(例如资源)中。我想在我的项目构建sbt文件中执行此操作。 这样做的适当方法是什么? 我知道sbt.IO已经解压缩并下载了。我找不到一个使用下载的好例子(那些我发现不起作用)。 有什么sbt插件可以帮我吗?


8572
2017-12-14 06:20


起源



答案:


目前还不清楚你何时想要下载和提取,所以我打算用它来做 TaskKey。这将创建一个可以从被调用的sbt控制台运行的任务 downloadFromZip,它将下载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)

11
2017-12-15 17:24



非常感谢,它正在做我需要的,是否可以在编译时运行此任务?如果文件夹不存在那么下载提取,如果存在则不运行任务? - Omid
@Omid是的。看我的编辑。 - Michael Zajac


答案:


目前还不清楚你何时想要下载和提取,所以我打算用它来做 TaskKey。这将创建一个可以从被调用的sbt控制台运行的任务 downloadFromZip,它将下载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)

11
2017-12-15 17:24



非常感谢,它正在做我需要的,是否可以在编译时运行此任务?如果文件夹不存在那么下载提取,如果存在则不运行任务? - Omid
@Omid是的。看我的编辑。 - Michael Zajac