如何在 Visual Studio 中暂时禁用构建事件?

发布于 2025-01-02 12:48:27 字数 108 浏览 0 评论 0原文

有没有办法暂时禁用构建前和构建后事件?

即在没有构建事件的情况下进行构建(这需要一些时间,但运行并不总是至关重要)

目前,我有缩小和其他一些事情正在进行,但我并不总是需要它。

Is there a way to temporarily disable pre and post build events?

i.e. build without build events (which are taking a bit of time but arent always crucial to run)

At the moment, I have minification and a couple of other things going on and I don't always need that.

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

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

发布评论

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

评论(5

莫言歌 2025-01-09 12:48:27

禁用构建事件的最简单方法是传递空值:

msbuild your.sln /p:PreBuildEvent=;PostBuildEvent=

The simplest way to disable the build events is passing empty values:

msbuild your.sln /p:PreBuildEvent=;PostBuildEvent=
回忆追雨的时光 2025-01-09 12:48:27

像最小化这样的东西只对发布版本重要。所以你可以这样跳过它:

if "$(ConfigurationName)" == "Debug" goto skip
; stuff here...
:skip

还有一些其他的宏你可以使用,点击编辑按钮,然后宏>>按钮来查看它们。环境变量也可以测试,使用%varname%。但设置起来要困难得多。

Stuff like minimization only matters for the Release build. So you could skip it like this:

if "$(ConfigurationName)" == "Debug" goto skip
; stuff here...
:skip

There are some other macros you can use, click the Edit button and the Macro>> button to see them. Environment variables can be tested as well, use %varname%. But are much harder to set.

栖迟 2025-01-09 12:48:27

我还玩了一点 msbuild foo.vcxproj /p:PreBuildEvent= /p:PostBuildEvent=,但对我来说它不起作用,可能是因为我正在使用自定义道具文件。

然而,我发现有效的是 /p:PostBuildEventUseInBuild=false

I also played a little with msbuild foo.vcxproj /p:PreBuildEvent= /p:PostBuildEvent=, but for me it didn't work, probably because I am using custom props files.

What I found to work however was /p:PostBuildEventUseInBuild=false

平定天下 2025-01-09 12:48:27

检查您的构建后事件设置。在“构建事件”选项卡上,将“运行构建后事件”组合框值更改为“当构建更新项目输出时”。仅当更新输出程序集时才会执行构建后事件。

或者

使用 MSBuild 命令构建您的解决方案(这对于多解决方案项目很有用)。
在电脑上的某个位置创建“DisableBuildEvents.msbuild”文件。
DisableBuildEvents.msbuild 内容:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PostBuildEvent"/>
  <Target Name="PreBuildEvent" />
</Project>

在命令行中设置 CustomAfterMicrosoftCommonTargets 属性来执行 MsBuild:

MSBuild.exe YourSolution.sln /t:Build p:CustomAfterMicrosoftCommonTargets="c:\DisableBuildEvents.msbuild"

注意:CustomAfterMicrosoftCommonTargets 值应该是完整路径名。

Check your post build event settings. On the "Build Events" tab change the "Run the post-build event" combo box value to "When the build updates project output". Post build events will be executed only when output assembly is updated.

OR

Use MSBuild command to build your solution (this is useful for multi-solution projects).
Create "DisableBuildEvents.msbuild" file somehere on your PC.
DisableBuildEvents.msbuild content:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PostBuildEvent"/>
  <Target Name="PreBuildEvent" />
</Project>

Execute MsBuild with CustomAfterMicrosoftCommonTargets property set in the command line:

MSBuild.exe YourSolution.sln /t:Build p:CustomAfterMicrosoftCommonTargets="c:\DisableBuildEvents.msbuild"

Note: CustomAfterMicrosoftCommonTargets value should be full path name.

澜川若宁 2025-01-09 12:48:27

通过用户设置文件禁用构建事件

此处尚未提及的另一个选项涉及修改 .vcxproj.user 文件。要禁用构建事件,请将此文件放在 .vcxproj 旁边:

<!-- ProjectName.vcxproj.user -->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup>
    <PreBuildEvent>
      <Command>echo Skipping pre-build event!</Command>
    </PreBuildEvent>
    <PostBuildEvent>
      <Command>echo Skipping post-build event!</Command>
    </PostBuildEvent>
  </ItemDefinitionGroup>
</Project>

这会将消息打印到控制台,而不是运行项目中定义的预/构建后事件。使用 标记使其正常工作非常重要。

正如其他答案中提到的,还可以使用以下属性完全禁用自定义构建事件:(

<PropertyGroup>
  <PreBuildEventUseInBuild>false</PreBuildEventUseInBuild>
  <PreLinkEventUseInBuild>false</PreLinkEventUseInBuild>
  <PostBuildEventUseInBuild>false</PostBuildEventUseInBuild>
</PropertyGroup>

请注意,此处应使用 而不是 .)

这种方法的优点

  • 不需要修改项目/解决方案文件
  • 不需要更改环境(请参阅 Custom.After.Microsoft.Common.Targets 方法)
  • 适用于命令行和 IDE构建 - 不需要额外的参数,只需将 .vcxproj.user 文件放入项目目录中
  • 不会干扰源代码控制(Git 等通常默认忽略 .user 文件)

Disabling build events via the User Settings file

Another option not yet mentioned here involves modifying the .vcxproj.user file. To disable build events, put this file next to your .vcxproj:

<!-- ProjectName.vcxproj.user -->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup>
    <PreBuildEvent>
      <Command>echo Skipping pre-build event!</Command>
    </PreBuildEvent>
    <PostBuildEvent>
      <Command>echo Skipping post-build event!</Command>
    </PostBuildEvent>
  </ItemDefinitionGroup>
</Project>

This will print a message to the console instead of running pre/post-build events defined in your project. It is important to use <ItemDefinitionGroup> tag for it to work.

As mentioned in other answers, one can also use the following properties to disable custom build events completely:

<PropertyGroup>
  <PreBuildEventUseInBuild>false</PreBuildEventUseInBuild>
  <PreLinkEventUseInBuild>false</PreLinkEventUseInBuild>
  <PostBuildEventUseInBuild>false</PostBuildEventUseInBuild>
</PropertyGroup>

(note a <PropertyGroup> should be used here instead of <ItemDefinitionGroup>.)

Advantages of this approach

  • No need to modify project/solution files
  • Doesn't require changing the environment (see Custom.After.Microsoft.Common.Targets approach)
  • Works for both command line and IDE builds—no extra arguments required, just put the .vcxproj.user file in the project directory
  • Does not interfere with source control (Git etc. usually ignores .user files by default)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文