5

要使用 Simple Build Tool 生成分发 ZIP,可以简单地做

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

这会将 JAR 文件添加到 ZIP 的根目录中。如何将 JAR 添加到libZIP 的子文件夹中?

4

1 回答 1

4

对于 sbt 0.7.x:

据我所知,默认情况下没有任何实现。但是,您可以使用 SBT 的 FileUtilities。

尝试使用以下示例,它将您的工件 jar 复制到 tmp 目录中,压缩该目录并将其删除。将其扩展到依赖库应该很简单。

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                                                                                                                                                                                                       
  }                                                                                                                                                                                                            

}                 

上面使用了以下函数FileUtilities

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]

他们的声明应该是不言自明的。

于 2010-09-28T21:14:32.670 回答