title: What happened to BeforeBuild and other targets in VS2012?
body: <p>I'm trying to get some pre-build steps to work in a C++ project in Visual Studio 2012 but they do not get invoked (while I'm pretty sure the same techniques were OK in Visual Studio 2010). Command line builds behave exactly the same.</p>
  
  <p>This is the end of the project file; the file was generated using Visual Studio and then I just added the last couple of lines:</p>
  
  <pre><code>&lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
  &lt;Target Name="BeforeBuild"&gt;
    &lt;Message Text="### BeforeBuild ###" /&gt;
  &lt;/Target&gt;
  &lt;Target Name="BeforeCompile"&gt;
    &lt;Message Text="### BeforeCompile ###" /&gt;
  &lt;/Target&gt;
  &lt;Target Name="AfterBuild"&gt;
    &lt;Message Text="### AfterBuild ###" /&gt;
  &lt;/Target&gt;
  </code></pre>
  
  <p>and here's the output:</p>
  
  <pre><code>Project "d:\temp\temp.vcxproj" on node 1 (default targets).
  InitializeBuildStatus:
    Creating "Debug\temp.unsuccessfulbuild" because "AlwaysCreate" was specified.
  AfterBuild:
    AfterBuild
  FinalizeBuildStatus:
    Deleting file "Debug\temp.unsuccessfulbuild".
    Touching "Debug\temp.lastbuildstate".
  </code></pre>
  
  <p>So only AfterBuild is considered and the others are ignored.
  Looking into this I found this PropertyGroup in Microsoft.BuildSteps.targets:</p>
  
  <pre><code>&lt;BuildDependsOn&gt;
    _PrepareForBuild;
    $(BuildSteps);
    AfterBuild;
    FinalizeBuildStatus;
  &lt;/BuildDependsOn&gt;
  </code></pre>
  
  <p>Shouldn't this also have BeforeBuild and the BuildEvent targets? Or is something wrong with my MSBuild install causing it to use this BuildSteps.targets file instead of something else?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>As Alexey points out, using Before/AfterTarget provides a usable workaround. You just have to take care of which targets to use, but this is easy by looking at the BuildSteps file. This seems to work fine for now:</p>
  
  <pre><code>&lt;Target Name="BeforeBuild" BeforeTargets="PrepareForBuild"&gt;
    &lt;Message Text="### BeforeBuild ###" /&gt;
  &lt;/Target&gt;
  &lt;Target Name="BeforeCompile" BeforeTargets="BuildCompile"&gt;
    &lt;Message Text="### BeforeCompile ###" /&gt;
  &lt;/Target&gt;
  &lt;Target Name="AfterBuild" AfterTargets="Build"&gt;
    &lt;Message Text="### AfterBuild ###" /&gt;
  &lt;/Target&gt;
  </code></pre>