MSBuild 和创建 ZIP 文件

标签 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"/>
  <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>

我的脚本运行良好,只有一个奇怪的问题。当我第一次构建项目时,没有 \bin\Debug文件夹及其在构建期间创建,但 ZIP 文件仍然为空。第二次运行构建脚本时 \bin\Debug文件夹现在包含已构建的文件,然后将文件添加到 ZIP。

第一次运行脚本 ZIP 文件是空的可能是什么问题?

最佳答案

问题出在 DebugApplicationFiles项目集合。它是在调用构建之前创建的。移动 DebugApplicationFiles进入 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>

关于MSBuild 和创建 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397449/

相关文章:

gradle - Gradle Zip DuplicatesStrategy无法正常工作

MSBUILD动态创建配置XML Dotfuscator

azure - 访问 Azure Pipelines 中的私有(private) Nuget 服务器中托管的 Nuget 包

configuration - 选择要链接的程序集以使用配置 (MonoDevelop)

node.js - 实际上在不验证的情况下从发布中排除文件

asp.net-mvc - 如何让 MvcBuildViews 在出错时继续其他 View ?

msbuild - 如何使用带有集成 Windows 身份验证的 MSDeploy 和 TeamCity 进行部署?

php - 压缩目录中的所有文件并下载生成的 .zip

C++ 跨平台归档操作/显示

msbuild - 在 "BeforeBuild"目标之前捕获的程序集版本