【问题标题】:Gradle Task Not Running After Test测试后 Gradle 任务未运行
【发布时间】:2017-11-09 03:35:04
【问题描述】:

我在build.gradle 文件中设置了集成测试:

task integrationSetup(dependsOn: jar, type: Exec) {
    workingDir "$projectDir/resources/integration"
    commandLine 'sh', './start_service.sh'
}

task testIntegration(dependsOn: integrationSetup, type: Test) {
    testClassesDirs = sourceSets.testIntegration.output.classesDirs
    classpath = sourceSets.testIntegration.runtimeClasspath
    ignoreFailures = true
}

task integrationTearDown(dependsOn: testIntegration, type: Exec) {
    workingDir "$projectDir/resources/integration"
    commandLine 'sh', './stop_service.sh'
}

testIntegration.mustRunAfter integrationSetup
testIntegration.finalizedBy integrationTearDown
integrationTearDown.mustRunAfter testIntegration

但是,自从将 Gradle Wrapper 升级到版本 4+ 后,任务不再正确执行。最后的拆除永远不会运行,服务会继续。版本 3 和 4 之间发生了哪些变化来改变这种行为。 Gradle 在没有警告或弃用通知的情况下执行此操作。

一个愚蠢的选择是降级 Gradle 包装器版本(可以确认此设置仍然适用于 3.1)。但这不应该是海事组织的必要条件。

更新:对每个用户 @Opal 进行了一些更改。但是,如果在集成测试期间发生任何错误,最终的拆卸不会运行,仍然存在问题。

> Task :compileTestIntegrationJava
Putting task artifact state for task ':compileTestIntegrationJava' into context took 0.0 secs.
file or directory '/home/project/cleaner/src/testIntegration/java', not found
file or directory '/home/project/cleaner/src/testIntegration/java', not found
Executing task ':compileTestIntegrationJava' (up-to-date check took 0.072 secs) due to:
  Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration has changed.
  Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration/com has been removed.
  Output property 'destinationDir' file /home/project/cleaner/build/classes/java/testIntegration/com/project has been removed.
All input files are considered out-of-date for incremental task ':compileTestIntegrationJava'.
file or directory '/home/project/cleaner/src/testIntegration/java', not found
Compiling with JDK Java compiler API.
/home/project/cleaner/src/integration/java/com/project/cleaner/CleansRequestsTests.java:415: error: reached end of file while parsing
}
 ^
1 error

:compileTestIntegrationJava (Thread[Daemon worker Thread 8,5,main]) completed. Took 0.162 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestIntegrationJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED in 8s
8 actionable tasks: 8 executed
Stopped 0 worker daemon(s).

【问题讨论】:

  • 依赖项没有配置好,应该是:testIntegration.dependsOn integrationSetup testIntegration.finalizedBy integrationTearDown - 但是由于它不起作用,您能否使用-i 开关运行 gradle 并验证任务的输出?任务是否执行以及为什么要写。
  • 这也应该适用于 gradle > 4.0。刚刚用 gradle 4.3.1 测试了它。可能拆解任务不是在 testIntegration 之后直接执行,而是在图中稍后执行?正如@opal 建议的那样,请提供您构建的-m 运行的输出
  • Opal 是正确的,对于您的用例 dependsOn 设置和拆除 finalizedBy 应该足够了。
  • 我以这种方式进行设置的原因是,如果在集成测试期间遇到任何错误或问题,它在工作中最有效。如果出现故障,我仍然希望关闭该服务。我只是不明白为什么它适用于一个版本而不适用于最新版本。
  • @MarkII,您至少使用-i-m 运行它。请执行此操作并验证输出。

标签: java gradle build.gradle


【解决方案1】:

在讨论中,结果证明 OP 想要在运行测试之前停止启动的服务,例如,无论如何。编译错误。可以使用以下脚本完成:

ext.integrationTearDown = { 
  workingDir "$projectDir/resources/integration" 
  commandLine 'sh', './stop_service.sh' 
} 

task(type: Exec, 'stop_service', integrationTearDown) 

gradle.buildFinished { 
  exec integrationTearDown 
} 

testIntegration.dependsOn integrationSetup 
testIntegration.finalizedBy stop_service

使用这段代码,服务将在每次构建后停止 - 如果成功则发生事件。为了避免这种行为 BuildResult 被传递给 buildFinished 可以用来确定所需的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2015-07-04
    • 2017-12-24
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多