Grant
Grant

Reputation: 1164

VS2012 Post-build event not firing

I have a web project that fires a post-build event "On successful build" to perform some cleanup/migration activities (command script).

In VS2012, post-build on success ONLY fires when there is a code change. If there is no code change, the compiler still reports Successful Build, however, the on success post-build event does not fire.

In VS2010, post-build event on success fires on every successful build regardless of code changes. This is what I would expect. The compile was successful, even if no changes occurred, so the event should fire.

Example VS2012 build with code change:

------ 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 ==========

Example VS2012 build without code change:

------ 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 ==========

I tried using post-build event "Always" in VS2012. It only fires the Always post-build event if there is a code change (identical to On success). My only workaround has been to do a Rebuild - painful when I have dozens of dependent projects! Or manually run my script - also annoying! (And NO, it's not my script - this script works perfectly fine when there is a code change as the first example illustrates!)

This is either an intentional change or a bug.

Has anyone else experienced this post-build problem in VS2012?

Upvotes: 21

Views: 15168

Answers (4)

noelicus
noelicus

Reputation: 15055

For me, I needed this answer, which states that if you're calling a batch file then use:

CALL mybatch
CALL anothercommand

instead of directly using the command:

mybatch
anothercommand

only mybatch will be called if CALL is not used.

Upvotes: 1

Mitch
Mitch

Reputation: 22251

Post build events may not run correctly if the PostBuildEvent property is defined before the import of $(MSBuildToolsPath)\Microsoft.CSharp.targets. Make sure that the <PropertyGroup> defining the post build event is after the import of this file.

Example of an incorrect msbuild file:

<?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>

Example of a correct msbuild file:

<?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>

Upvotes: 2

Jorge L. Fatta
Jorge L. Fatta

Reputation: 557

I had the same problem and I solved it as follows:

  • add some dummy empty txt file to the project.
  • property "Build Action": Content.
  • property "Copy to Output Directory": Always.

thats it, it will execute de POST-BUILD step always, even if the project is up to date or is not the startup project.

enter image description here

Upvotes: 31

lloyd christmas
lloyd christmas

Reputation: 576

I had a similar issue and this helped me:

Tools... Options... Projects and Solutions ... Build and Run...

Uncheck "Only build startup projects and dependencies on Run"

As suggested here: Post-Build Event on Run (F5) In Visual Studio?

Upvotes: 3

Related Questions