4

Is it possible using default MSBuild technology to access a listing within an item group as a property in msbuild? I know I can do this in a custom task in C#, but I am trying to use built-in capabilities if possible.

Example:

I have an item group:

<ItemGroup>
    <SolutionToBuild Include="$(SolutionRoot)\Solutions\ClassLib\ClassLib.sln">
      <Properties>
        AssemblySigningKey=MySigningKey;
        OutDir=$(BinariesRoot)\SomeLocation\;
        LibraryName=ClassLib;
        PlatformTarget=x86;
      </Properties>
    </SolutionToBuild>

    <SolutionToBuild Include="$(SolutionRoot)\Solutions\BLAH\BLAH.sln">
      <Properties>
        ProjectType=Web;
      </Properties>
    </SolutionToBuild>
</ItemGroup>

I would like to extract the value of AssemblySigningKey, if it exists, and place this value into an MSBuild variable.

I have tried a few methods and the closest example I could find is using a tranformation within a separate target, but even this looks to be a bit of a hack, even if I could get the Condition to work I would then have to parse out the value splitting on the =. Is there no standard method to access this metadata within the item group?

<Target Name="TransformProps"
        Inputs="%(SolutionToBuild.Identity)"
        Outputs="_Non_Existent_Item_To_Batch_">

    <PropertyGroup>
        <IncludeProps>%(SolutionToBuild.Properties)</IncludeProps>
    </PropertyGroup>

    <ItemGroup>
        <IncludeProps Include="$(IncludeProps)" />
        <Solution Include="@(SolutionToBuild)">
          <IncludeProps Condition="'True'=='True' ">@(IncludeProps ->'-PROP %(Identity)', ' ')</IncludeProps>

        </Solution>
    </ItemGroup>
</Target>

My main target would call into the tranform in the following manner:

<Target Name="Main"  DependsOnTargets="TransformProps">    
    <Message Text="Solution info:  %(Solution.Identity) %(Solution.IncludeProps)" />
</Target>
g t
  • 7,287
  • 7
  • 50
  • 85
Jay
  • 2,644
  • 1
  • 30
  • 55
  • The ItemGroup structure for the SolutionsToBuild element and its child "Properties" is existing and I cannot change that. Is this what you are referring to? Please elaborate more if you have a suggestion, I'm willing to change what I can. – Jay Mar 05 '13 at 17:50
  • I wanted to know exactly what you wrote - your ItemGroup structure is given and cannot be changed. I´ll give it a try now that I´m sure. – Marcos Brigante Mar 05 '13 at 20:17

1 Answers1

2

Items Metadata are declared and transformed using xml tags. It seems like you're using the MSBuild Task to build some solutions - the properties tag is a parameter specific to this task.

The conversion from comma separated list and items as you tried won´t help because, as you mentioned, you still have the equal sign as the link from the keys to the values. I think there´s no way of obtaining the signing key value without parsing. After all msbuild do not consider the list of properties as metadata, it is just a list of strings.

I did the script below to exemplify how msbuild declare and read metadata. It is not an option for you because your ItemGroup structure cannot be changed.

IMHO in this case you have no option but use a custom task and do the parsing. Use Inline Tasks if you´re building with msbuild 4.0.

<?xml version="1.0" encoding="UTF-8" ?>
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <SolutionToBuild Include="$(SolutionRoot)\Solutions\ClassLib\ClassLib.sln">
      <AssemblySigningKey>MySigningKey123</AssemblySigningKey>
      <Properties>
        AssemblySigningKey=MySigningKey456;
        OutDir=$(BinariesRoot)\SomeLocation\;
        LibraryName=ClassLib;
        PlatformTarget=x86;
      </Properties>
    </SolutionToBuild>
  </ItemGroup>

  <Target Name="TransformProps">
    <PropertyGroup>
      <MySigningKey>@(SolutionToBuild->'%(AssemblySigningKey)')</MySigningKey>
    </PropertyGroup>
  </Target>

  <Target Name="Main"  DependsOnTargets="TransformProps">
    <Message Text="My desired Property Value:  $(MySigningKey)" />
  </Target>
Marcos Brigante
  • 974
  • 9
  • 25
  • Marcos, thanks for the information. I had come to a similar conclusion and ended up using a custom build task to parse and alter the metadata. Ill post my solution shortly. – Jay Mar 11 '13 at 18:58