In MSBuild, why isn't Item Metadata, within a

2019-07-19 04:59发布

问题:

Below is a portion of a MSBuild file that I'm working on:

<ItemGroup>
  <Tests Include="$(SolutionDir)\**\bin\$(TestPlatform)\$(Configuration)\*.Tests.dll" />
</ItemGroup>

<PropertyGroup>
  <TestProperties>/testcontainer:%(Tests.FullPath)</TestProperties>
</PropertyGroup>

I want to have a property that holds a command line switch. However, when I try to use $(TestProperties) in an Exec Command string, %(Tests.FullPath) is never resolved to the absolute path of the Tests item. Instead, it's always processed literally, as "%(Tests.FullPath)".

Am I doing something wrong or is this a standard MSBuild behavior? If the latter, is there a way for me to workaround this?

Thanks

P.S. - I realize I probably don't need to access the FullPath property since my Include value is an absolute path. However, I'd still like to understand the issue, along with how to handle it.

回答1:

You have a syntax error. Item lists are referenced via the @ character and item meta data is referenced via %. Reference the MSBuild Special Character Reference for details. To access the well known item metadata, you need to apply a transform inside the Property itself.

<ItemGroup>
  <Tests Include="MyFile.txt" />
</ItemGroup>

<PropertyGroup>
  <TestProperties>/testcontainer:@(Tests->'%(FullPath)')</TestProperties>
</PropertyGroup>

You can find more help here



标签: msbuild