4

我需要更新项目元数据值。很容易增加价值:

<ItemDefinitionGroup>
  <ClCompile>
    <PreprocessorDefinitions>FOO;BAR;%(PreprocessorDefinitions)</PreprocessorDefinitions>
  </ClCompile>
</ItemDefinitionGroup>

但是,我需要做的是删除部分价值。理想情况下,这样的事情会起作用,但它不会:

<ItemDefinitionGroup>
  <ClCompile>
    <PreprocessorDefinitions>%(PreprocessorDefinitions.Replace('FOO;',''))</PreprocessorDefinitions>
  </ClCompile>
</ItemDefinitionGroup>

有没有办法在 MSBuild 4 中实现这一点?

4

2 回答 2

2

我试图做同样的事情,虽然我不知道如何从字符串中删除定义,但我确实发现了一个额外的属性:UndefinePreprocessorDefinitions.

<ItemDefinitionGroup>
  <ClCompile>
    <UndefinePreprocessorDefinitions>FOO</UndefinePreprocessorDefinitions>
  </ClCompile>
</ItemDefinitionGroup>

这将取消之前对 FOO 的定义。传递给编译器而不是什么都不传递可能看起来有点傻-DFOO -UFOO,但它也同样有效。

于 2012-08-14T18:39:42.273 回答
1

在随后的ItemDefinitionGroup中,您可以创建当前元数据的副本,然后对其调用Replace

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Dump" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemDefinitionGroup>
    <SomeItem>
      <SomeMetaData>foo,bar,baz</SomeMetaData>
    </SomeItem>
  </ItemDefinitionGroup>

  <ItemGroup>
    <SomeItem Include="one;two" />
  </ItemGroup>

  <ItemDefinitionGroup>
    <SomeItem>
      <!-- Remove "bar" -->
      <SomeMetaData>$([System.String]::Copy('%(SomeMetaData)').Replace('bar',''))</SomeMetaData>
    </SomeItem>
  </ItemDefinitionGroup>

  <Target Name="Dump">
    <Message Text="SomeItem.SomeMetaData: @(SomeItem -> '%(Identity)=%(SomeMetaData)') " />
  </Target>
</Project>

下面是使用 MSBuild 14 运行时的输出:

 > MSBuild .\foo.proj
Microsoft (R) Build Engine version 14.0.25420.1
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 2/17/2017 7:09:48 PM.
Project "D:\temp\mb\foo.proj" on node 1 (default targets).
Dump:
  SomeItem.SomeMetaData: one=foo,,baz;two=foo,,baz
Done Building Project "D:\temp\mb\foo.proj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.03
于 2017-02-18T03:12:14.810 回答