如何在MSBuild中获取扩展名(不带点)

时间:2010-08-11 13:55:57

标签: msbuild metadata itemgroup

我有一个ItemGroup,我在MSBuild项目中使用其元数据作为标识符进行批处理。例如:

        <BuildStep
          TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
          BuildUri="$(BuildUri)"
          Name="RunUnitTestsStep-%(TestSuite.Filename)-%(TestSuite.Extension)"
          Message=" - Unit Tests: %(TestSuite.Filename): %(TestSuite.Extension)">

          <Output
            TaskParameter="Id"
            PropertyName="RunUnitTestsStepId-%(TestSuite.Filename)-%(TestSuite.Extension)" />
        </BuildStep>

但是,这不起作用,因为扩展中有一个点,这是Id的无效字符(在BuildStep任务中)。因此,MSBuild始终在BuildStep任务上失败。

我一直试图删除这个点,但没有运气。也许有办法在现有的ItemGroup中添加一些元数据?理想情况下,我希望有类似%(TestSuite.ExtensionWithoutDot)的东西。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

我认为你对<Output>元素在这里做了什么感到有些困惑 - 它将创建一个以PropertyName属性中的值命名的属性,并将设置该属性的属性是BuildStep任务中Id 输出的值。您对Id的值没有影响 - 您只需将其存储在属性中供以后参考,以便设置构建步骤的状态

考虑到这一点,我不明白为什么你担心创建的Property会有一个包含扩展串联的名称。只要属性名称是唯一的,您可以稍后在后续的BuildStep任务中引用它,并且我假设您的testsuite文件名足以表示唯一性。

实际上,如果您执行Target批处理,则可以避免创建跟踪每个testsuite / buildstep对的唯一属性:

<Target Name="Build"
        Inputs="@(TestSuite)"
        Outputs="%(Identity).Dummy">
    <!--
    Note that even though it looks like we have the entire TestSuite itemgroup here,
    We will only have ONE - ie we will execute this target *foreach* item in the group
    See http://beaucrawford.net/post/MSBuild-Batching.aspx
    -->


    <BuildStep
          TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
          BuildUri="$(BuildUri)"
          Name="RunUnitTestsStep-%(TestSuite.Filename)-%(TestSuite.Extension)"
          Message=" - Unit Tests: %(TestSuite.Filename): %(TestSuite.Extension)">

          <Output
            TaskParameter="Id"
            PropertyName="TestStepId" />
        </BuildStep>

    <!--
    ..Do some stuff here..
    -->

    <BuildStep Condition=" Evaluate Success Condition Here "
           TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
           BuildUri="$(BuildUri)"
           Id="$(TestStepId)"
           Status="Succeeded" />
    <BuildStep Condition=" Evaluate Failed Condition Here "
           TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
           BuildUri="$(BuildUri)"
           Id="$(TestStepId)"
           Status="Failed" />
</Target>