Darien Pardinas
Darien Pardinas

Reputation: 6186

Trick to run different pre/post build events when building with MSBuild or from Visual Studio IDE

Don't ask me why, but does anyone know any trick to put in the pre/post build event command that will run different commands if the project is being built from command line with MSBuild or from inside the Visual Studio IDE?

Upvotes: 1

Views: 1237

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100543

The easiest solution would be to define build targets that are conditioned on the $(BuildingInVisualStudio) property that visual studio sets to true when buildinging.

<Target Name="SpecialPreBuild" BeforeTargets="BeforeBuild" Condition="'$(BuildingInVisualStudio)' != 'true'">
  <Exec Command="some-command.exe --magic" />
  <Copy SourceFiles="foo.txt" DestinationFolder="bin\$(Configuration)\bar" />
</Target>

<Target Name="SpecialPostBuild" AfterTargets="AfterBuild" Condition="'$(BuildingInVisualStudio)' != 'true'">
  <Exec Command="some-other-command.exe --magic" />
</Target>

If you want to skip these targets in other IDEs / editors as well, you could introduce a custom property as well and change the Condition attributes above to

Condition="'$(PerformSpecialLogic)' == 'true'"

That way no "default" builds will execute these targets and you could build with the following arguments in your build script / CI definition:

msbuild /p:PerformSpecialLogic=true

Upvotes: 3

Related Questions