【问题标题】:How to access ItemGroup Metadata as properties within MSBuild script如何访问 ItemGroup 元数据作为 MSBuild 脚本中的属性
【发布时间】:2013-03-05 15:07:50
【问题描述】:

是否可以使用默认的 MSBuild 技术来访问项目组中的列表作为 msbuild 中的属性?我知道我可以在 C# 中的自定义任务中执行此操作,但如果可能,我会尝试使用内置功能。

例子:

我有一个项目组:

<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>

我想提取AssemblySigningKey 的值(如果存在),并将该值放入 M​​SBuild 变量中。

我尝试了一些方法,我能找到的最接近的例子是在单独的目标中使用转换,但即使这看起来有点像 hack,即使我可以让 Condition 工作我也会然后必须解析出= 上的值拆分。是否没有标准方法可以在项目组中访问此元数据?

<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>

我的主要目标将通过以下方式调用转换:

<Target Name="Main"  DependsOnTargets="TransformProps">    
    <Message Text="Solution info:  %(Solution.Identity) %(Solution.IncludeProps)" />
</Target>

【问题讨论】:

  • SolutionsToBuild 元素及其子“属性”的 ItemGroup 结构已存在,我无法更改。这是你指的吗?如果您有建议,请详细说明,我愿意改变我能改变的。
  • 我想确切地知道你写了什么——你的 ItemGroup 结构是给定的,不能改变。我现在肯定会试一试。

标签: msbuild msbuild-4.0


【解决方案1】:

Items Metadata 使用 xml 标签声明和转换。您似乎正在使用 MSBuild Task 构建一些解决方案 - properties 标记是此任务特定的参数。

您尝试从逗号分隔的列表和项目进行转换将无济于事,因为正如您所提到的,您仍然拥有等号作为从键到值的链接。我认为没有解析就无法获得签名密钥值。毕竟 msbuild 不将属性列表视为元数据,它只是一个字符串列表。

我编写了下面的脚本来举例说明 msbuild 如何声明和读取元数据。这不是您的选择,因为您的 ItemGroup 结构无法更改。

恕我直言,在这种情况下,您别无选择,只能使用自定义任务并进行解析。如果您使用 msbuild 4.0 构建,请使用 Inline Tasks

<?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>

【讨论】:

  • 马科斯,感谢您提供的信息。我得出了类似的结论,并最终使用自定义构建任务来解析和更改元数据。我很快就会发布我的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
相关资源
最近更新 更多