Add MSBuild output for batch task as object metadata

I have a list of projects in my MSBuild file:

<ItemGroup> <SubProject Include="**\*.csproj" /> </ItemGroup> 

And now I would like to set my TargetPath for each project in the metadata property for each project.

I already know how to extract the target path for each project and put it in a separate list of elements:

 <Target Name="ExtractTargetPaths"> <MSBuild Projects="%(SubProject.Identity)" Targets="GetTargetPath"> <Output TaskParameter="TargetOutputs" ItemName="SubProjectTargetPath" /> </MSBuild> </Target> 

However, I would like to have access to this "SubProjectTargetPath" as metadata in the SubProject elements instead of a separate list of elements.

That is, instead of writing, for example. this is:

 <SomeTask Parameter="%(SubProjectTargetPath.Identity)" /> 

I could write something like:

 <SomeTask Parameter="%(SubProject.TargetPath)" /> 
+6
source share
1 answer

OK, I found one solution that should use targeted refinement with a temporary property:

 <ItemGroup> <SubProject Include="**\*.csproj" /> </ItemGroup> <Target Name="UpdateSubProjectMetadata" Outputs="%(SubProject.Identity)"> <!-- Retrieves the Target DLL path and puts it in the temporary property "_TempTargetPath" --> <MSBuild Projects="%(SubProject.Identity)" Targets="GetTargetPath"> <Output TaskParameter="TargetOutputs" PropertyName="_TempTargetPath" /> </MSBuild> <!-- Set the metadata item for TestProject to the value of the temporary property --> <ItemGroup> <SubProject Condition="'%(SubProject.Identity)' == '%(Identity)'" > <TargetPath>$(_TempTargetPath)</TargetPath> </SubProject> </ItemGroup> <!-- Clear the temporary property --> <PropertyGroup> <_TempTargetPath></_TempTargetPath> </PropertyGroup> </Target> 

After launching this target, TargetPath is available for each metadata element.

Implementation note : the above code is tested only for MSBuild 4.0 - I think that it works the same as on MSBuild 3.5, and users of previous versions will use the tasks <CreateItem> and <CreateProperty> placing <PropertyGroup> and <ItemGroup> .

+5
source

Source: https://habr.com/ru/post/919666/


All Articles