试试...最终等效于MsBuild [英] try...finally equivalent in MsBuild

查看:69
本文介绍了试试...最终等效于MsBuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论测试"目标成功还是失败(例如

How can I run a certain cleanup task after my "Test" target runs, regardless of whether the Test target succeeded or failed (like the try...finally construct in C#/Java).

推荐答案

Target元素具有 OnError 属性,您可以将其设置为要在出错时执行的目标,但只有在目标执行时才执行错误,只能解决您一半的情况.

The Target element has an OnError attribute you could set to a target to execute on error, but as it only executes if the target is in error, it only solves half your scenario.

您是否考虑过将目标链接在一起以表示您要执行的测试步骤"?

Have you considered chaining together targets to represent the test 'steps' you'd like to execute?

<PropertyGroup>
    <TestSteps>TestInitialization;Test;TestCleanup</TestSteps>
</PropertyGroup>

在"TestInitialization"目标中,您可以执行任何测试初始化​​,在"Test"目标中执行测试,在"TestCleanup"目标中进行任何后期测试清理.

The 'TestInitialization' target is where you can perform any test initialization, the 'Test' target executes the test, the 'TestCleanup' target does any sort of post test clean up.

然后,使用设置为 True RunEachTargetSeparately 属性,使用 CallTarget 任务执行这些目标.这将执行 all 个目标,无论成功与否.

Then, execute these targets using the CallTarget task, using the RunEachTargetSeparately attribute set to True. This will execute all the targets, regardless of success or failure.

完整的示例如下:

<Project DefaultTargets = "TestRun"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

    <!-- Insert additional tests between TestInitialization and TestCleanup as necessary -->
    <PropertyGroup>
        <TestSteps>TestInitialization;Test;TestCleanup</TestSteps>
    </PropertyGroup>

   <Target Name = "TestRun">

      <CallTarget Targets="$(TestSteps)" RunEachTargetSeparately="True" />

   </Target>

    <Target Name = "TestInitialization">
        <Message Text="Executing Setup..."/>
    </Target>

    <Target Name = "Test">
        <Message Text="Executing Test..."/>

        <!-- this will fail (or should unless you meet the conditions below on your machine) -->
        <Copy 
          SourceFiles="test.xml"
          DestinationFolder="c:\output"/>
    </Target>

    <Target Name = "TestCleanup">
        <Message Text="Executing Cleanup..."/>
    </Target>

</Project>

这篇关于试试...最终等效于MsBuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆