35

can someone explain the differences between these:

<Target Name="AfterBuild">
    <!-- task here -->
</Target>

and:

<PropertyGroup>
<PostBuildEvent>copy "$(ProjectDir)\..\lib\$(PlatformName)\x.dll" .</PostBuildEvent>
</PropertyGroup>

Thank you.

Sako73
  • 9,957
  • 13
  • 57
  • 75

2 Answers2

28

Both PostBuildEvent and AfterBuild are MSBuild targets. The difference between the two is the conditions around when they are invoked

  • AfterBuild: This runs as the last action in the Build target and does so irrespective of whether or not a build succeeds. It runs after PostBuildEvent (if it runs at all)
  • PostBuildEvent: This runs conditionally after a build completes. When it runs is very configurable but in general it will only run if a build successfully completes and produces new output.
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
22

The PostBuildEvent property is able to hold a command that is passed as the Command attribute to an Exec task. Essentially you end up with a target that looks like this,

<Target Name="PostBuildEvent">
   <Exec Command="$(PostBuildEvent)" />
</Target>

You can configure the conditions when this will be run with a setting in the IDE, by default it only runs on a successful build.

The AfterBuild target is able to contain arbitrary MSBuild tasks, including one or more Exec tasks or any other task available to MSBuild, which allows for greater complexity.

In terms of when they are executed, the PostBuildEvent target runs just prior to "CoreBuild" while the "AfterBuild" target will run after "CoreBuild". If the placement is critical, you can make your own target and wire it into whereever in the build you need it to run, using the $(DependsOn..) declarations, or by specifying BeforeTargets and AfterTargets on your new target.

Brian Kretzler
  • 9,748
  • 1
  • 31
  • 28
  • PostBuildEvent runs after CoreBuild – JaredPar May 25 '11 at 18:09
  • 3
    Well, technically the PostBuildEvent runs at the end of the CoreBuild dependent targets, which all execute prior to the CoreBuild target running, but then CoreBuild is just an empty target that only seems to exist as a junction point for its DependsOn target list. It doesn't run "after" CoreBuild, just toward the end of everything useful CoreBuild causes to run. See Microsoft.Common.targets ~line 560 (2010 SP1). – Brian Kretzler May 25 '11 at 23:42
  • oh yes I forget it is a part of the DependsOn list so yes it does run before. I constantly read that as CoreBuild executes ... instead of CoreBuild DependsOn ... – JaredPar May 26 '11 at 00:00