1

当我从 XML 文件中读取值时,它是正确的,但是当我使用它(作为 rc.exe 的一个选项)时,它是未定义的。我该如何解决?

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

  <!-- Read custom build number from generated XML file -->
  <Target Name="ReadCustomVersion" BeforeTargets="ResourceCompile">
    <XmlPeek XmlInputPath="$(OutDir)\CustomVersionNumber.xml" Query="/CustomVersion/CustomBuildNumber/text()">
      <Output TaskParameter="Result" ItemName="CustomBuildNumber" />
    </XmlPeek>

    **<!-- Print out the custom build number -- it is correct here -->**
    <Message Text="CustomBuildNumber = @(CustomBuildNumber)">
  </Target>

  <!-- Add version resource -->
  <ItemGroup>
    **<!-- CustomBuildNumber will not be set here -->**
    <ResourceCompile Include="..\..\build\CommonVersionResource.rc">
      <AdditionalOptions>/DBUILD_NUMBER=$(CustomBuildNumber) %(AdditionalOptions)</AdditionalOptions>
    </ResourceCompile>
  </ItemGroup>
</Project>
4

1 回答 1

2

在 XmlPeek/Output 元素中使用 PropertyName 属性而不是 ItemName。为什么不直接在 ReadCusomVersion 目标中添加 AdditionalOptions?

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

<Target Name="SetCustomVersion" BeforeTargets="ResourceCompile">
  <XmlPeek XmlInputPath="$(OutDir)\CustomVersionNumber.xml" Query="/CustomVersion/CustomBuildNumber/text()">
      <Output TaskParameter="Result" PropertyName="CustomBuildNumber" />
  </XmlPeek>

  <ItemGroup>
    <ResourceCompile>
      <AdditionalOptions>/DBUILD_NUMBER=$(CustomBuildNumber) %(AdditionalOptions)</AdditionalOptions>
    </ResourceCompile>
  </ItemGroup>
</Target>

于 2014-03-09T22:01:28.960 回答