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 <noreply@anthropic.com>
gem-slots-and-the-fangs-element
Amir Alexander Abdelbaki 2026-08-14 15:04:57 +02:00
parent 9239f03fe0
commit 1f0b4c5848
2 changed files with 34 additions and 12 deletions

View File

@ -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 `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` 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 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 there is none to pick. Arguments are passed through, with any task named
`./build.sh clean build` and `./build.sh --info` work. 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 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. `build.gradle` is Java 21 either way, and Gradle provisions it if it is missing.

View File

@ -39,11 +39,30 @@ if [ -z "${JAVA_HOME:-}" ] || ! usable "$JAVA_HOME"; then
exit 1 exit 1
fi 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" 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 # 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. # 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
if [ "$builds_jar" = true ]; then
prop() { sed -n "s/^$1=//p" gradle.properties; } prop() { sed -n "s/^$1=//p" gradle.properties; }
jar="build/libs/$(prop mod_id)-$(prop mod_version).jar" jar="build/libs/$(prop mod_id)-$(prop mod_version).jar"
@ -54,3 +73,4 @@ else
echo "build.sh: expected $jar, but it is not there." >&2 echo "build.sh: expected $jar, but it is not there." >&2
exit 1 exit 1
fi fi
fi