MSBuild:如何使用可在ResouceCompile任务中使用的XmlPeek进行定义?

时间:2014-03-09 03:27:19

标签: msbuild vcxproj

当我从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>

1 个答案:

答案 0 :(得分: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>