【问题标题】:download a zip from url and extract it in resource using SBT从 url 下载 zip 并使用 SBT 将其提取到资源中
【发布时间】:2015-02-12 12:50:40
【问题描述】:

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

【问题讨论】:

    标签: scala sbt


    【解决方案1】:

    不清楚何时要下载和解压,所以我将使用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)
    

    【讨论】:

    • 非常感谢,它正在做我需要的,是否可以在编译时运行此任务?如果文件夹不存在,则下载-解压,如果存在则不运行任务?
    • 这可能会激发创建一个插件(如果不存在)来处理整个过程:校验和、下载、提取。
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 2011-01-07
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多