Create a zip with no root directory with universal sbt-native-packager

1.1k views Asked by At

I use universal sbt-native-packager to generate a zip file distribution.

sbt universal:packageBin

The generated zip file, once extracted, contains everything inside a main directory named as my zip file:

unzip my-project-0.0.1.zip

my-project-0.0.1/bin
my-project-0.0.1/lib
my-project-0.0.1/conf
...

How can I create a zip that has no root folder, so that when extracted it will have a structure like that?

bin
lib
conf

Thanks

2

There are 2 answers

0
Paolo Rascunà On BEST ANSWER

I'm not confident enough with sbt and scala to submit a pull request. bash scripting has to be excluded right now, so my current (and ugly) solution is this one:

packageBin in Universal := { val originalFileName = (packageBin in Universal).value val (base, ext) = originalFileName.baseAndExt val newFileName = file(originalFileName.getParent) / (base + "_dist." + ext) val extractedFiles = IO.unzip(originalFileName,file(originalFileName.getParent)) val mappings: Set[(File, String)] = extractedFiles.map( f => (f, f.getAbsolutePath.substring(originalFileName.getParent.size + base.size + 2))) val binFiles = mappings.filter{ case (file, path) => path.startsWith("bin/")} for (f <- binFiles) f._1.setExecutable(true) ZipHelper.zip(mappings,newFileName) IO.move(newFileName, originalFileName) IO.delete(file(originalFileName.getParent + "/" + originalFileName.base)) originalFileName }

The solution proposed on github seems to be way nicer than mine even tough it doesn't work for me: https://github.com/sbt/sbt-native-packager/issues/276

0
millhouse On

Unfortunately it looks like the name of that top-level directory is fixed to be the same as the name of the distributable zip (check out line 24 of the ZipHelper source on GitHub).

So unless you feel like making it configurable and submitting a pull request, it might just be easier to modify the resulting zip on the command line (assuming some flavour of UNIX):

unzip my-project-0.0.1.zip && cd my-project-0.0.1 && zip -r ../new.zip * && cd -

This will create new.zip alongside the existing zipfile - you could then mv it over the top if you like.