msbuildtarget

Make ItemGroup created in MSBuild target available to calling targets


If I have the following targets in an MSBuild file:

<Target Name="Temp">
   <CallTarget Targets="CreateTestList" />
   <Message Text="TestList: -- @(TestAssembly) -- " />
   <Message Text="Testing &quot;%(TestAssembly.Identity)&quot;" />
</Target>   

<Target Name="CreateTestList">
     <CreateItem Include="**\bin\$(Configuration)\*Tests.dll">
          <Output TaskParameter="Include" ItemName="TestAssembly" />
     </CreateItem>
     <Message Text="TestList: -- @(TestAssembly) -- " />
     <Message Text="Testing &quot;%(TestAssembly.Identity)&quot;" />
</Target>

How do I make the Message statements in my Temp target print out the items that the CreateTestList target put into the @(TestAssembly) ItemGroup?


Solution

  • Two things to note. First, the CreateItem task is essentially obsolete. Make it more readable by just declaring an ItemGroup inside your target. Second, due to how MSBuild publishes items, you need to make the CreateTestList target run as a dependency, not with CallTarget, which in most cases has limited usefulness. So,

    <Target Name="Temp" DependsOnTargets="CreateTestList">
       <Message
          ...
    </Target>
    
    <Target Name="CreateTestList">     
       <ItemGroup>
          <TestAssembly Include="**\bin\$(Configuration)\*Tests.dll">
       </ItemGroup>
       <Message
          ...
    </Target>