使用MSBuild压缩多个项目目录

使用MSBuild压缩多个项目目录

本文介绍了使用MSBuild压缩多个项目目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As part of my build process in MSBuild 4.0, I end up with the following directory structure:

\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文件.如果我执行以下操作:

I want to be able to create one zip file per subfolder of \OutDir. If I do the following:

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

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

然后将每个子文件夹递归压缩,这对于ProjectA和ProjectC可以正常工作,但是ProjectB最终得到两个zip文件,一个是其根目录,另一个是其子文件夹.

then each subfolder is recursively zipped up, which works fine for ProjectA and ProjectC, but ProjectB ends up with two zip files, one of its root level and one of its subfolder.

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

My other requirement is that the number of projects is not known by the build file, so I can't just create an ItemGroup and enumerate the projects that I want zipped up.

在NAnt中,通过其foreach任务可以轻松完成此任务,但是我如何才能在MSBuild中实现此任务,最好不求助于自定义任务?

This task would be easy in NAnt through its foreach task, but how can I achieve this in MSBuild, preferably without resorting to custom tasks?

推荐答案

我想出了一种解决方法-MSBuild Extension Pack的FileUnder任务,枚举要压缩的ProjectX文件夹和Exec任务的组合调用7Zip.代码是:

I came up with a workaround - a combination of the MSBuild Extension pack's FileUnder task, to enumerate the ProjectX folders that I want to zip up, and the Exec task invoking 7Zip. The code is:

<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等的文件.

So now each zip file is named after the folder its contents came from (as well as configuration and versioning details), so I'll have files in my output folder named ProjectA-Debug-0.1.2.345.zip etc.

这篇关于使用MSBuild压缩多个项目目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:26