Skip to content

Commit 910693b

Browse files
authored
Improve test-compile resilience (#175)
1 parent 3b8812c commit 910693b

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
66

77
## [Unreleased]
88

9+
- Improve test-compile resilience and performance. ([#175](https://github.com/heroku/heroku-buildpack-gradle/pull/175))
910

1011
## [v46] - 2025-09-15
1112

bin/compile

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,37 @@ if ! (cd "${BUILD_DIR}" && ./gradlew ${GRADLE_TASK}) 2>&1 | tee "${gradle_output
294294
exit 1
295295
fi
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

bin/test-compile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ export GRADLE_TASK="testClasses"
88

99
source "${BUILDPACK_DIR}/bin/compile"
1010

11-
# Copy the contents of the Gradle user home to the build directory so that configuration
12-
# and build caches are available as runtime for tests.
13-
util::cache_copy ".gradle" "${CACHE_DIR}" "${BUILD_DIR}"
14-
1511
# We need to resolve the test runtime dependencies for the tests to run, but Gradle doesn't have a built-in way to do this.
1612
# Instead, we use a custom init script to resolve the dependencies.
1713
# https://docs.gradle.org/current/userguide/init_scripts.html
@@ -43,8 +39,9 @@ EOF
4339
output::step "Resolving test runtime dependencies"
4440
(cd "${BUILD_DIR}" && ./gradlew --init-script "${heroku_gradle_init_script}" resolveTestRuntime) 2>&1 | output::indent
4541

46-
# Write the Gradle user home back into the cache to make sure the test dependencies get permanently cached too.
47-
util::cache_copy ".gradle" "${BUILD_DIR}" "${CACHE_DIR}"
42+
# Copy the contents of the Gradle user home to the build directory so that configuration
43+
# and build caches are available at runtime for tests.
44+
util::cache_copy ".gradle" "${CACHE_DIR}" "${BUILD_DIR}"
4845

4946
mkdir -p "${BUILD_DIR}/.profile.d"
5047
cat <<-EOF >"${BUILD_DIR}/.profile.d/gradle.sh"

test/spec/ci_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
2121
BUILD SUCCESSFUL in \\d+s
2222
3 actionable tasks: 3 executed
23-
-----> Stopping Gradle daemon
2423
-----> Resolving test runtime dependencies
25-
Starting a Gradle Daemon, \\d+ stopped Daemon could not be reused, use --status for details
2624
> Task :resolveTestRuntime
2725
2826
BUILD SUCCESSFUL in \\d+s
2927
1 actionable task: 1 executed
28+
-----> Stopping Gradle daemon
3029
-----> No test-setup command provided\\. Skipping\\.
3130
-----> Running Gradle buildpack tests...
3231
Picked up JAVA_TOOL_OPTIONS: -Dfile\\.encoding=UTF-8 -XX:MaxRAM=2684354560 -XX:MaxRAMPercentage=80\\.0
@@ -66,13 +65,12 @@
6665
6766
BUILD SUCCESSFUL in \\d+s
6867
3 actionable tasks: 1 executed, 2 from cache
69-
-----> Stopping Gradle daemon
7068
-----> Resolving test runtime dependencies
71-
Starting a Gradle Daemon, \\d+ stopped Daemon could not be reused, use --status for details
7269
> Task :resolveTestRuntime
7370
7471
BUILD SUCCESSFUL in \\d+s
7572
1 actionable task: 1 executed
73+
-----> Stopping Gradle daemon
7674
-----> No test-setup command provided\\. Skipping\\.
7775
-----> Running Gradle buildpack tests...
7876
Picked up JAVA_TOOL_OPTIONS: -Dfile\\.encoding=UTF-8 -XX:MaxRAM=2684354560 -XX:MaxRAMPercentage=80\\.0

0 commit comments

Comments
 (0)