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?
Asked
Active
Viewed 3,925 times
1 Answers
15
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)

Michael Zajac
- 55,144
- 7
- 113
- 138
-
Thanks a lot, It's doing what I need , Is it possible to run this task in compile time? if the file folder was not there then download-extract and if it exist don't run the task? – Omid Dec 15 '14 at 18:53
-
This would probably motivate the creation of a plugin (if none exists) that handles the whole process: checksum, download, extract. – 6infinity8 Aug 21 '20 at 16:18