【问题标题】:MSBuild missing output files in AfterBuild when solution is cleaned清理解决方案时,MSBuild 在 AfterBuild 中缺少输出文件
【发布时间】:2016-01-26 12:20:22
【问题描述】:

我确定我缺少一些小东西。问题来了:

我有一个包含多个项目的解决方案,每次构建后都会压缩这些项目。这是在一个项目中创建 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 目标在构建输出文件存在之前运行。听起来对吗?我完全从 Visual Studio 中触发构建。

无论如何,我如何确保只有在创建输出文件后才能在构建输出文件上运行任务?


适用于 Visual Studio 2013 Update 5 / MSBuild 12.0

【问题讨论】:

  • 请提及 VS/msbuild 的哪个版本,因为 AfterBuild 的处理方式存在差异(例如,请参阅 stackoverflow.com/questions/13727351/…
  • @stijn Sure - Visual Studio 2013 更新 5/MSbuild 12.0
  • 谢谢,只是认为这可能无关紧要:似乎问题更可能只是 msbuild 评估顺序:在加载项目时评估 CopySourceFiles。因此,如果当时没有文件,它将是空的。尝试将包含 CopySourceFiles 的 ItemGroup 移动到 AfterBuild 目标中,以便在需要时对其进行评估。
  • @stijn 就是这样!正如你所说,我将 ItemGroup 移动到 AfterBuild 目标中,并且效果很好。再次感谢:)

标签: visual-studio msbuild


【解决方案1】:

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

解决方案只是将 ItemGroup 移动到 AfterBuild 目标内。然后它将在构建之后 进行评估,从而正确查看 outDir 中的当前文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多