代码之家  ›  专栏  ›  技术社区  ›  jelovirt

如何在SBT中压缩带有前缀文件夹的文件

  •  4
  • jelovirt  · 技术社区  · 14 年前

    要使用简单的构建工具生成一个分发zip,您只需

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

    这会将JAR文件添加到zip的根目录中。如何将罐子放入 lib Zip中的子文件夹?

    1 回复  |  直到 13 年前
        1
  •  4
  •   axel22    13 年前

    SBT 0.7 .X:

    据我所知,没有默认的实现。但是,您可以使用SBT的文件实用程序。

    尝试使用下面的示例,将工件jar复制到tmp目录中,压缩目录并删除它。应该直接将其扩展到依赖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                                                                                                                                                                                                       
      }                                                                                                                                                                                                            
    
    }                 
    

    以下函数来自 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]
    

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