Replicating OnSuccess and OnFailure in MSBuild

As you may have read in an earlier post, I'm currently helping a customer migrate their project from Visual Studio .NET 2003 to Visual Studio 2005. Our current build script uses NAnt, and we're in the process of converting it to MSBuild. Since we're big fans of continuous integration, and we depend on our Ambient Orb for at-a-glance build status, we have a need to replicate the behavior of NAnt's OnSuccess and OnFailure properties.

I wasn't able to find similar MSBuild functionality by browsing through the limited information I have (or can find on the internet), so I sent an e-mail to our internal team, and Alex Kipman was kind enough to suggest this solution:

<Project DefaultTargets="A;OnSuccess" >

      <Target Name="A" DependsOnTargets="B;C;D" >

            <OnError Targets="OnFailure" />



      <Target Name="OnFailure" >

            <DoSomething />



      <Target Name="OnSuccess" >

            <DoSomething />




Although this technique isn't as intuitive as the NAnt solution, it does work.

Comments (4)
  1. Did they miss a "DependsOnTargets=A" in target "OnSuccess" ?

    If this is not present, then targate A and OnSuccess might be executed in parallel, or?



  2. Jochen:

    No, the "DefaultTargets" on the Project tag specifies that Target A should be executed before Target OnSuccess. If "A" fails, it will run "OnFailure" and the build will complete. If no failure occurs while executing "A", then "OnSuccess" will be called and the build script will end.

  3. Incorporating a Repository in the Virtual Cellar

Comments are closed.

Skip to main content