简体   繁体   English

如果gradle项目中的测试任务失败,请运行stop tomcat任务

[英]run stop tomcat task if test task fails in gradle project

I am working on a gradle project. 我正在做一个gradle项目。 I have written few tests for it. 我为此写了一些测试。 In build.gradle, I have written three tasks for it: tomcatStart, tomcatEnd and test. 在build.gradle中,我为此编写了三个任务:tomcatStart,tomcatEnd和test。 Inside test task, i am executing tomcatStart in doFirst and tomcatStop in doLast. 在测试任务中,我正在doFirst中执行tomcatStart,在doLast中执行tomcatStop。 It all works fine if there is no test failure. 如果没有测试失败,则一切正常。 But if a test fails, the tomcat keeps on running. 但是,如果测试失败,则tomcat会继续运行。

I want to know if there is any way, i can stop tomcat even if test task fails. 我想知道是否有办法,即使测试任务失败,我也可以停止tomcat。

Try to use finalizebBy property of the test task. 尝试使用测试任务的finalizebBy属性。 It's well described in the official documentation . 官方文档中对此进行了很好的描述。 According to it: 根据它:

Finalizer tasks are useful in situations where the build creates a resource that has to be cleaned up regardless of the build failing or succeeding. 在构建创建无论构建失败还是成功都必须清除的资源的情况下,终结器任务很有用。 An example of such a resource is a web container that is started before an integration test task and which should be always shut down, even if some of the tests fail. 这种资源的一个示例是一个Web容器,它在集成测试任务之前启动,并且即使某些测试失败,也应始终将其关闭。

And here is an example from the docs: 这是来自文档的示例:

task taskX {
    doLast {
        println 'taskX'
        throw new RuntimeException()
    }
}
task taskY {
    doLast {
        println 'taskY'
    }
}

taskX.finalizedBy taskY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM