5

I asked yesterday about getting AfterBuild working and was able to get that working by placing it at the very bottom of the Project section: MSBuild AfterBuild Step

I have tried the exact same thing with a different project. This is my BeforeBuild code:

<Target Name="BeforeBuild">
    <Message Text="###HI###" Importance="High" />
</Target>

I don't have another before Target Name="BeforeBuild" in the project and I have this placed at the very bottom of the Project section. The .vcxproj is parsing/loading into Visual Studio 2010 without issue. I have my "MSBuild project build output/log verbocity" set to "Normal".

Is there something else I haven't thought of that would cause this not to run?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 2
    Hmm, C++ builds don't have a BeforeBuild target. I don't see a good one to use, only decent candidate I see is the "PreBuildEvent" target. But do beware that you'll disable the Pre-Build Event as defined in the settings when you do this. – Hans Passant Nov 06 '14 at 21:39
  • As long as Target declarations are at the level allowed by the schema, their location doesn't change anything. – Tom Blodget Nov 07 '14 at 05:49
  • @HansPassant I don't know if you would have any insight as to why, but how come `AfterBuild` is allowed in Visual C and `BeforeBuild` is not? – Jonathan Mee Nov 07 '14 at 12:08
  • 1
    Hmya, it is subjective but C++ building is entirely too convoluted. The build model dates from the 1970s and picked up lots of cruft over the years. Too many "before" targets. Everybody has this problem, building such a common library as Boost goes wrong *way* too often for example. Well, if it was easy then anybody could do it :) – Hans Passant Nov 07 '14 at 12:37

1 Answers1

8

A target is not called unless called explicitly, is a default target, is declared in DependsOn of a target that is called or declare its own BeforeTargets or AfterTargets and one of those targets is called.

So, if you want a target to be called before a target called "InitializeBuildStatus", you can write it like this:

<Target Name="MyBeforeBuild" BeforeTargets="InitializeBuildStatus">
    <Message Text="###HI###" Importance="High" />
</Target>

It'll run if the "InitializeBuildStatus" target runs. (You can enable verbose logging in Visual Studio or for an MSBuild run to help determine the appropriate BeforeTarget targets.)

The "BeforeBuild" and "AfterBuild" targets are called explicitly by the target system of some of Microsoft's project types. That offers only limited extensibility. The newer (.NET 4.0) way is BeforeTargets and AfterTargets.

See Target Build Order.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • This is the solution, however, if you do `BeforeTargets="Build"` it always runs right before "BuildSucceeded". _After_ compiling and everything else has happened. If you will change your `BeforeTargets="Build"` to `BeforeTargets="InitializeBuildStatus"` I will accept. – Jonathan Mee Nov 07 '14 at 12:25