@@ -294,23 +294,37 @@ if ! (cd "${BUILD_DIR}" && ./gradlew ${GRADLE_TASK}) 2>&1 | tee "${gradle_output
294294 exit 1
295295fi
296296
297- output::step " Stopping Gradle daemon"
298- if [[ -f " ${BUILD_DIR} /gradlew" ]]; then
299- (cd " ${BUILD_DIR} " && ./gradlew -q --stop) | output::indent
300- else
301- output::warning << -EOF
302- Warning: Gradle wrapper (gradlew) not found during daemon cleanup.
297+ # We use an EXIT trap for cleanup to ensure it runs after all tasks are complete, even when sourced.
298+ # When bin/compile runs standalone, cleanup executes immediately since the script ends here.
299+ # When the script is sourced by bin/test-compile, cleanup is deferred until test-compile finishes.
300+
301+ exit_cleanup () {
302+ # Only run cleanup when the script exits successfully (exit code 0).
303+ # The $? variable contains the exit status of the script when the EXIT trap fires.
304+ if [[ " ${?} " -ne 0 ]]; then
305+ return
306+ fi
303307
304- The gradlew script was present at the beginning of the build but was removed
305- during the build process. This is unexpected behavior.
308+ if [[ -f " ${BUILD_DIR} /gradlew" ]]; then
309+ output::step " Stopping Gradle daemon"
310+ (cd " ${BUILD_DIR} " && ./gradlew -q --stop) | output::indent
311+ else
312+ output::warning << -EOF
313+ Warning: Gradle wrapper (gradlew) not found during daemon cleanup.
306314
307- While this did not fail your current build, we strongly recommend fixing the
308- issue to prevent unexpected behavior in future builds. Check your Gradle tasks or
309- build scripts to see if they're removing the Gradle wrapper from your project directory.
310- EOF
315+ The gradlew script was present at the beginning of the build but was removed
316+ during the build process. This is unexpected behavior.
311317
312- metrics::set_raw " gradle_daemon_cleanup_gradlew_missing" " true"
313- fi
318+ While this did not fail your current build, we strongly recommend fixing the
319+ issue to prevent unexpected behavior in future builds. Check your Gradle tasks or
320+ build scripts to see if they're removing the Gradle wrapper from your project directory.
321+ EOF
322+
323+ metrics::set_raw " gradle_daemon_cleanup_gradlew_missing" " true"
324+ fi
325+
326+ # https://github.com/heroku/heroku-buildpack-gradle/issues/49
327+ rm -rf " ${CACHE_DIR} /.gradle/nodejs"
328+ }
314329
315- # https://github.com/heroku/heroku-buildpack-gradle/issues/49
316- rm -rf " ${CACHE_DIR} /.gradle/nodejs"
330+ trap exit_cleanup EXIT
0 commit comments