如何递归执行MSBuild目标?

时间:2014-12-05 07:45:33

标签: msbuild

我尝试执行以下目标但最终出现circular dependency错误。我在目标$(Value) > 0中确实有一个停止条件Recursive

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <Target Name="Recursive" Condition="$(Value) > 0">
    <PropertyGroup>
      <Value>$([MSBuild]::Subtract($(Value), 1))</Value>
    </PropertyGroup>
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="Display" />
  </Target>

  <Target Name="Display">
    <Message Text="Value: $(Value)" />
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="Recursive" />
  </Target>

  <PropertyGroup>
    <Value>10</Value>
  </PropertyGroup>

  <Target Name="Build">
    <MSBuild Projects="$(MSBuildProjectFile)" Targets="Display" />
  </Target>
</Project>

1 个答案:

答案 0 :(得分:1)

您的停止条件没问题,但是您忘记将重新计算的Value属性传递给下一个递归。像这样更改您的Recursive目标:

<Target Name="Recursive" Condition="$(Value) > 0">
  <PropertyGroup>
    <Value>$([MSBuild]::Subtract($(Value), 1))</Value>
  </PropertyGroup>
  <MSBuild Projects="$(MSBuildProjectFile)" Targets="Display"
           Properties="Value=$(Value)" />
</Target>