java - Gradle 任务在测试后未运行

标签 java gradle build.gradle

我在我的 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 上仍然有效)。但在 IMO 看来,这不是必需的。

更新:针对每个用户@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).

最佳答案

在讨论中,事实证明 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

使用这段代码,服务将在每个 build - 成功后停止。避免这种行为 BuildResult传递给 buildFinished 可用于确定所需的行为。

关于java - Gradle 任务在测试后未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193479/

相关文章:

java - 增强现实尺寸变换

java - 如何使用 Java8 流从嵌套对象组装 Map<Key, Value>?

gradle - 如何从 Gradle 订购单元测试执行?

Android Studio Project 在 SSD 上会很快吗?

android - 我无法在我的 Android 项目 Android Studio 3.1.3 中使用 volley 库

Android/Sdk/build-tools/22.0.1/aapt'' 以非零退出值 1 完成

java - 从项目文件夹中的文件加载图像

git - Gradle:阅读最新的xy-release GIT标签进行版本控制

gradle - jacocoRootReport仅显示多项目gradle构建的最后一个项目的覆盖范围

java - Dockers 上的多模块 Maven 项目