Can you prevent MSBuild.exe from running Build Events?

EventsBuildMsbuild

Events Problem Overview


I'm building a number of projects via a script, and the occasional use of custom build events causes a great deal of difficulty for the build system. If it is possible, I'd like to invoke MSBuild.exe in such a was as to block the execution of any build events. In the long run, this is not an issue for build automation - submitters of projects with build events are forewarned that such snafuery is against the rules.

In short, is there a way to call MSBuild that will prevent the execution of any custom build steps, if present?

UPDATE:

I'd considered doing an in-place (automated) edit of the project files, but would prefer the command-line equivalent of setting "Excluded From Build" (see the Build Events options) to Yes for each of the three events.

Events Solutions


Solution 1 - Events

Pre/PostBuildEvents are properties, so to override them just set them from command line to empty string.

msbuild foo.sln /p:PreBuildEvent= /p:PostBuildEvent=

Solution 2 - Events

It seems that the answer is different depending on the project type.

For C/C++ projects (.vcxproj), you can suppress the PostBuildEvent on the command line with /p:PostBuildEventUseInBuild=false, as AndreiM suggests.

(Setting /p:PostBuildEvent to an empty string doesn't work for C++ projects, and I can't find any other way to override the post-build command).

For C# projects (.csproj), you can suppress the PostBuildEvent on the command line with /p:PostBuildEvent=, as most other responders suggest.

Solution 3 - Events

You could also make the property conditional

<PreBuildEvent Condition="'$(BuildingInsideVisualStudio)' == '' Or '$(BuildingInsideVisualStudio)' == true"> ... </PreBuildEvent>

Solution 4 - Events

What I would do is define a new .proj file, lets say C:\Data\SupressBuildEvents.proj and it would contain:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="PostBuildEvent"/>
    
    <Target Name="PreBuildEvent" />
</Project>

Then you need to specify that these files get imported after Microsoft.Common.targets and you would do so by defining the well known property CustomAfterMicrosoftCommonTargets to the path of that file. So lets your build script is in a file named MyBuild.proj you would invoke it as:

msbuild MyBuild.proj /p:CustomAfterMicrosoftCommonTargets="C:\Data\SupressBuildEvents.proj
"

This will cause MSBuild to import your file after the Microsoft.Common.targets file gets imported and it will override the PostBuildEvent and PreBuildEvent targets and make them do nothing.

Now if your MyBuild.proj files uses the MSBuild task to build other targets then you should also pass them this property as follows:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <ProjectsToBuild Include=" FILL THIS IN "/>
    </ItemGroup>

    <Target Name="YourTarget">
        <MSBuild Projects="@(ProjectsToBuild)"
                 Properties="CustomAfterMicrosoftCommonTargets=$(CustomAfterMicrosoftCommonTargets)"/>
    </Target>

</Project>

This is necessary because by design properties on the parent script are not passed to the builds executed by the MSBuild task.

Solution 5 - Events

I also played a little with msbuild foo.vcxproj /p:PreBuildEvent= /p:PostBuildEvent=, but for me it didn't work, probably because I am using custom props files.

What I found to work however was /p:PostBuildEventUseInBuild=false

Solution 6 - Events

You can also set the properties within your MSBuild-task:

<MSBuild 
  Projects="$(MSBuildProjectDirectory)\YourProject.csproj" 
  Properties="Configuration=$(BuildConfiguration);BuildingFromBuildProjXml=true;PreBuildEvent=;PostBuildEvent=" 
  ContinueOnError="false" />

Solution 7 - Events

In some C# projects I need PostBuildEvent works in Visual studio build, but not works in MSBuild and TFS build. For this purpose I add PostBuildEvent in VS. It sets below code in .csproj:

  <PropertyGroup>
    <PostBuildEvent>*... my custom post build event ....*</PostBuildEvent>
  </PropertyGroup>

after that I add below codes to .csproj:

  <Target Name="BeforeBuild">
	  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'false' Or '$(BuildingInsideVisualStudio)' != 'true'">
		<PostBuildEvent></PostBuildEvent>
	  </PropertyGroup>
  </Target>

This code sets PostBuildEvent to empty and it occurs only in MSBuild and TFSBuild BeforeBuild target. It is simple, permanent, not need to set params and works fine.

Solution 8 - Events

<Target Name="PreBuild" BeforeTargets="PreBuildEvent" 
Condition="'$(VisualStudioDir)' != ''">

Adding this Condition to the Target for the PreBuild in the project csproj file is the only solution that worked for me. I ran into this issue trying to automate a build within VSTS where I wanted to skip the PreBuild event defined in the project csproj file. Using Visual Studio 2017, a .NET Core 2.0 project.

I tried the msbuild command line suggestions listed here but none of them worked for me.

Solution 9 - Events

By default when you import Microsoft.Common.targets you get

<PropertyGroup>
    <BuildDependsOn>
        BeforeBuild;
        CoreBuild;
        AfterBuild
    </BuildDependsOn>
</PropertyGroup>

I think you can maybe just replace it with

<PropertyGroup>
    <BuildDependsOn>
        CoreBuild
    </BuildDependsOn>
</PropertyGroup>

to turn off these pre/post build events. (Not sure if you need to put that in your .proj file before or after the import, or if you need to modify Microsoft.Common.targets to have such an effect. Don't have time to experiment right now...)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSniggerfardimungusView Question on Stackoverflow
Solution 1 - EventsradicalView Answer on Stackoverflow
Solution 2 - Eventsdtopham75View Answer on Stackoverflow
Solution 3 - EventsSarinView Answer on Stackoverflow
Solution 4 - EventsSayed Ibrahim HashimiView Answer on Stackoverflow
Solution 5 - EventsAndreiMView Answer on Stackoverflow
Solution 6 - EventsderFunkView Answer on Stackoverflow
Solution 7 - EventsAhmadYoView Answer on Stackoverflow
Solution 8 - EventsNeil SchurrerView Answer on Stackoverflow
Solution 9 - EventsBrianView Answer on Stackoverflow