visual-studio 如何在 Visual Studio 2008 中创建自定义清理(清理后)事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:15:32  来源:igfitidea点击:

How to create custom clean (post-clean) event in Visual Studio 2008?

c++visual-studiovisual-studio-2008visual-c++

提问by Pete

In our build process, for each project we use Post Build events to copy our executable files into a separate deployment directory. That works just peachy, but the problem is that we run into problems with stale files after performing a Clean Solution/Clean Project. I'd like to set up a "Clean" event that deletes the copied file and Visual Studio 2008 does not seem to provide an option in the project properties page.

在我们的构建过程中,对于每个项目,我们使用构建后事件将我们的可执行文件复制到单独的部署目录中。这很有效,但问题是我们在执行清理解决方案/清理项目后遇到了陈旧文件的问题。我想设置一个“清理”事件来删除复制的文件,而 Visual Studio 2008 似乎没有在项目属性页面中提供选项。

It has:

它有:

Build Events:
   Pre-Build Event
   Pre-Link Event
   Post-Build Event
Custom Build Step
   General

What I'd like to find is some way to execute an arbitrary command line when the project is cleaned.

我想找到的是在清理项目时执行任意命令行的某种方法。

回答by Pete

You can use the MSBuild target syntax in your csproj file. e.g

您可以在 csproj 文件中使用 MSBuild 目标语法。例如

  <Target Name="AfterClean">
    <Delete Files="$(OutDir)$(TargetName).exe" ContinueOnError="true" />
  </Target>

There is a neat way to edit your .csproj file directly in the Visual Studio IDE described in the MSBuild team blog, but this is my first post so I can only include one hyperlink. (briefly: Unload the project, then right-click on it to see the entry "Edit [project].csproj" ... your csproj will come up in the IDE as an xml file with intellisense on elements and attributes. Wonderful!)

有一种直接在 MSBuild 团队博客中描述的 Visual Studio IDE 中编辑 .csproj 文件的巧妙方法,但这是我的第一篇文章,因此我只能包含一个超链接。(简而言之:卸载项目,然后右键单击它以查看条目“编辑 [项目].csproj”……您的 csproj 将作为 xml 文件出现在 IDE 中,其中包含有关元素和属性的智能感知。太棒了!)

A full list of custom Targets is here: http://blogs.msdn.com/b/msbuild/archive/2005/11/23/496396.aspx

自定义目标的完整列表在这里:http: //blogs.msdn.com/b/msbuild/archive/2005/11/23/496396.aspx

回答by Drew Thaler

For Visual C++ projects, you need to add the files to the "Extensions to Delete On Clean" section under the "General" project configuration properties. Even though it claims to want extensions, it's actually using globs and will happily accept full paths and expand MSBuild variables. This worked for me:

对于 Visual C++ 项目,您需要将文件添加到“General”项目配置属性下的“Extensions to Delete On Clean”部分。尽管它声称需要扩展,但它实际上是在使用 globs 并且很乐意接受完整路径并扩展 MSBuild 变量。这对我有用:

$(ProjectDir)\deployment\*.*

$(ProjectDir)\deployment\*.*

I'm not sure if you can remove directories this way, but it can at least get the files.

我不确定您是否可以通过这种方式删除目录,但它至少可以获取文件。

回答by fbastian

If you use "Project Properties --> Configuration Properties --> Custom Build Setup" you have to remember to fill in the 'Outputs' field, otherwise the it won't run.

如果您使用“项目属性 --> 配置属性 --> 自定义构建设置”,您必须记住填写“输出”字段,否则它将无法运行。

from:

从:

http://blogs.msdn.com/b/visualstudio/archive/2010/04/26/custom-build-steps-tools-and-events.aspx

http://blogs.msdn.com/b/visualstudio/archive/2010/04/26/custom-build-steps-tools-and-events.aspx

"The Outputs property is a semicolon-delimited list of files, and must list whatever files are generated as part of the execution of your custom build step. If you leave your Outputs property empty, your Custom Build Step will never execute, since MSBuild will determine that no outputs are out of date. If your step doesn't generate files but you still want it to execute with every build, making up a fake filename will do the trick, since that file will never exist and MSBuild will always determine that the custom build step is out of date."

“Outputs 属性是一个以分号分隔的文件列表,必须列出在执行自定义构建步骤时生成的任何文件。如果您将 Outputs 属性留空,则您的自定义构建步骤将永远不会执行,因为 MSBuild 将确定没有输出是过时的。如果您的步骤没有生成文件,但您仍然希望它在每次构建时都执行,那么制作一个假文件名就可以解决问题,因为该文件永远不会存在并且 MSBuild 将始终确定自定义构建步骤已过时。”

回答by Scott Dorman

You will need to edit the .csproj files by hand and add an "AfterClean" target.

您需要手动编辑 .csproj 文件并添加“AfterClean”目标。

回答by Aaron Saarela

There is no documented way to insert custom cleanup steps, unfortunately. You can clean up your output in your pre-build event but that will still leave artifacts around just after a clean.

不幸的是,没有记录的方法来插入自定义清理步骤。您可以在预构建事件中清理输出,但在清理后仍会留下工件。

From MSDN, here is the order of invocation for the various build steps:

从 MSDN,这里是各种构建步骤的调用顺序:

  1. Pre-Build event
  2. Custom build steps on individual files
  3. Proxy generator
  4. MIDL
  5. Resource compiler
  6. The C/C++ compiler
  7. Pre-Link event
  8. Linker or Librarian (as appropriate)
  9. BSCMake
  10. Custom build step on the project
  11. Deployment tool.
  12. Post-Build event

    MSDN: Understanding custom build steps

  1. 预构建事件
  2. 单个文件的自定义构建步骤
  3. 代理生成器
  4. 中间件
  5. 资源编译器
  6. C/C++ 编译器
  7. 预链接事件
  8. 链接器或图书馆员(视情况而定)
  9. BSCMake
  10. 项目的自定义构建步骤
  11. 部署工具。
  12. 构建后事件

    MSDN:了解自定义构建步骤

回答by Samuel

The others didn't do exactly what I wanted, and I just found a better way. Tested in VS2010 for a C++ Win32 project, go to Project Properties --> Configuration Properties --> Custom Build Setup. You can add a custom command line, and tell VS what operation to execute the command before or after.

其他人没有完全按照我的意愿去做,我只是找到了更好的方法。在 VS2010 中针对 C++ Win32 项目进行测试,转到项目属性 --> 配置属性 --> 自定义构建设置。可以添加自定义命令行,告诉VS在该命令之前或之后执行什么操作。