5

To generate a distribution ZIP with Simple Build Tool one can simply do

def distPath = (
  ((outputPath ##) / defaultJarName) +++
  mainDependencies.scalaJars
)
lazy val dist = zipTask(distPath, "dist", "distribution.zip") dependsOn (`package`) describedAs("Zips up the project.")

This adds the JAR files into the root of the ZIP. How does one add the JARs into a lib subfolder in the ZIP?

jelovirt
  • 5,844
  • 8
  • 38
  • 49

1 Answers1

4

For sbt 0.7.x:

Nothing implemented by default as far as I know. However, you can use SBT's FileUtilities.

Try playing around with the following example, which copies your artifact jar into a tmp dir, zips the dir, and deletes it. It should be straightforward to extend it to dependent libs.

class project(info: ProjectInfo) extends DefaultProject(info) {                                                                                                                                                

  def distPath = {                                                                                                                                                                                             
    ((outputPath ##) / defaultJarName) +++ mainDependencies.scalaJars                                                                                                                                          
  }                                                                                                                                                                                                            

  private def str2path(str: String): Path = str                                                                                                                                                                

  lazy val dist = task {                                                                                                                                                                                       
    FileUtilities.copyFile((outputPath ##) / defaultJarName, "tmp" / "lib" / defaultJarName, log)                                                                                                              
    FileUtilities.zip(List(str2path("tmp")), "dist.zip", true, log)                                                                                                                                            
    FileUtilities.clean("tmp", log)                                                                                                                                                                            
    None                                                                                                                                                                                                       
  }                                                                                                                                                                                                            

}                 

The following functions from FileUtilities were used above:

def zip(sources: Iterable[Path], outputZip: Path, recursive: Boolean, log: Logger)                                                                                                                                                                                                         
def copyFile(sourceFile: Path, targetFile: Path, log: Logger): Option[String]
def clean(file: Path, log: Logger): Option[String]

Their declarations should be self-explanatory.

axel22
  • 32,045
  • 9
  • 125
  • 137
  • I'm new to SBT and can't find FileUtilities. Was it removed or am I looking in the wrong place? It doesn't appear to be in the scaladoc http://harrah.github.com/xsbt/latest/api/sbt/package.html – Ben McCann Mar 15 '12 at 01:09
  • Times change, and so does SBT. There have been some major changes between sbt 0.7 and 0.10., so the above might not work anymore. – axel22 Mar 15 '12 at 08:03
  • @BenMcCann object sbt.IO now contains the methods that used to be in FileUtilities. – Phil Harvey Oct 11 '13 at 13:16