From 1f0b4c5848c9f250a6f94f259718b9df7d0f5592 Mon Sep 17 00:00:00 2001 From: The_miro Date: Fri, 14 Aug 2026 15:04:57 +0200 Subject: [PATCH] Stop build.sh cleaning away the jar it just built Arguments were appended to the default task, so build.sh clean build ran gradlew build clean build. Gradle folds that back into build and clean, in that order, and the run ended by deleting the jar - with the script then reporting the jar was missing, which was at least honest. The README had claimed the invocation worked; nobody had run it. A named task now replaces the default instead of being appended to it. Flags are told apart from tasks by their leading dash, so --info still gets a task to attach to rather than running gradlew with nothing to do. The closing jar report is now conditional on a task that produces one having run, since build.sh clean properly ends with no jar and should not call that a failure. Co-Authored-By: Claude Opus 5 --- README.md | 6 ++++-- build.sh | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 462df1f..64fedd1 100644 --- a/README.md +++ b/README.md @@ -251,8 +251,10 @@ distro's default java may well be by now, and the failure when it is says `Could not create task ':test'` and `Type T not present` without ever mentioning Java. `build.sh` exists to pick a JDK between 17 and 23 - honouring `JAVA_HOME` if it already points at one - and to say what is wrong in as many words when -there is none to pick. Extra arguments are passed straight through, so -`./build.sh clean build` and `./build.sh --info` work. +there is none to pick. Arguments are passed through, with any task named +replacing the default `build` rather than adding to it, so `./build.sh clean +build` cleans and then builds; a bare flag such as `./build.sh --info` still +gets the default task. None of this touches what the mod is compiled against: the toolchain in `build.gradle` is Java 21 either way, and Gradle provisions it if it is missing. diff --git a/build.sh b/build.sh index 386b333..703f984 100755 --- a/build.sh +++ b/build.sh @@ -39,18 +39,38 @@ if [ -z "${JAVA_HOME:-}" ] || ! usable "$JAVA_HOME"; then exit 1 fi +# Named tasks replace the default rather than being appended to it. Appending +# would turn `build.sh clean build` into `gradlew build clean build`, which +# Gradle folds back into build and clean, in that order - it cleans away the jar +# it just built. Bare flags still get the default task, so `build.sh --info` +# means an informative build rather than a no-op. +tasks=() +for arg in "$@"; do + [ "${arg#-}" = "$arg" ] && tasks+=("$arg") +done +[ ${#tasks[@]} -eq 0 ] && set -- build "$@" + echo "build.sh: using JDK $(java_major "$JAVA_HOME") at $JAVA_HOME" -./gradlew build "$@" +./gradlew "$@" # Name the jar this run produced. Old versions linger in build/libs/, so listing -# the whole directory would be ambiguous about which one is current. -prop() { sed -n "s/^$1=//p" gradle.properties; } -jar="build/libs/$(prop mod_id)-$(prop mod_version).jar" +# the whole directory would be ambiguous about which one is current. Only worth +# saying when a task that builds one actually ran: `build.sh clean` legitimately +# ends with no jar at all. +builds_jar=false +for arg in "$@"; do + case "$arg" in build | assemble | jar) builds_jar=true ;; esac +done -echo -if [ -f "$jar" ]; then - echo "Built: $jar" -else - echo "build.sh: expected $jar, but it is not there." >&2 - exit 1 +if [ "$builds_jar" = true ]; then + prop() { sed -n "s/^$1=//p" gradle.properties; } + jar="build/libs/$(prop mod_id)-$(prop mod_version).jar" + + echo + if [ -f "$jar" ]; then + echo "Built: $jar" + else + echo "build.sh: expected $jar, but it is not there." >&2 + exit 1 + fi fi