views:

328

answers:

3

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.

A: 

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...)

Brian
This is not what you want to do. BeforeBuild and AfterBuild are not related to pre and post build events.
Sayed Ibrahim Hashimi
+3  A: 

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

msbuild foo.sln /p:PreBuildEvent= /p:PostBuildEvent=
Ankit
+1  A: 

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"&gt;
    <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"&gt;
    <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.

Sayed Ibrahim Hashimi