如何使用msbuild/web部署项目删除多个文件?

发布于 2024-07-19 05:11:43 字数 1436 浏览 9 评论 0原文

我对 msbuild 在 VS2008 Web 部署项目中的表现有一个奇怪的问题,并且想知道为什么它似乎随机表现不佳。

我需要从部署文件夹中删除一些仅应存在于我的开发环境中的文件。 这些文件是 Web 应用程序在开发/测试期间生成的,不包含在我的 Visual Studio 项目/解决方案中。

我使用的配置如下:

<!--  Partial extract from Microsoft Visual Studio 2008 Web Deployment Project -->
<ItemGroup>
  <DeleteAfterBuild Include="$(OutputPath)data\errors\*.xml" /> <!-- Folder 1: 36 files -->
  <DeleteAfterBuild Include="$(OutputPath)data\logos\*.*" />    <!-- Folder 2: 2 files -->
  <DeleteAfterBuild Include="$(OutputPath)banners\*.*" />       <!-- Folder 3: 1 file -->
</ItemGroup>

<Target Name="AfterBuild">
  <Message Text="------ AfterBuild process starting ------" Importance="high" />
    <Delete Files="@(DeleteAfterBuild)">
      <Output TaskParameter="DeletedFiles" PropertyName="deleted" />
    </Delete>
    <Message Text="DELETED FILES: $(deleted)" Importance="high" />
  <Message Text="------ AfterBuild process complete ------" Importance="high" />
</Target>

我遇到的问题是,当我构建/重建 Web 部署项目时,它“有时”会删除所有文件,但有时它不会删除任何内容! 或者,它将仅删除“DeleteAfterBuild”项目组中三个文件夹中的一两个。 构建过程决定是否删除文件的时间似乎没有一致性。

当我编辑配置以仅包含文件夹 1(例如)时,它会正确删除所有文件。 然后添加文件夹 2 和 3,它开始删除我想要的所有文件。 然后,我会随机重建该项目,并且不会删除任何文件!

我尝试将这些项目移动到 ExcludeFromBuild 项目组(这可能是它应该在的位置),但它给了我同样不可预测的结果。

有人经历过这个吗? 难道我做错了什么? 为什么会出现这种情况?

I have an odd issue with how msbuild is behaving with a VS2008 Web Deployment Project and would like to know why it seems to randomly misbehave.

I need to remove a number of files from a deployment folder that should only exist in my development environment. The files have been generated by the web application during dev/testing and are not included in my Visual Studio project/solution.

The configuration I am using is as follows:

<!--  Partial extract from Microsoft Visual Studio 2008 Web Deployment Project -->
<ItemGroup>
  <DeleteAfterBuild Include="$(OutputPath)data\errors\*.xml" /> <!-- Folder 1: 36 files -->
  <DeleteAfterBuild Include="$(OutputPath)data\logos\*.*" />    <!-- Folder 2: 2 files -->
  <DeleteAfterBuild Include="$(OutputPath)banners\*.*" />       <!-- Folder 3: 1 file -->
</ItemGroup>

<Target Name="AfterBuild">
  <Message Text="------ AfterBuild process starting ------" Importance="high" />
    <Delete Files="@(DeleteAfterBuild)">
      <Output TaskParameter="DeletedFiles" PropertyName="deleted" />
    </Delete>
    <Message Text="DELETED FILES: $(deleted)" Importance="high" />
  <Message Text="------ AfterBuild process complete ------" Importance="high" />
</Target>

The problem I have is that when I do a build/rebuild of the Web Deployment Project it "sometimes" removes all the files but other times it will not remove anything! Or it will remove only one or two of the three folders in the DeleteAfterBuild item group. There seems to be no consistency in when the build process decides to remove the files or not.

When I've edited the configuration to include only Folder 1 (for example), it removes all the files correctly. Then adding Folder 2 and 3, it starts removing all the files as I want. Then, seeming at random times, I'll rebuild the project and it won't remove any of the files!

I have tried moving these items to the ExcludeFromBuild item group (which is probably where it should be) but it gives me the same unpredictable result.

Has anyone experienced this? Am I doing something wrong? Why does this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-07-26 05:11:43

在脚本加载时且在 处理之前进行评估。

似乎有不止一种方法可以正确执行此操作 -

  1. 包含在 中,并且应该在正确的时间。 这将适用于 MS-Build v3.5+

  2. 使用 生成项目列表

示例构建脚本 -

<!-- Using ItemGroup -->
<Target Name="AfterBuild">
  <ItemGroup>
    <DeleteAfterBuild Include="$(OutputPath)data\errors\*.xml" />
  </ItemGroup>
  <Delete Files="@(DeleteAfterBuild)" />
</Target>

<!-- Using CreateItem -->
<Target Name="AfterBuild">
  <CreateItem Include="$(OutputPath)data\errors\*.xml">
    <Output TaskParameter="Include" ItemName="DeleteAfterBuild"/>
  </CreateItem>
  <Delete Files="@(DeleteAfterBuild)" />
</Target>

解释为什么删除过程生成“不可预测”的结果 -

  1. 从干净的构建输出路径开始
  2. 构建 #1 - 构建 Web 部署项目。 @(DeleteAfterBuild) 将在没有文件的情况下进行评估,因为 $(OutputPath) 文件夹中不存在这些文件,并且不会删除作为 一部分的任何文件AfterBuild 目标
  3. Build #2 - 构建 Web 部署项目。 @(DeleteAfterBuild) 将使用 $(OutputPath) 文件夹中的所有预期文件进行评估,并将这些文件作为 AfterBuild 目标的一部分删除
  4. 基本上我们现在回到了阶段 2。重复。 结果当然是可以预见的——不用再挠头了。

参考资料:
如何:动态创建项目组
延迟评估 MSBUILD 中的项目文件

The <ItemGroup> is evaluated when the script loads and before the <Target> has been processed.

There appears to be more than one way to do this correctly -

  1. Include the <ItemGroup> inside of the <Target> and it should be evaluated at the correct time. This will work with MS-Build v3.5+

  2. Use <CreateItem> to generate the item list

Example build script for this -

<!-- Using ItemGroup -->
<Target Name="AfterBuild">
  <ItemGroup>
    <DeleteAfterBuild Include="$(OutputPath)data\errors\*.xml" />
  </ItemGroup>
  <Delete Files="@(DeleteAfterBuild)" />
</Target>

<!-- Using CreateItem -->
<Target Name="AfterBuild">
  <CreateItem Include="$(OutputPath)data\errors\*.xml">
    <Output TaskParameter="Include" ItemName="DeleteAfterBuild"/>
  </CreateItem>
  <Delete Files="@(DeleteAfterBuild)" />
</Target>

To explain why the delete process was generating 'unpredictable' results -

  1. Start with a clean build output path
  2. Build #1 - build the Web Deployment Project. @(DeleteAfterBuild) will evaluate with no files as the files don't exist in the $(OutputPath) folder and will not delete any files as part of the AfterBuild target
  3. Build #2 - build the Web Deployment Project. @(DeleteAfterBuild) will evaluate with all the expected files in the $(OutputPath) folder and will remove the files as part of the AfterBuild target
  4. Basically we're now back to stage 2. Repeat. The results are of-course predictable - scratch your head no longer.

Reference material:
How To: Create Item Groups on the Fly,
Delayed evaluation of items in MSBUILD file

乱世争霸 2024-07-26 05:11:43

我意识到这个问题已经得到了回答,但我想我应该加 5 美分。 对于 Web 部署项目,无需使用提供的目标,只需添加包含 ExcludeFromBuild 元素的项目组即可。 我提供了部署项目文件底部的相关部分以供参考。

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.WebDeployment.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="BeforeMerge">
  </Target>
  <Target Name="AfterMerge">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <ItemGroup>
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\obj\**\*.*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\Properties\**\*.*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.csproj*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.resx" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.Publish.xml" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\LocalTestRun.testrunconfig" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\TestResults\**\*.*" />
  </ItemGroup>

I realise this has already been answered but I thought I would add my 5 cents. For a Web Deployment project there's no need to use the provided targets, just add an item group containing ExcludeFromBuild elements. I've provided the relevant section from the bottom of my deployment project file for reference.

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.WebDeployment.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="BeforeMerge">
  </Target>
  <Target Name="AfterMerge">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <ItemGroup>
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\obj\**\*.*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\Properties\**\*.*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.csproj*" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.resx" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.Publish.xml" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\LocalTestRun.testrunconfig" />
    <ExcludeFromBuild Include="$(SourceWebPhysicalPath)\TestResults\**\*.*" />
  </ItemGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文