MarkII
MarkII

Reputation: 892

Gradle Task Not Running After Test

I have integration tests setup in my build.gradle file as such:

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

However since upgrading the Gradle Wrapper to version 4+ the tasks no longer execute correctly. The final tear down never runs and the service continues. What has changed between version 3 and 4 to change this behaviour. Pretty upsetting Gradle did this without warning or deprecation notices.

One dumb option is to downgrade the Gradle wrapper version (can confirm this setup still works on 3.1). But that shouldn't be necessary IMO.

UPDATE: Made some changes per user @Opal. However still have issue where if any errors occur during integration tests the final tear down does not run.

> 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).

Upvotes: 3

Views: 2705

Answers (1)

Opal
Opal

Reputation: 84756

In discussion it turned out that OP wants to stop the service started before running test no matter what e.g. compilation errors. It can be done with the following script:

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

With this piece of code the service will be stopped after every build - event if it succeeds. To avoid this behaviour BuildResult which is passed to buildFinished may be used to determine the required behaviour.

Upvotes: 1

Related Questions