Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify MSBuild ItemGroup Metadata

Tags:

Is it possible to modify a ItemGroup's metadata after it is declared.

For Example:

  <ItemGroup>     <SolutionToBuild Include="$(BuildProjectFolderPath)\MySolution.sln">       <Targets></Targets>       <Properties></Properties>     </SolutionToBuild>    </ItemGroup>    <Target Name="BuildNumberOverrideTarget">      <!--Code to get the version number from a file (removed)-->       <!--Begin Pseudo Code-->      <CodeToChangeItemGroupMetaData             ItemToChange="%(SolutionToBuild.Properties)"             Condition ="'%(SolutionToBuild.Identity)' ==                        '$(BuildProjectFolderPath)\MySolution.sln'"            NewValue="Version=$(Version)" />      <!--End Pseudo Code-->             </Target> 

I am hoping there is a way that does not require me to remove the item then re-declare it.

Thanks for any answers. Vaccano

like image 555
Vaccano Avatar asked May 08 '09 21:05

Vaccano


People also ask

What is an ItemGroup in MSBuild?

In addition to the generic Item element, ItemGroup allows child elements that represent types of items, such as Reference , ProjectReference , Compile , and others as listed at Common MSBuild project items.

What is %( RecursiveDir?

%(Directory) Contains the directory of the item, without the root directory. For example: MyProject\Source\ %(RecursiveDir)

What is MSBuild target?

A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output. If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. This is called an incremental build of the target.

What is none include?

None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file. Compile - The file is compiled into the build output. This setting is used for code files.


2 Answers

Yes you can modify or add to an <ItemGroup>'s meta data after it is defined (MSBuild 3.5)

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    <!-- Define ItemGroup -->   <ItemGroup>     <TestItemGroup Include="filename.txt">       <MyMetaData>Test meta data</MyMetaData>     </TestItemGroup>     <TestItemGroup Include="filename2.txt">       <MyMetaData>Untouched</MyMetaData>     </TestItemGroup>   </ItemGroup>    <Target Name="ModifyTestItemGroup" BeforeTargets="Build">     <!-- Show me-->     <Message Text="PRE:  %(TestItemGroup.Identity)  MyMetaData:%(TestItemGroup.MyMetaData)  OtherMetaData:%(TestItemGroup.OtherMetaData)" Importance="high" />      <!-- Now change it - can only do it inside a target -->     <ItemGroup>       <TestItemGroup Condition="'%(TestItemGroup.MyMetaData)'=='Test meta data' AND 'AnotherCondition'=='AnotherCondition'">         <MyMetaData>Well adjusted</MyMetaData>         <OtherMetaData>New meta data</OtherMetaData>       </TestItemGroup>     </ItemGroup>      <!-- Show me the changes -->     <Message Text="POST: %(TestItemGroup.Identity)  MyMetaData:%(TestItemGroup.MyMetaData)  OtherMetaData:%(TestItemGroup.OtherMetaData)" Importance="high" />   </Target>    <Target Name="Build" /> </Project> 

Reference: MSDN Library: New Methods for Manipulating Items and Properties (MSBuild)

like image 141
Alex Avatar answered Sep 20 '22 14:09

Alex


There is a new to modify metadata by using the Update attribute E.g.

<ItemGroup>     <Compile Update="somefile.cs">  // or Update="*.designer.cs"         <MetadataKey>MetadataValue</MetadataKey>     </Compile> </ItemGroup>   

More in the MSBuild documentation

like image 28
honzajscz Avatar answered Sep 21 '22 14:09

honzajscz