2012-11-20 73 views
5

我感興趣的是構建未壓縮的jar文件,以便在只有幾個類更改時使我的rsync更快,到目前爲止,我無法弄清楚如何告訴sbtassembly禁用壓縮。如何使用sbtassembly在sbt中禁用jar壓縮?

server > inspect assembly 
[info] Task: java.io.File 
[info] Description: 
[info] Builds a single-file deployable jar. 
[info] Provided by: 
[info] {file:/.../}server/*:assembly 
[info] Dependencies: 
[info] server/*:assembly-merge-strategy(for assembly) 
[info] server/*:assembly-output-path(for assembly) 
[info] server/*:package-options(for assembly) 
[info] server/*:assembly-assembled-mappings(for assembly) 
[info] server/*:cache-directory 
[info] server/*:test(for assembly) 
[info] server/*:streams(for assembly) 
[info] Delegates: 
[info] server/*:assembly 
[info] {.}/*:assembly 
[info] */*:assembly 

...

server > inspect assembly-option(for assembly) 
[info] Setting: sbtassembly.AssemblyOption = AssemblyOption(true,true,true,<function1>) 
[info] Description: 
[info] 
[info] Provided by: 
[info] {file:/.../}server/*:assembly-option(for assembly) 
[info] Dependencies: 
[info] server/*:assembly-assemble-artifact(for package-bin) 
[info] server/*:assembly-assemble-artifact(for assembly-package-scala) 
[info] server/*:assembly-assemble-artifact(for assembly-package-dependency) 
[info] server/*:assembly-excluded-files(for assembly) 
... 

AssemblyOption並沒有說明什麼包裝,然而,和插件似乎使用SBT自己Package對於這一點,所以也許有配置方式?程序包反過來調用IO.jar(...)來寫入文件。這使用withZipOutput來創建一個ZipOutputStream(或JarOutputStream),我想打電話給setMethod(ZipOutputStream.STORED),但我不能。

除了sbt功能請求以外的任何想法?

回答

4

無法通過sbt配置直接執行此操作,因爲sbt假定zip和jar工件中的任何文件都應該被壓縮。

解決方法之一是解壓縮並重新壓縮(不壓縮)jar文件。

packageBin in Compile <<= packageBin in Compile map { file => 
    println("(Re)packaging with zero compression...") 
    import java.io.{FileInputStream,FileOutputStream,ByteArrayOutputStream} 
    import java.util.zip.{CRC32,ZipEntry,ZipInputStream,ZipOutputStream} 
    val zis = new ZipInputStream(new FileInputStream(file)) 
    val tmp = new File(file.getAbsolutePath + "_decompressed") 
    val zos = new ZipOutputStream(new FileOutputStream(tmp)) 
    zos.setMethod(ZipOutputStream.STORED) 
    Iterator.continually(zis.getNextEntry). 
    takeWhile(ze => ze != null). 
    foreach { ze => 
     val baos = new ByteArrayOutputStream 
     Iterator.continually(zis.read()). 
     takeWhile(-1 !=). 
     foreach(baos.write) 
     val bytes = baos.toByteArray 
     ze.setMethod(ZipEntry.STORED) 
     ze.setSize(baos.size) 
     ze.setCompressedSize(baos.size) 
     val crc = new CRC32 
     crc.update(bytes) 
     ze.setCrc(crc.getValue) 
     zos.putNextEntry(ze) 
     zos.write(bytes) 
     zos.closeEntry 
     zis.closeEntry 
    } 
    zos.close 
    zis.close 
    tmp.renameTo(file) 
    file 
} 

現在,當您運行在SBT package,最終的jar文件將是未壓縮的,您可以與驗證:您可以通過添加以下設置到您的項目(例如,在build.sbt)做到這一點unzip -vl path/to/package.jar

相關問題