如何分组转换输入?

时间:2013-07-23 17:08:47

标签: msbuild

我有一个需要生成的文件类型,然后使用2个单独的工具进行验证。我希望尽可能逐步进行,所以我想创建一个0文件标记,记住上次验证生成的文件的时间。我设法让这一代只在修改过的文件上重新运行,但不是验证。

例如,假设这是.targets:

<Project ToolsVersion="4.0" DefaultTargets="Validate" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <MyItem Include="itemA" />
    <MyItem Include="itemB" />
  </ItemGroup>

  <Target Name="Generate"
          Inputs="@(MyItem)"
          Outputs="@(MyItem->'%(Filename).gen')">
    <Copy SourceFiles="@(MyItem)" DestinationFiles="@(MyItem->'%(Filename).gen')" />
  </Target>

  <Target Name="Validate" DependsOnTargets="Generate"
          Inputs="@(MyItem->'%(Filename).gen')"
          Outputs="@(MyItem->'%(Filename).gen.validated')">
    <!-- Do validation stuff... -->
    <Touch AlwaysCreate="true" Files="@(MyItem->'%(Filename).gen.validated')" />
  </Target>
</Project>

我第一次运行它,它生成并验证两个项目 - 这很好:

> msbuild
Generate:
  Copying file from "itemA" to "itemA.gen".
  Copying file from "itemB" to "itemB.gen".
Validate:
  Creating "itemA.gen.validated" because "AlwaysCreate" was specified.
  Creating "itemB.gen.validated" because "AlwaysCreate" was specified.

再次运行它,什么都没做 - 这很好:

> MSBuild
Generate:
Skipping target "Generate" because all output files are up-to-date with respect to the input files.
Validate:
Skipping target "Validate" because all output files are up-to-date with respect to the input files.

现在只更改itemA:

> touch itemA
> MSBuild
Generate:
Building target "Generate" partially, because some output files are out of date with respect to their input files.
  Copying file from "itemA" to "itemA.gen".     <<<<< Good - MSBuild knows not to generate itemB.gen
Validate:
  Touching "itemA.gen.validated".
  Touching "itemB.gen.validated".    <<<< Bad - how can I tell MSBuild to skip validation of itemB

那么,我如何告诉MSBuild部分运行我的Validate目标呢?

1 个答案:

答案 0 :(得分:0)

解决方案:将转换后的输入保存在单独的ItemGroup中。像这样:

<Project ToolsVersion="4.0" DefaultTargets="Validate" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <MyItem Include="itemA" />
    <MyItem Include="itemB" />
  </ItemGroup>

  <Target Name="Generate"
          Inputs="@(MyItem)"
          Outputs="@(MyItem->'%(Filename).gen')">
    <Copy SourceFiles="@(MyItem)" DestinationFiles="@(MyItem->'%(Filename).gen')" />
    <ItemGroup>
      <Generated Include="@(MyItem->'%(Filename).gen')" />
    </ItemGroup>
  </Target>

  <Target Name="Validate" DependsOnTargets="Generate"
          Inputs="@(Generated)"
          Outputs="@(Generated->'%(Filename).gen.validated')" >
    <!-- Do validation stuff... -->
    <Touch AlwaysCreate="true" Files="@(Generated->'%(Filename).gen.validated')" />
  </Target>
</Project>

我仍然希望我能避免重复@(MyItem->'%(Filename).gen') 3次......