HOW TO: Beyond PreBuild and PostBuild Steps

Do you use PreBuild and PostBuild steps that are provided in the IDE? You will find that they are still available in Visual Studio 2005 - but from within the IDE, you only get the ability to run command line batch files / scripts. Essentially, prebuild and postbuild steps get executed using the <Exec> task.

It's all well and good - except we already provide a rich set of tasks for you to use so that you have more flexibility and control over your build. For instance, using the <Copy> task instead of the copy command gives you richer programmatic control over that step. So, what if you wanted to implement PreBuild and PostBuild using MSBuild tasks?

Open up your project file for edit (using the supercool tip I mentioned earlier). You will see that all Visual Studio projects have two targets that are commented out - <BeforeBuild> and <AfterBuild> . All you need to do is really have targets with those names in your project (we call it target overriding - because those targets are actually pre-defined in the Microsoft.Common.targets file that contains the Visual Studio Build process). So it's these targets that will allow you to implement your pre-build and post-build steps using MSBuild tasks instead of relying on command line scripts.

Here's an example of how it would look in the project file:


Target Name="BeforeBuild">
<Message Text="This is the BeforeBuild target" Importance="normal"/>
<!-- Insert other tasks to run before build here -->
<Target Name="AfterBuild">
<Message Text="This is the AfterBuild target" Importance="normal"/>
<!-- Insert other tasks to run after build here -->

[Author: Faisal Mohamood]