#!/usr/bin/env bash # Builds the mod jar into build/libs/. # # Gradle 8.12 cannot run on a JDK newer than 23, and a distro's default java is # often newer than that, so this picks a JDK Gradle can run on rather than # leaving you to read the "Type T not present" failure that comes of not doing # so. The build itself still compiles against Java 21 either way - that is the # toolchain in build.gradle, and Gradle provisions it if it is missing. set -euo pipefail cd "$(dirname "$0")" # Major version of the java at $1/bin/java, or nothing if it will not run. java_major() { local out out=$("$1/bin/java" -version 2>&1) || return 0 sed -n 's/^[^"]*"\([0-9]*\).*/\1/p' <<<"$out" | head -1 } usable() { local major [ -x "$1/bin/java" ] || return 1 major=$(java_major "$1") [ -n "$major" ] && [ "$major" -ge 17 ] && [ "$major" -le 23 ] } if [ -z "${JAVA_HOME:-}" ] || ! usable "$JAVA_HOME"; then for candidate in /usr/lib/jvm/java-{21,17}-openjdk /usr/lib/jvm/java-{21,17}-openjdk-amd64; do if usable "$candidate"; then export JAVA_HOME="$candidate" break fi done fi if [ -z "${JAVA_HOME:-}" ] || ! usable "$JAVA_HOME"; then echo "build.sh: no JDK between 17 and 23 found - Gradle 8.12 cannot run on anything newer." >&2 echo "Install one (e.g. pacman -S jdk17-openjdk) or point JAVA_HOME at it." >&2 exit 1 fi echo "build.sh: using JDK $(java_major "$JAVA_HOME") at $JAVA_HOME" ./gradlew build "$@" # 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" echo if [ -f "$jar" ]; then echo "Built: $jar" else echo "build.sh: expected $jar, but it is not there." >&2 exit 1 fi