MSBuild和创建ZIP文件

MSBuild和创建ZIP文件,msbuild,zip,msbuildcommunitytasks,Msbuild,Zip,Msbuildcommunitytasks,以下是我的构建脚本: <?xml version="1.0" encoding="utf-8" ?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> <Prop

以下是我的构建脚本:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <PropertyGroup>
    <!-- Path where the solution file is located (.sln) -->
    <ProjectPath>W:\Demo</ProjectPath>
    <!-- Location of compiled files -->
    <DebugPath>W:\Demo\bin\Debug</DebugPath>
    <ReleasePath>W:\Demo\bin\Release</ReleasePath>
    <!-- Name of the solution to be compiled without the .sln extension -->         <ProjectSolutionName>DemoTool</ProjectSolutionName>

    <!-- Path where the nightly zip file will be copyd -->
    <NightlyBuildPath>W:\Nightly_Builds\Demo</NightlyBuildPath>
    <!-- Name of the nighly zip file (YYYYMMDD_NightlyZipName.zip, date added automatically) -->
    <NightlyZipName>Demo</NightlyZipName>
  </PropertyGroup>

  <ItemGroup>
    <!-- All files and folders from ./bin/Debug or ./bin/Release what will be added to the nightly zip -->
    <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    <ReleaseApplicationFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\*vshost.exe*" />
  </ItemGroup>

  <Target Name="DebugBuild">
    <Message Text="Building $(ProjectSolutionName) Debug Build" />
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Clean" Properties="Configuration=Debug"/>
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Build" Properties="Configuration=Debug"/>
    <Message Text="$(ProjectSolutionName) Debug Build Complete!" />
    <CallTarget Targets="CreateNightlyZip" />
  </Target>

  <Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
          WorkingDirectory="$(DebugPath)"
          ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
          ZipLevel="9" />
  </Target>
</Project>

W:\Demo
W:\Demo\bin\Debug
W:\Demo\bin\Release
降级
W:\Nightly\u Builds\Demo
演示
$([System.DateTime]::Now.ToString('yyyyMMdd'))
我的剧本很完美,只是有一个奇怪的问题。当我第一次构建项目时,没有
\bin\Debug
文件夹及其在构建过程中创建的文件夹,但是ZIP文件仍然是空的。当
\bin\Debug
文件夹现在包含构建文件时,再次运行构建脚本,然后将该文件添加到ZIP


当ZIP文件为空时第一次运行脚本可能会出现什么问题?

问题出现在
DebugApplicationFiles
项集合中。它是在调用生成之前创建的。将
debugapplicationfile
移动到
CreateNightlyZip
目标中。通过以下方式更新脚本:

<Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <ItemGroup>
        <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    </ItemGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
      WorkingDirectory="$(DebugPath)"
      ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
      ZipLevel="9" />
</Target>

$([System.DateTime]::Now.ToString('yyyyMMdd'))

如果powershell 5.0或更高版本可用,则可以直接使用powershell命令

<Target Name="Zip" BeforeTargets="AfterBuild">
  <ItemGroup>
    <ZipFiles Include="$(OutDir)release\file1.exe" />
    <ZipFiles Include="$(OutDir)release\file2.exe" />
  </ItemGroup>
  <Exec Command="PowerShell -command Compress-Archive @(ZipFiles, ',') $(OutDir)release\zippedfiles.zip" />
</Target>

由于MSBuild 15.8有一种简单的方法-ZipDirectory生成任务,因此,如果您希望压缩“xcopy deploy”的整个文件夹,请执行此操作

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="ZipOutputPath" AfterTargets="Build">
        <ZipDirectory
            SourceDirectory="$(OutputPath)"
            DestinationFile="$(OutputPath)\..\$(AssemblyName).zip"
            Overwrite=="true" />
    </Target>

</Project>


[1]

这甚至适用于子目录。它们是使用原始目录结构压缩的。@kiewic您是如何做到这一点的?在我的例子中,由于MSBuild扩展了项组,因此命令行中提供了一个逗号分隔的路径列表,Compress Archive将尝试将提供给根的每个路径都放在其中。所以我得到了一个扁平的档案。我想使用项目组(而不是直接提供压缩存档的模式),因为我有很多排除项等@realmarkuschmidt您能压缩原始结构吗?我想实现同样的效果。@kerzek,对不起,没有。我使用了本机代码任务和System.IO.Compression。下面是一些可能给您留下印象的示例代码(您也可以将其保存到专用文件中,并从实际构建脚本中包含):@realmarkuschmidt太好了,我将使用System.IO.Compression方法,谢谢Markus。