Make a target run once at the Solution level in MSBuild

后端 未结 2 2063
一向
一向 2021-02-05 16:20

I need a set of tasks that need to be executed exactly once for the entire solution. This will run tasks that will modify each project to run a separate set of tasks for each pr

相关标签:
2条回答
  • 2021-02-05 17:02

    An alternative solution is to have a single target that dispatches to an MSBuild invoked target with as many Global properties removed as possible. My team have a target in the InitialTargets of a Directory.Build.props Import'ed props file - something like:

    <Target Name="Prebuild">
      <MSBuild Projects="$(MSBuildThisFileFullPath)"
               Targets="PrebuildWorker"
               RemoveProperties="Configuration;Platform;TargetFramework;BuildProjectReferences" />
    </Target>
    

    Since MSBuild appears to synchronize parallel builds on the {project file, global properties, target} set, then by removing all of the properties you can synchronize the build and run it once for all projects being built. The downside: you have to maintain the RemoveProperties attribute - MSBuild doesn't have a way to remove all global properties. If something in the build issues a new MSBuild task with a custom property specified, then you'll get a second instance of the Target invoked.

    And - of course - your build will be synchronized on this target. You could try hooking the target up by setting, say, CompileDependsOn to depend on the Prebuild target, to allow independent progress in the build. But to have the target run early and ubiquitously using InitialTargets seems like the better option.

    0 讨论(0)
  • 2021-02-05 17:05

    Since Solution files are not in MSBuild format they are not easily extended or customized. If you want more control over the build process you would have to create a "driver" msbuild file which would replace your solution file. Inside this driver file you would build all the projects that you needed and perform some additional tasks. You would do this using the MSBuild task. Here is a sample showing how to build more than 1 project.

    <Project ...>
        <ItemGroup>
            <Projects Include="proj01.csproj"/>
            <Projects Include="proj02.csproj"/>
            <Projects Include="proj03.csproj"/>
        </ItemGroup>
    
        <Target Name="BuildAll">
            <MSBuild Projects="@(Projects)" BuildInParallel="true" />
        </Target>
    
    </Project>
    

    So in your case you would just execute the tasks before you build the projects. Also note that I specified the value true for the BuildInParallel indicating that MSBuild can try and build more than one project at once.

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