summaryrefslogtreecommitdiffstats
path: root/tools/package.sh
blob: 5877444f70b75b3944a11919c7aed92a1c950d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash

set -e
cd "$(dirname "$0")/.."
. tools/_util.sh

bash tools/build.sh

DIST=build/dist
rm -rf "$DIST"
mkdir -p "$DIST"

echo "=== Staging runtime libs beside the jars ==="
for d in $RUNTIME_LIB_DIRS; do
  mkdir -p "$DIST/lib/$d"
  n=0
  for j in lib/"$d"/*.jar; do
    is_shippable_jar "$j" || continue # never ship -sources / -javadoc
    cp "$j" "$DIST/lib/$d/"
    n=$((n + 1))
  done
  echo "  lib/$d/ ($n jars)"
done

echo "=== Generating console manifest ==="
# Generated from what is actually staged, never hand-maintained. Four silent
# failure modes: wildcards match nothing; a missing trailing newline makes jar
# drop the line; paths are relative to this jar; and java -jar ignores -cp, so
# this line is the entire classpath at runtime.
MF="$TMP/console-manifest.txt"
CPLINE=""
for m in $MODULES; do
  [ "$m" = ui-console ] && continue
  CPLINE="$CPLINE${CPLINE:+ }$m.jar"
done
for d in $RUNTIME_LIB_DIRS; do
  for j in "$DIST"/lib/"$d"/*.jar; do
    [ -f "$j" ] || continue
    CPLINE="$CPLINE lib/$d/$(basename "$j")"
  done
done
printf 'Class-Path: %s\n\n' "$CPLINE" >"$MF"
echo "  $CPLINE"

echo "=== Packaging ==="
for m in $MODULES; do
  if [ "$m" = ui-console ]; then
    "$JAR" --create --file "$DIST/$m.jar" \
      --main-class "$MAIN_CLASS" --manifest "$MF" -C "build/classes/$m" .
  else
    "$JAR" --create --file "$DIST/$m.jar" -C "build/classes/$m" .
  fi
  echo "  $DIST/$m.jar"
done

cat >"$DIST/run.bat" <<'EOF'
@echo off
cd /d "%~dp0"
java -jar "%~dp0ui-console.jar"
if errorlevel 1 pause
EOF

echo "=== Manifest as it actually landed in the jar ==="
# Cheapest check that prevents a Level-0: if Main-Class or Class-Path didn't
# survive, it surfaces here instead of on the grader's machine.
rm -rf "$TMP/mf" && mkdir -p "$TMP/mf"
(cd "$TMP/mf" && "$JAR" -xf "$OLDPWD/$DIST/ui-console.jar" META-INF/MANIFEST.MF)
sed 's/^/  /' "$TMP/mf/META-INF/MANIFEST.MF"

grep -q '^Main-Class:' "$TMP/mf/META-INF/MANIFEST.MF" || {
  echo "*** Main-Class missing ***"
  exit 1
}
grep -q 'Class-Path:' "$TMP/mf/META-INF/MANIFEST.MF" || {
  echo "*** Class-Path missing ***"
  exit 1
}

echo "======================================"
echo "Self-contained output in $DIST"
echo "  cd $DIST && java -jar ui-console.jar"
echo "======================================"