Target Batching notes

When batching a target, we can not batch a series of calltarget since it will batch each calltarget one by one. See below sample script to see it.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
  <Target Name="MAIN">
    <CallTarget Targets="t" />
    <CallTarget Targets="tt" />
    <CallTarget Targets="ttB" />
  </Target>

  <Target Name="t">
    <ItemGroup>
      <aa Include="1;22;333" />
    </ItemGroup>
  </Target>

  <Target Name="tt" Outputs="%(aa.Identity)">
    <Message Text="This batching won't work!" />
    <CallTarget Targets="ttt" />
    <CallTarget Targets="tttt" />
  </Target>

  <Target Name="ttt">
    <Message Text="1:%(aa.Identity)" />
  </Target>

  <Target Name="tttt">
    <Message Text="2:%(aa.Identity)" />
  </Target>

  <Target Name="ttB" Outputs="%(aa.Identity)">
    <Message Text="Batch it this way will work!" />
    <Message Text="1B:%(aa.Identity)" />
    <Message Text="2B:%(aa.Identity)" />
  </Target>
</Project>

For those who insist on calling a target in batching mode, then is the handy tool. See below sample script.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MAIN" >
  <!--<Import Project="$(MSBuildExtensionsPath)\MSBuildContrib\MSBuildContrib.Tasks"/>-->
  <!--<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>-->
  <!--<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />-->

  <Target Name="MAIN">
    <CallTarget Targets="t" />
    <CallTarget Targets="tt" />
    <CallTarget Targets="ttB" />
  </Target>

  <Target Name="t">
    <ItemGroup>
      <aa Include="1;22;333" />
    </ItemGroup>
  </Target>

  <Target Name="tt" Outputs="%(aa.Identity)">
    <Message Text="This batching won't work!" />
    <CallTarget Targets="ttt" />
    <CallTarget Targets="tttt" />
  </Target>

  <Target Name="ttt">
    <Message Text="1:%(aa.Identity)" />
  </Target>

  <Target Name="tttt">
    <Message Text="2:%(aa.Identity)" />
  </Target>

  <Target Name="ttB" Outputs="%(aa.Identity)">
    <Message Text="Batch it this way will work!" />
    <Message Text="1B:%(aa.Identity)" />
    <Message Text="2B:%(aa.Identity)" />

    <Message Text="Still need to call another target? Belowings are the work-around." />
    <PropertyGroup>
      <aaList>@(aa)</aaList>
    </PropertyGroup>
    <MSBuild Projects="$(MSBuildProjectFile)"
             Targets="tttB"
             Properties="aaList=$(aaList)"
             />
  </Target>
  <Target Name="tttB">
    <CallTarget Targets="tttB1" />
    <CallTarget Targets="tttB2" />
  </Target>
  <Target Name="tttB1">
    <ItemGroup>
      <aa Include="$(aaList.Split(';'))" />
    </ItemGroup>
  </Target>
  <Target Name="tttB2" Outputs="%(aa.Identity)">
    <Message Text="3B:%(aa.Identity)" />
  </Target>

</Project>

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: