【问题标题】:VS2012 Post-build event not firingVS2012 构建后事件未触发
【发布时间】:2012-08-18 12:38:06
【问题描述】:

我有一个 Web 项目,它会触发构建后事件“成功构建”以执行一些清理/迁移活动(命令脚本)。

在 VS2012 中,成功后构建仅在代码更改时触发。如果没有代码更改,编译器仍会报告成功构建,但是,成功后构建事件不会触发。

在 VS2010 中,无论代码更改如何,每次成功构建都会触发构建后成功事件。这是我所期望的。编译成功,即使没有发生任何更改,所以应该触发事件。

带有代码更改的 VS2012 构建示例:

------ Build started: Project: ABC.Business.Web.Migrate, Configuration: Debug Any CPU ------
Build started 2012-08-23 01:26:13.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva- /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" /reference:C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Business.Web.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll" /debug+ /debug:full /optimize- /out:obj\Debug\ABC.Web.Migrate.dll /target:library /utf8output Properties\AssemblyInfo.cs "C:\Users\Administrator\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.cs"
_CopyFilesMarkedCopyLocal:
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Business.Web.dll" to "bin\ABC.Business.Web.dll".
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Web.dll" to "bin\ABC.Web.dll".
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC._Services.dll" to "bin\ABC._Services.dll".
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Business.Web.pdb" to "bin\ABC.Business.Web.pdb".
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Web.pdb" to "bin\ABC.Web.pdb".
  Copying file from "C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC._Services.pdb" to "bin\ABC._Services.pdb".
CopyFilesToOutputDirectory:
  Copying file from "obj\Debug\ABC.Web.Migrate.dll" to "bin\ABC.Web.Migrate.dll".
  ABC.Business.Web.Migrate -> C:\Dev\ABC\Source\ABC.Business.Web.Migrate\bin\ABC.Web.Migrate.dll
  Copying file from "obj\Debug\ABC.Web.Migrate.pdb" to "bin\ABC.Web.Migrate.pdb".
PostBuildEvent:
  "C:\Dev\bin\spawn.exe" "C:\Dev\ABC\Scripts\Migrate Business Web.bat"

Build succeeded.

Time Elapsed 00:00:00.34
========== Build: 4 succeeded, 0 failed, 53 up-to-date, 0 skipped ==========

无需更改代码的 VS2012 构建示例:

------ Build started: Project: ABC.Business.Web, Configuration: Debug Any CPU ------
Build started 2012-08-23 01:36:04.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  ABC.Business.Web -> C:\Dev\ABC\Source\ABC.Business.Web\bin\ABC.Business.Web.dll

Build succeeded.

Time Elapsed 00:00:00.31
========== Build: 1 succeeded, 0 failed, 56 up-to-date, 0 skipped ==========

我尝试在 VS2012 中使用构建后事件“始终”。如果有代码更改(与 On success 相同),它只会触发 Always post-build 事件。我唯一的解决方法是进行重建——当我有几十个依赖项目时很痛苦!或者手动运行我的脚本——也很烦人! (不,这不是我的脚本 - 如第一个示例所示,当发生代码更改时,此脚本可以正常工作!)

这是有意更改或错误。

有其他人在 VS2012 中遇到过这个构建后问题吗?

【问题讨论】:

  • 我忘了说我有两个项目。 ABC.Business.Web 和 ABC.Business.Web.Migrate - Migrate 具有对 Web 项目的单一引用。 post-build 事件仅包含在 Migrate 项目中(这样我可以在不运行脚本的情况下进行编译)。所以在上面的输出中,ABC.Business.Web.Migrate 项目似乎根本没有编译!

标签: visual-studio-2012 post-build-event


【解决方案1】:

对我来说,我需要this answer,它指出如果您要调用批处理文件,请使用:

CALL mybatch
CALL anothercommand

而不是直接使用命令:

mybatch
anothercommand

如果不使用CALL,则只会调用mybatch

【讨论】:

    【解决方案2】:

    如果在导入 $(MSBuildToolsPath)\Microsoft.CSharp.targets 之前定义了 PostBuildEvent 属性,则构建后事件可能无法正确运行。确保定义构建后事件的<PropertyGroup> 在此文件的导入之后。

    错误的 msbuild 文件示例:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <!-- Snip -->
      <!-- DO NOT USE, INCORRECT ORDERING OF IMPORT -->
      <PropertyGroup>
        <PostBuildEvent>echo $(TargetName)</PostBuildEvent>
      </PropertyGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    </Project>
    

    正确的 msbuild 文件示例:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <!-- Snip -->
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
      <PropertyGroup>
        <PostBuildEvent>echo $(TargetName)</PostBuildEvent>
      </PropertyGroup>
    </Project>
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我解决了如下:

      • 在项目中添加一些虚拟的空 txt 文件。
      • 属性“构建操作”:内容。
      • 属性“复制到输出目录”:始终。

      就是这样,它会一直执行 de POST-BUILD 步骤,即使项目是最新的或者不是启动项目。

      【讨论】:

      • 我不知道这是否回答了 OP 的具体问题,但它解决了我的问题,谢谢!
      • 我在 c++ 项目中没有看到这个选项。嗯
      • 对我来说似乎是 Visual Studio 回归。
      • 你是个十足的英雄!
      • 请注意,这会带来不同的烦恼 - 现在您的项目将在您运行时始终重新构建,即使没有代码更改也是如此。如果解决方案很大并且有很多依赖项,这可能会非常缓慢和烦人。
      【解决方案4】:

      我遇到了类似的问题,这对我有帮助:

      工具...选项...项目和解决方案...构建和运行...

      取消选中“仅在运行时构建启动项目和依赖项”

      这里建议:Post-Build Event on Run (F5) In Visual Studio?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 2011-10-09
        • 2014-12-18
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        相关资源
        最近更新 更多