none
How to skip building the current project and move on to the next one in the pre-build event RRS feed

  • Question

  • I have a condition check in my pre-build event and if the condition passes then I want to skip building the current project and move on to the next project in the solution.  How do I specify this skip in the pre-build event or some other way to do it?  Thanks
    • Moved by Chao KuoModerator Monday, April 19, 2010 9:28 AM (From:Visual Studio Extensibility)
    Thursday, April 15, 2010 5:32 PM

Answers

  • Hi Chao,

    I do agree that in the PreBuildEvent command which is executed by Exec task you can't change anything in MSBuild project.  The solution I'm proposing is to use a new target "CheckIfBuildIsNeeded" instead of PreBuildEvent and wire it up to the build process by overwriting BuildDependsOn. 

    I'd also like to mention that you can get the exit code from Exec task and use it in the build, so the check might look like below:

     <PropertyGroup>
     <BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
     <BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
     </PropertyGroup>
    
     <Target Name="CheckIfBuildIsNeeded">
     <!-- Execute command here that checks if proceed with the build and sets the exit code -->
     <Exec
      Command="exit /b 1"
      WorkingDirectory="$(SourcesPath)"
      IgnoreExitCode="true"
      >
      <Output TaskParameter="ExitCode" PropertyName="ExecExitCode"/>
     </Exec>
     <Message Text="Exit Code: $(ExecExitCode)" Importance="high" />
     <PropertyGroup Condition="'$(ExecExitCode)' == '1'">
      <DoBuild>false</DoBuild>
     </PropertyGroup>
     </Target>
    
     <Target
     Name="BuildWrapper"
     Condition=" '$(DoBuild)' != 'false' "
     DependsOnTargets="$(BuildWrapperDependsOn)"
     Returns="$(TargetPath)" />
    
    

    Cheers,
    Ilya

    Wednesday, April 21, 2010 5:28 PM
  • Hi, queandansUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals

    As far as I invested, this could not be done, Pre-build event is just to execute some DOS command (like copy, set environment variable), this command could not prevent our MsBuild to run.

    Because MsBuild command line have a /target option that enable us to determine which targets to run. So I think the following batch to build is optional.

    If ….

    (MsBuild Yourproject)

    Else

    (MsBuild Youproject /target:Afterbuild) // this enable you to skip the build target.

    If I misunderstood you, feel free to let me know.

    Thanks

    Chao

    Monday, April 19, 2010 12:20 PM
    Moderator
  • Hi,

    Well, first of all, if you pre-build event is executed, the build has already started.  So you probably want to skip just part of it.

    If you want to skip the Build target, you can override BuildDependsOn property with your own target and wrap Build target for you project to not build if you don't need it, like below:

     <PropertyGroup>
     <BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
     <BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
     </PropertyGroup>
    
     <Target
     Name="CheckIfBuildIsNeeded">
     <PropertyGroup Condition="....">
     <DoBuild>false</DoBuild>
     </PropertyGroup>
     </Target>
    
     <Target
     Name="BuildWrapper"
     Condition=" '$(DoBuild)' != 'false' "
     DependsOnTargets="$(BuildWrapperDependsOn)"
     Returns="$(TargetPath)" />
    

    For other targets there are other TargetDependsOn properties you can override

    Monday, April 19, 2010 9:22 PM

All replies

  • Hi, queandansUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals

    As far as I invested, this could not be done, Pre-build event is just to execute some DOS command (like copy, set environment variable), this command could not prevent our MsBuild to run.

    Because MsBuild command line have a /target option that enable us to determine which targets to run. So I think the following batch to build is optional.

    If ….

    (MsBuild Yourproject)

    Else

    (MsBuild Youproject /target:Afterbuild) // this enable you to skip the build target.

    If I misunderstood you, feel free to let me know.

    Thanks

    Chao

    Monday, April 19, 2010 12:20 PM
    Moderator
  • Hi,

    Well, first of all, if you pre-build event is executed, the build has already started.  So you probably want to skip just part of it.

    If you want to skip the Build target, you can override BuildDependsOn property with your own target and wrap Build target for you project to not build if you don't need it, like below:

     <PropertyGroup>
     <BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
     <BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
     </PropertyGroup>
    
     <Target
     Name="CheckIfBuildIsNeeded">
     <PropertyGroup Condition="....">
     <DoBuild>false</DoBuild>
     </PropertyGroup>
     </Target>
    
     <Target
     Name="BuildWrapper"
     Condition=" '$(DoBuild)' != 'false' "
     DependsOnTargets="$(BuildWrapperDependsOn)"
     Returns="$(TargetPath)" />
    

    For other targets there are other TargetDependsOn properties you can override

    Monday, April 19, 2010 9:22 PM
  • Hi, Biryukov

    I don’t think you could override any properties of your MsBuild Script in your preBuildEvent.

    Here is what it is defined, if you specify the PreBuildEvent.

      <PropertyGroup>

        <PreBuildEvent>dir</PreBuildEvent>

      </PropertyGroup>

    And how the PreBuildEvent is executed, was defined in Microsoft.Common.Targets.

    Here is

        <!--

        ============================================================

                                            PreBuildEvent

     

        Run the pre-build event if there is one.

        ============================================================

        -->

        <PropertyGroup>

            <PreBuildEventDependsOn></PreBuildEventDependsOn>

        </PropertyGroup>

        <Target

            Name="PreBuildEvent"

            Condition="'$(PreBuildEvent)'!=''"

            DependsOnTargets="$(PreBuildEventDependsOn)">

     

            <Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" />

     

        </Target>

    The exec task could only execute some system commands or some bats, it could not override the properties.

    Thanks

    Chao

    Tuesday, April 20, 2010 2:41 AM
    Moderator
  • Hi Chao,

    I do agree that in the PreBuildEvent command which is executed by Exec task you can't change anything in MSBuild project.  The solution I'm proposing is to use a new target "CheckIfBuildIsNeeded" instead of PreBuildEvent and wire it up to the build process by overwriting BuildDependsOn. 

    I'd also like to mention that you can get the exit code from Exec task and use it in the build, so the check might look like below:

     <PropertyGroup>
     <BuildWrapperDependsOn>$(BuildDependsOn)</BuildWrapperDependsOn>
     <BuildDependsOn>CheckIfBuildIsNeeded;BuildWrapper</BuildDependsOn>
     </PropertyGroup>
    
     <Target Name="CheckIfBuildIsNeeded">
     <!-- Execute command here that checks if proceed with the build and sets the exit code -->
     <Exec
      Command="exit /b 1"
      WorkingDirectory="$(SourcesPath)"
      IgnoreExitCode="true"
      >
      <Output TaskParameter="ExitCode" PropertyName="ExecExitCode"/>
     </Exec>
     <Message Text="Exit Code: $(ExecExitCode)" Importance="high" />
     <PropertyGroup Condition="'$(ExecExitCode)' == '1'">
      <DoBuild>false</DoBuild>
     </PropertyGroup>
     </Target>
    
     <Target
     Name="BuildWrapper"
     Condition=" '$(DoBuild)' != 'false' "
     DependsOnTargets="$(BuildWrapperDependsOn)"
     Returns="$(TargetPath)" />
    
    

    Cheers,
    Ilya

    Wednesday, April 21, 2010 5:28 PM
  • Good work!
    Thursday, April 22, 2010 2:36 AM
    Moderator