Can you prevent MSBuild.exe from running Build Events?

后端 未结 9 1784
我在风中等你
我在风中等你 2020-12-08 00:11

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\

相关标签:
9条回答
  • 2020-12-08 00:33

    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.

    0 讨论(0)
  • 2020-12-08 00:38

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

    <MSBuild 
      Projects="$(MSBuildProjectDirectory)\YourProject.csproj" 
      Properties="Configuration=$(BuildConfiguration);BuildingFromBuildProjXml=true;PreBuildEvent=;PostBuildEvent=" 
      ContinueOnError="false" />
    
    0 讨论(0)
  • 2020-12-08 00:38
    <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.

    0 讨论(0)
  • 2020-12-08 00:40

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

    msbuild foo.sln /p:PreBuildEvent= /p:PostBuildEvent=
    
    0 讨论(0)
  • 2020-12-08 00:40

    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.

    0 讨论(0)
  • 2020-12-08 00:48

    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

    0 讨论(0)
提交回复
热议问题