How To: Insert Custom Process at Specific Points During Build


Today Kieran, Faisal, and I met with one of our partners to give them an introduction to MSBuild. During the discussion one of them asked us about the different places someone can plug into the standard Visual Studio build process (as defined by the Microsoft.Common.Targets file).


In general the way you hook into the existing build process is by re-defining targets that exist in Microsoft.Common.Targets in either your project file or in your own .targets file. Faisal wrote about the specifics of what the XML looks like in an earlier post, but it doesn’t look like we have a list of those targets documented anywhere (other than in Microsoft.Common.Targets!), so here’s an easier to parse list:




























Target Name Description
BeforeBuild
AfterBuild
These are primary for legacy support with prior versions of Visual Studio 2005. Tasks inserted in these targets will run before and after everything else in the build.
BeforeRebuild
AfterRebuild
Tasks inserted in these targets run before and after the core rebuild functionality is invoked. The flow is: BeforeRebuild, Clean, the default target (Build), and After Rebuild.
BeforeClean
AfterClean
Tasks inserted in these targets run before and after the core clean functionality is invoked.
BeforePublish
AfterPublish
Tasks inserted in these targets run before and after the core publish functionality is invoked.
BeforeResolveReferences
AfterResolveReferences
Tasks inserted in these targets run before and after assembly references are resolved.
BeforeCompile
AfterCompile
Tasks inserted in these targets run before and after core compliation is done. Typically most custom work is done in one of these two targets.
BeforeResGen
AfterResGen
Tasks inserted in these targets run before and after resources are generated.

[ Author: Neil Enns ]

Comments (6)

  1. Sayed Ibrahim Hashimi says:

    "it doesn’t look like we have a list of those targets documented anywhere (other than in Microsoft.Common.Targets!)"

    There is this post on the MSBuild forum at:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=58137&SiteID=1

    By Dan Moseley

  2. Mike Fourie says:

    Yes, Dan’s on our team and he wrote a great post in the forum. It doesn’t, however, include the specific list of targets you can override in the build process.

    Neil

  3. Judah says:

    Hi guys

    I’m trying to obfuscate an assembly. It works fine until I go to publish, that’s when the compiler publishes an assembly without the obfuscation.

    How can I obfuscate my assembly so that it gets published as obfuscated? At what build target should i obfuscate? I tried the BeforePublish target but nothing seems to happen. Any ideas?

  4. About a week ago a VB developer, who I was giving an overview to an .NET application, asked about creating

  5. From the description of BeforeBuild and AfterBuild "These are primary for legacy support …" Is this being confused with the PreBuildEvent and PostBuildEvent target/properties? While I don’t like BeforeBuild and AfterBuild targets myself too much, I still suggest people to use it for non-reusable project files.