2

如何将一个项目扁平化为一个属性?

根据这个MSDN page (section Conversions between Strings and Item Lists),只需声明一个以项目为值的属性即可。

但我发现实际做法完全相反。当我使用 MSBuild 4.0 运行此项目时,指定目标install...

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(Project-to-import)" Condition="'$(Project-to-import)' != ''" />

  <ItemGroup>
    <RuntimeProj    Include="C:\TEMP\Design.proj" />
    <DesigntimeProj Include="C:\TEMP\Run.dproj" />
  </ItemGroup>

  <PropertyGroup> <!-- Flatten out the items to properties. -->
    <RuntimeProj>@(RuntimeProj)</RuntimeProj>
     <!-- ^ This bit doesn't work. MS documentation is wrong? -->
    <DesigntimeProj>@(RuntimeProj)</DesigntimeProj>
  </PropertyGroup>

  <Target Name="install">
    <Message Text="Hello world!"/>
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="BuildRunPackage"   
             Properties="Project-to-import=$(RuntimeProj)" />
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="BuildDesignPackage"
             Properties="Project-to-import=$(DesigntimeProj)" />
  </Target>

  <Target Name="BuildRunPackage">
    <CallTarget Targets="Clean" /> 
    <CallTarget Targets="Build" /> 
  </Target>

  <Target Name="BuildDesignPackage">
    <CallTarget Targets="Clean" /> 
    <CallTarget Targets="Make" /> 
    <CallTarget Targets="Register" />
  </Target>
<.Project>

...返回一个错误...

错误 MSB4012:表达式“Project-to-import=@(RuntimeProj) 不能在此上下文中使用。项目列表不能与需要项目列表的其他字符串连接。使用分号分隔多个项目列表。

4

1 回答 1

4

我不确定,但我认为您会遇到评估顺序问题。据我所知,在项目之前评估属性(请参阅http://msdn.microsoft.com/en-us/library/dd997067.aspx中的属性和项目评估顺序)如果您可以尝试将属性组从“全局”移动在安装目标下。它应该工作。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(Project-to-import)" Condition="'$(Project-to-import)' != ''" />

  <ItemGroup>
    <RuntimeProj    Include="C:\TEMP\Design.proj" />
    <DesigntimeProj Include="C:\TEMP\Run.dproj" />
  </ItemGroup>


  <Target Name="install">
    <PropertyGroup> <!-- Flatten out the items to properties. -->
      <RuntimeProj>@(RuntimeProj)</RuntimeProj>
      <DesigntimeProj>@(RuntimeProj)</DesigntimeProj>
    </PropertyGroup>

    <Message Text="Hello world!"/>
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="BuildRunPackage"   
             Properties="Project-to-import=$(RuntimeProj)" />
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="BuildDesignPackage"
             Properties="Project-to-import=$(DesigntimeProj)" />
  </Target>

  <Target Name="BuildRunPackage">
    <CallTarget Targets="Clean" /> 
    <CallTarget Targets="Build" /> 
  </Target>

  <Target Name="BuildDesignPackage">
    <CallTarget Targets="Clean" /> 
    <CallTarget Targets="Make" /> 
    <CallTarget Targets="Register" />
  </Target>
</Project>
于 2013-07-02T19:21:59.217 回答