清理解决方案时,MSBuild 在 AfterBuild 中缺少输出文件

MSBuild missing output files in AfterBuild when solution is cleaned

我确定我遗漏了一些小东西。问题是:

我有一个包含多个项目的解决方案,每次构建后这些项目都会被压缩。这是在一个项目中创建 zip 的示例(它们在其他项目中几乎相同):

<ItemGroup>
  <CopySourceFiles Include="$(OutDir)\**\*.*" Exclude="$(OutDir)\**\*.pdb;$(OutDir)\*.mdf;$(OutDir)\*.ldf;$(OutDir)\*.vshost.*" />
</ItemGroup>

...

<Target Name="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <MakeDir Directories="$(OutDir)\..\zip_working" />
  <!-- first copy the source files specified in the CorySourceFiles ItemGroup above. -->
  <Copy SourceFiles="@(CopySourceFiles)" DestinationFiles="@(CopySourceFiles->'$(OutDir)\..\zip_working\%(RecursiveDir)%(Filename)%(Extension)')" />
  <!-- Perform the zip by calling the UsingTask. Make sure the DestinationFiles above and the SourceDirectory below are pointing to the same place -->
  <Zip SourceDirectory="$(OutDir)\..\zip_working" OutputFilename="$(OutDir)\..\zip$(ProjectName).zip" />
  <!-- Clean up. -->
  <RemoveDir Directories="$(OutDir)\..\zip_working" />
</Target>

有一个最终项目,其中包含指向它合并到一个包中的压缩文件的链接。一切看起来都很正常,但显然只有当 binzip_working 文件夹已经存在时。 IE。如果我清理解决方案,删除 bin 文件夹然后重建,在每个项目的 "zip" 文件夹中创建的最终 zip 是空的...

然后 zip 文件只有在我再次构建 后才有内容。

所以我猜测在构建过程中,AfterBuild 目标是 运行ning before 构建输出文件存在。听起来对吗?我完全从 Visual Studio.

中触发构建

无论如何,我如何确保我可以 运行 仅在创建输出文件后才能执行构建输出文件的任务?


适用于 Visual Studio 2013 更新 5/MSBuild 12.0

如果您删除 OutDir 中的所有内容,然后构建项目,甚至在构建开始之前就会评估顶级(如在目标中,而不是在目标内部)ItemGroup。例如,可以找到一些信息 here。换句话说,在构建之前和空的 OutDir $(OutDir)\**\*.* 计算结果为空,您的 CopySourceFiles 项目为空。

解决方案就是将 ItemGroup 移到 AfterBuild 目标中。然后它将在构建 之后进行评估,因此可以正确查看 outDir 中的当前文件。