visual-studiomsbuildnestedmsbuild-batching

Nested target batching in MSBuild?


I want to execute an action per DeploymentTarget, of which there can be more than 1 per ProjectsForDeployment. I know if there was only 1 DeploymentTarget child per ProjectsForDeployment, the target batching would work - but what happens if there's multiple children? Is it still possible to run ProcessDeployableObject 4 times against A1/A2/B1/B2 whilst maintaining references to the siblings and parent?

Thanks!

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="Deploy" >
    <CallTarget Targets="ProcessDeployableProject" />
  </Target>
  
  <Target Name="ProcessDeployableProject" Inputs="@(ProjectsForDeployment)" Outputs="%(ProjectsForDeployment.Identity)\dummy.txt" >
      <!-- Processing goes here for %(ProjectsForDeployment.DeploymentTarget), needs OtherMetadataWeNeed and ProjectsForDeployment.Identity too. -->
  </Target>

  <ItemGroup>
    <ProjectsForDeployment Include="/src/A/A.csproj" >
      <DeploymentTarget>A1</DeploymentTarget>
      <DeploymentTarget>A2</DeploymentTarget>
      <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
    </ProjectsForDeployment>
    <ProjectsForDeployment Include="/src/B/B.csproj" >
      <DeploymentTarget>B1</DeploymentTarget>
      <DeploymentTarget>B2</DeploymentTarget>
      <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
    </ProjectsForDeployment>
  </ItemGroup>
</Project>

Solution

  • To run four times, you should write four items rather than two items.

    In your side, DeploymentTarget A2 will be overwritten with A1 because they are under the same item.

    To solve it, you have to separate them which is more like a CSProj running with the DeploymentTarget one at a time. And when you build your project with msbuild command line, each command should work with one Configuration and Platform.

    Use this:

    <ItemGroup>
        <ProjectsForDeployment Include="/src/A/A.csproj" >
          <DeploymentTarget>A1</DeploymentTarget>
          <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
        </ProjectsForDeployment>
     
        <ProjectsForDeployment Include="/src/A/A.csproj" >
          <DeploymentTarget>A2</DeploymentTarget>
          <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
        </ProjectsForDeployment>
    
    
        <ProjectsForDeployment Include="/src/B/B.csproj" >
          <DeploymentTarget>B1</DeploymentTarget>    
          <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
        </ProjectsForDeployment>
    
        <ProjectsForDeployment Include="/src/B/B.csproj" >
          <DeploymentTarget>B2</DeploymentTarget>    
          <OtherMetadataWeNeed>Metadata</OtherMetadataWeNeed>
        </ProjectsForDeployment>
      </ItemGroup>