Share via


How To: Add Custom Process at Specific Points During Build (Method #2)

Back in November I wrote about a quick-and-dirty way to add custom build process into the standard Visual Studio build by overriding pre-defined targets that exist in Microsoft.Common.Targets. While this method works fine for internal build processes it can be brittle if you’re trying to deploy your build process to other teams or customers. In particular, if someone else decides to go and override the same target after you’ve done so, your customization will disappear. To handle this situation we’ve provided a second way of inserting your custom build process into the Visual Studio build: overriding properties that are used in TargetDependsOn attributes. The easiest way to understand how to do this is to look at a short snippet from Microsoft.Common.Targets:

<Target Name="Build" DependsOnTargets="$(BuildDependsOn)"/>

Notice how the Build target has a DependsOnTargets that points to the BuildDependsOn property. That means before the Build target can run, all the targets listed in BuildDependsOn have to run first:

<PropertyGroup>

    <BuildDependsOn>

    BeforeBuild;

    CoreBuild;

    AfterBuild

    </BuildDependsOn>

</PropertyGroup>

If you want to add something to the build process that runs after everything else in the build, you’d simply create your own target and then add it to BuildDependsOn like this:

<PropertyGroup>

    <BuildDependsOn>

    $(BuildDependsOn);

    MyCustomTarget

    </BuildDependsOn>

</PropertyGroup>

<Target Name="MyCustomTarget">

    <Message Text="Hello from MyCustomTarget"/>

</Target>

Notice how we’ve included the original value of BuildDependsOn in our new definition of the property. Using this pattern means that anyone who overrides the existing meaning of BuildDependsOn will include all the existing content of the property, without overwriting customizations others have made. You can use a similar approach to get your custom target to run before everything in BuildDependsOn.

Here’s a list of the most commonly used *DependsOn properties:

Property Description
BuildDependsOn This is the property to use if you want to insert custom process before or after the entire build process. As you might guess, this is frequently used.
CleanDependsOn This is the property to override if you want to clean up output from your custom build process.
CompileDependsOn This is a very useful property as it provides a place to insert custom process before or after the compliation step. This is frequently overriden.

In addition to the three common properties there are many other properties that are almost never used. For completeness, here’s what they are and why you might use them:

Property Description
CompileLicxFilesDependsOn Targets in this property run before .licx files get compiled.
CoreBuildDependsOn This is the heavy-duty part of the build process and pretty much drives the whole build.
CoreCleanDependsOn This is really provided for design pattern completeness. There’s no real reason to use this instead of CleanDependsOn.
CoreCompileDependsOn Interestingly this is one of only two *DependsOn properties that are defined in the language-specific targets files instead of Microsoft.Common.Targets. This is rarely used; instead, override CompileDependsOn.
CreateManifestResourceNamesDependsOn Run before the manifest resource name is created from .resx files. Rarely overridden.
CreateSatelliteAssembliesDependsOn Primarily used as a way to force intermediate satellite assemblies to be computed before the final satellite assemblies are regenerated.
GetRedistListsDependsOn While this is in the Common.Targets file and can technically be used to insert custom process around the collection of redistributable assemblies, this is really there to ensure framework path resolution happens before the lists are gathered. I doubt anyone will ever need to override this.
PostBuildEventDependsOn This runs before the PostBuildEvent target is executed. This is rarely overridden since PostBuildEvent largely exists to support the legacy build extension methods offered in Visual Studio 2003 and earlier. Instead of overriding this you should consider using BuildDependsOn.
PreBuildEventDependsOn This runs before the PreBuildEvent target is executed. This is rarely overridden since PreBuildEvent largely exists to support the legacy build extension methods offered in Visual Studio 2003 and earlier. Instead of overriding this you should consider using BuildDependsOn.
PrepareForBuildDependsOn This takes care of getting all the build pre-requisites done before the actual build process starts. You can think of targets in this property as being almost like initialization routines for the whole build.
PrepareForRunDependsOn This is a good place to insert build process that needs to get content into place before the application is executed from within Visual Studio. Having said that, it’s rarely overridden.
PrepareResourceNamesDependsOn Prepares the names of the resource files before they get passed to the complication step of the build.
PrepareResourcesDependsOn Prepares the resources before they get passed to the compliation step of the build.
PublishBuildDependsOn The list of targets that must run before the project is published. Rarely, if ever, used. Instead, use PublishOnlyDependsOn.
PublishDependsOn Runs before the project is published using ClickOnce deployment outside of the IDE. It causes a build to happen before publish takes place. If you need to do work before the project is published you should override PublishOnlyDependsOn instead, since PublishDependsOn includes a call to PublishOnlyDependsOn.
PublishOnlyDependsOn Run after the IDE has completed a build and then needs to do a publish (as opposed to a command-line build where build followed by publish might happen all in one build command). If you need to do work before or after the project is published this is the property to use.
RebuildDependsOn This drives the rebuild target, and handles clean and then re-building the project. If you want to do something before or after rebuild this is the property to override.
ResGenDependsOn This is used to drive the resource generation step of the build process. If you need to do something before or after resource generation, this is the property to override.
ResolveAssemblyReferencesDependsOn This has a similar story to GetRedistListsDependsOn. It’s unlikely anyone will ever need to override this.
ResolveReferencesDependsOn This is the workhorse for resolving assembly references, one of the most complicated parts of the build process. If you want to do special work to handle assembly resolution, or do work immediately following the resolution of all the assembly references, this is the property to use. Having said that, it’s rarely overridden.
RunDependsOn Targets in this property will get executed before the compiled application is run.
UnmanagedRegistrationDependsOn Targets in this property are run before the main assembly is registered for COM interop.
UnmanagedUnregistrationDependsOn This runs before the main assembly is registered for COM interop purposes.
BuiltProjectOutputGroupDependsOnDebugSymbolsProjectOutputGroupDependsOnDocumentationProjectOutputGroupDependsOnSatelliteDllsProjectOutputGroupDependsOnSourceFilesProjectOutputGroupDependsOnContentFilesProjectOutputGroupDependsOn Targets in these properties are run before output groups are computed for the IDE.

[ Author: Neil Enns ]

Comments

  • Anonymous
    June 04, 2006
    Hi! http://www.ringtones-dir.com/get/ ringtones site. Download ringtones FREE, Best free samsung ringtones, Cingular ringtones and more. From website .

  • Anonymous
    June 08, 2006
    Here's a couple of MSBuild gotchas...
    When specifying paths to your Include / Exclude ItemGroups make...

  • Anonymous
    November 30, 2006
    > ResolveAssemblyReferencesDependsOn This has a similar story to GetRedistListsDependsOn. It’s unlikely anyone will ever need to override this. Er. well, I did. I'm copying assemblies into Bin based on configuration. If I do it before BuildDependsOn I get an error that my assembly references can't be found. If I do ti before ResolveAssemblyReferencesDependsOn, Bingo! we're good.

  • Anonymous
    January 03, 2007
    Hello, nice site look this: [URL="http://fm7.biz/qjf"]coach handbags[/URL] End ^) See you

  • Anonymous
    January 07, 2007
    http://rw.da.cx  <a href="http://rw.da.cx">replica watches</a> replica watches

  • Anonymous
    May 11, 2007
    iq0108mi3g1o <a href = http://www.1042812.com/1058720.html > 6uo133en0 </a> [URL=http://www.494219.com/881071.html] 0i35eg5zzrx [/URL] fqzdrjr7

  • Anonymous
    May 19, 2007
    The comment has been removed

  • Anonymous
    September 25, 2007
    PingBack from http://blog.sneal.net/Blog/DontOverrideMSBuildTargetsAppend.aspx

  • Anonymous
    January 04, 2008
    <a href= http://index1.wupiwy.com >fetal development</a> <a href= http://wupiwy.com >fresh news</a>

  • Anonymous
    February 20, 2008
    Od jakiegoś czasu porzuciłem poczciwego NAnt-a na rzecz MSBuild-a. Gł&#243;wnym powodem była chęć ujednolicenia

  • Anonymous
    April 10, 2008
    Up until about an hour ago, I'd been using post-build events on my Visual Studio projects to create SharePoint

  • Anonymous
    April 15, 2008
    I've started digging into Silverlight pretty heavily over the past couple of weeks.&#160; My main interest

  • Anonymous
    July 15, 2008
    W ramach dalszych odcink&#243;w będę budował od podstaw pliki do MSBuild na przykładzie prostego projektu

  • Anonymous
    January 15, 2009
    <a href= http://wenlaro.angelfire.com >xeroderma pigmentosum ongoing research</a> <a href= http://jitfbzb.angelfire.com >highland towers apartment</a> <a href= http://eudauau.angelfire.com >play law and order online for free</a> <a href= http://vzairnr.angelfire.com >hugo salzar</a> <a href= http://qnwofuv.angelfire.com >how tall will my child be</a>

  • Anonymous
    June 19, 2009
    PingBack from http://heyse.us/blog/?p=112

  • Anonymous
    October 25, 2010
    I'm not sure about the others, but the comment about PostBuildEventdependsOn might not be quite accurate. "This runs before the PostBuildEvent target is executed. This is rarely overridden since PostBuildEvent largely exists to support the legacy build extension methods offered in Visual Studio 2003 and earlier. Instead of overriding this you should consider using BuildDependsOn." In fact, you would want to override this anytime you have targets that need to run after the build but BEFORE any post build batch file commands defined in the IDE's Advanced Compile options screen. For instance, I'm using a post build code weaver to take the compiled DLL, decompile it to IL, tweak it and recompile back to a dll. I need to do that BEFORE the postbuildevent, because I have a command to copy the final DLL to an alternate location in my PostBuildEvent commands option. If I didn't use PostBuildEventDependsOn, I'd end up copying the wrong version of the dll in the postbuildevent.

  • Anonymous
    November 06, 2010
    No matter what I try (and I've been working on this for 2 days) I cannot get a Task to "TransformXml" because I want to transform the web.configs during a build.  I have seen all your posts online and absolutely none of them work.  Can anyone tell me if something changed in the final 2010 release that I cannot do this?

  • Anonymous
    November 06, 2010
    I did eventually figure this out.  I was using a ".target" file instead of just using the ".csproj" file.  What I didn't realize is that you have to close and reopen Visual Studio 2010 every time you make a change to your "target" file.  If you don't it will not read any of your changes.

  • Anonymous
    March 20, 2011
    cocktail dresses varies depending on fashion and local custom. It ranges from just above the knee to touching the ankle. When the length is 2 2 <b><a href=www.clothing-wholesale.org/.../bridal-gown-126-b0.html>modest bridal gowns</a></b> 2 inches above the ankle it is called tea length which is usually worn elsewhere. However, the shorter-length cocktail dresses are now much more sought-after