Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download a zip from url and extract it in resource using SBT

Tags:

scala

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?

like image 777
Omid Avatar asked Dec 14 '14 06:12

Omid


1 Answers

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

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)
like image 54
Michael Zajac Avatar answered Oct 19 '22 09:10

Michael Zajac