从 url 下载 zip 并使用 SBT 将其解压缩到资源中 [英] download a zip from url and extract it in resource using SBT

查看:44
本文介绍了从 url 下载 zip 并使用 SBT 将其解压缩到资源中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to download a zip file (my database) from a URL and extract it in specific folder (e.g. resource). I want to do it in my project build sbt file. What would be the appropriate way to do that? I know that sbt.IO has unzip and download. I couldn't find a good example that uses download (those I found were not working). Is there any sbt plugin to do this for me?

推荐答案

不清楚什么时候要下载解压,所以我打算用TaskKey来做.这将创建一个您可以从名为 downloadFromZip 的 sbt 控制台运行的任务,它只会下载 sbt zip 并将其解压缩到一个临时文件夹:

It's not clear when you want to download and extract, so I'm going to do it with a TaskKey. This will create a task you can run from the sbt console called downloadFromZip, which will just download the sbt zip and extract it to a temp folder:

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"))
}

如果路径已经存在,这个任务可以修改为只运行一次:

This task can be modified to only run once if the path already exists:

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 中的项目设置).

And to have it run on compilation, add this line to build.sbt (or project settings in Build.scala).

compile in Compile <<= (compile in Compile).dependsOn(downloadFromZip)

这篇关于从 url 下载 zip 并使用 SBT 将其解压缩到资源中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆