【问题标题】:Using MSBuild to zip up multiple project directories使用 MSBuild 压缩多个项目目录
【发布时间】:2010-08-16 07:27:05
【问题描述】:

作为我在 MSBuild 4.0 中构建过程的一部分,我最终得到以下目录结构:

\OutDir
    \ProjectA
      \File1.dll  
      \File2.dll  
      \File3.exe
    \ProjectB
      \Subfolder1
        File4.html
      \File5.dll  
      \File6.dll  
      \File7.exe
    \ProjectC
      \File8.dll  
      \File9.exe

我希望能够为\OutDir 的每个子文件夹创建一个 zip 文件。如果我执行以下操作:

<ItemGroup>
  <ZipSource Include="\OutDir\**.*" />
</ItemGroup>

<MSBuild.Community.Tasks.Zip
  Files="@(ZipSource)"
  ZipFileName="OutDir\%(ZipSource.RecursiveDir)ZippedOutput.zip"
  WorkingDirectory="OutDir" />

然后递归压缩每个子文件夹,这适用于 ProjectA 和 ProjectC,但 ProjectB 最终有两个 zip 文件,一个是根目录,另一个是子文件夹。

我的另一个要求是构建文件不知道项目的数量,所以我不能只创建一个 ItemGroup 并枚举我想要压缩的项目。

通过它的 foreach 任务在 NAnt 中这项任务很容易,但我如何在 MSBuild 中实现这一点,最好不求助于自定义任务?

【问题讨论】:

    标签: .net msbuild build-automation


    【解决方案1】:

    我想出了一个解决方法 - MSBuild 扩展包的 FileUnder 任务的组合,用于枚举我想要压缩的 ProjectX 文件夹,以及调用 7Zip 的 Exec 任务。代码是:

    <MSBuild.ExtensionPack.FileSystem.FindUnder
        TaskAction="FindDirectories"
        Path="$(WebOutputFolder)"
        Recursive="False">
        <Output ItemName="WebsiteFolders" TaskParameter="FoundItems" />
    </MSBuild.ExtensionPack.FileSystem.FindUnder>
    
    <Exec Command="7za.exe a -r %22$(OutDir)%(WebsiteFolders.Filename)-$(Configuration)-$(AssemblyFileVersion).zip%22 %22@(WebsiteFolders)\*%22" />
    

    所以现在每个 zip 文件都以其内容来自的文件夹命名(以及配置和版本控制详细信息),所以我的输出文件夹中有名为 ProjectA-Debug-0.1.2.345.zip 等的文件。

    【讨论】:

      【解决方案2】:

      这很可能是因为本机 .Net 框架 zip 功能无法嵌套文件夹,只能嵌套文件 - 如果您正在使用的任务使用此功能,那么您将不走运。

      另外,ZipSource Include="\OutDir\**.*" 的语法有一点错误,请尝试改用 &lt;ZipSource Include="\OutDir\**\*.*"

      如果这不起作用,请尝试使用MSBuild Extension tasks 中的 Zip 任务,它可以满足您的需求。 (Here is the doco 为它)。

      【讨论】:

      • 不幸的是,MSBuild 扩展包中的 Zip 任务与 .NET 4 不兼容(正在编写替代品,但目前尚不可用)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      相关资源
      最近更新 更多