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