如何在 MSBUILD 中将 ItemGroup 转换为 Propertygroup

How to convert ItemGroup into Propertygroup in MSBUILD

我发现了类似的问题 here.

但这并不能解决我的问题。我有一个像这样的 ItemGroup

<ItemGroup>
    <DocumentationSource Include="TestLibrary\TestLibrary.csproj;TestLibrary2\TestLibrary2.csproj;TestLibrary2\TestLibrary3.csproj" />
</ItemGroup>

我需要将其更改为这种格式的属性组

<PropertyGroup>
    <DocumentationSources>
      <DocumentationSource sourceFile="TestLibrary\TestLibrary.csproj" />
      <DocumentationSource sourceFile="TestLibrary2\TestLibrary2.csproj" />
      <DocumentationSource sourceFile="TestLibrary2\TestLibrary3.csproj" />
    </DocumentationSources>
</PropertyGroup>

我正在使用 sandcastle 文档生成器来生成文档。这需要我以 PropertyGroup 格式显示的文档源。但是在我的构建脚本中,我已经有了一个 ItemGroup,其中包含上述格式中提到的所有项目。

如何使用这里的ItemGroup作为SandCastle的文档源或者如何将ItemGroup转换为上述格式的PropertyGroup?

实际上我可以将 ItemGroup 更改为 PropertyGroup 格式,但它是使用类似这样的逻辑动态形成的

 <_ProjectFilesPlatform Include="%(ProjectDefinitionsPlatform.Identity)">
        <_ProjectPath>$([System.String]::Copy(%(ProjectDefinitionsPlatform.Identity)).Replace(".","\"))</_ProjectPath>
      </_ProjectFilesPlatform>

[这是我在这里给出的粗略轮廓。这种操作不是实际使用的]

我是这个 MSBUILD 脚本的新手。任何人都可以对此有所了解吗?

谢谢。

您可以使用 @() 语法将项目转换为字符串,以换行符分隔。这是一个示例项目文件(在 .net 核心上使用 MSBuild 15 测试):

<Project>
  <ItemGroup>
    <DocumentationSource Include="TestLibrary\TestLibrary.csproj;TestLibrary2\TestLibrary2.csproj;TestLibrary2\TestLibrary3.csproj" />
  </ItemGroup>

  <PropertyGroup>
    <DocumentationSources>
      @(DocumentationSource->'&lt;DocumentationSource sourceFile="%(Identity)" /&gt;', '
      ')
    </DocumentationSources>
  </PropertyGroup>

  <Target Name="Build">
    <Message Importance="high" Text="Value of DocumentationSources: $(DocumentationSources)" />
  </Target>
</Project>

产生以下输出:

$ dotnet msbuild
Microsoft (R) Build Engine version 15.3.378.6360 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Value of DocumentationSources: 
        <DocumentationSource sourceFile="TestLibrary/TestLibrary.csproj" />
        <DocumentationSource sourceFile="TestLibrary2/TestLibrary2.csproj" />
        <DocumentationSource sourceFile="TestLibrary2/TestLibrary3.csproj" />

这甚至允许您为项目元素使用通配符:

<DocumentationSource Include="**\*.csproj" />