I recently had to move some code from a PreBuildEvent in Visual Studio into the BeforeBuild target to make it work on AppHarbor. While doing so, I also noticed a BeforeCompile target.
What is the difference between these three seemingly similar events: PreBuildEvent, BeforeBuild Target, BeforeCompileTarget?
What can/can't be done with each, and why would you pick one over another?
The answer to this question can be found in the
Microsoft.Common.targets
file which can be found (depending on wether you're using the 64-bit or 32-bit framework) at:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target
for 64-bit andC:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
for the 32-bit runtime. This file defines all the steps a build of your project undergoes. Quoting the source:The code is nice enough to explain the use of the
BeforeBuild
andAfterBuild
target in the comments for both targets.This is followed by the definition of the
CoreBuild
target:So the
Build
target is just a wrapper around theCoreBuild
target to enable you to perform custom steps just before or after theCoreBuild
target. As can be seen above thePreBuildEvent
andPostBuildEvent
are listed as dependencies of theCoreBuild
target. The dependencies of theCompile
target are defined as follows:Again
BeforeCompile
andAfterCompile
are commented in the code:Given this information I do not know why AppHarbor does not support
Pre-, PostBuildEvent
while theBuild
can be modified usingBefore-, AfterBuild
.Choosing which
Target
to override for which scenario depends on the moment during the build at which you wish to perform your given task. The targets do not have specific restrictions and/or benefits as to what they can accomplish. Apart from the fact that they can adaptItemGroup
's or properties that were defined/filled by previous steps.Using nuget to bring in packages is probably best performed before the build tries to resolve the projects dependencies. So
BeforeCompile
is not a good candidate for this kind of action.I hope this sheds some light on the matter. Found another nice explanation on MSDN