create-mekanism-modular/build.sh

77 lines
2.6 KiB
Bash
Executable File

#!/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
# 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 "$@"
# 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. 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; }
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