aboutsummaryrefslogtreecommitdiffstats
path: root/tools/package.sh
blob: 2569e96e4c56a817b5fdf9abd57be8111ad1d95b (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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
  for dll in lib/"$d"/*.dll; do
    [ -f "$dll" ] || continue # e.g. openjfx's native Prism/Glass libraries
    cp "$dll" "$DIST/lib/$d/"
  done
  echo "  lib/$d/ ($n jars)"
done

echo "=== Generating executable manifests ==="
# 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.
for e in $EXECUTABLES; do
  CPLINE=""
  for m in $(deps_of "$e"); do
    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
  write_manifest_attr "$TMP/$e-manifest.txt" "Class-Path" "$CPLINE"
  echo "  $e: $CPLINE"
done

echo "=== Packaging ==="
for m in $MODULES; do
  [ "$m" = "$WAR_MODULE" ] && continue # ships as a WAR, below
  case " $EXECUTABLES " in
  *" $m "*)
    "$JAR" --create --file "$DIST/$m.jar" --main-class "$(mainclass_of "$m")" \
      --manifest "$TMP/$m-manifest.txt" -C "build/classes/$m" .
    ;;
  *)
    "$JAR" --create --file "$DIST/$m.jar" -C "build/classes/$m" .
    ;;
  esac
  echo "  $DIST/$m.jar"
done

for e in $EXECUTABLES; do
  cat >"$DIST/run-${e#ui-}.bat" <<EOF
@echo off
cd /d "%~dp0"
java -Djava.library.path="%~dp0lib\openjfx" -jar "%~dp0$e.jar"
if errorlevel 1 pause
EOF
done

[ -f README.pdf ] && cp README.pdf "$DIST/"

echo "=== Exercise 3: WAR ==="
# The WAR carries everything the server needs; Tomcat only provides the servlet API.
WARDIR="$TMP/war"
rm -rf "$WARDIR" && mkdir -p "$WARDIR/WEB-INF/lib"
cp -r "build/classes/$WAR_MODULE" "$WARDIR/WEB-INF/classes"
for m in $(deps_of "$WAR_MODULE"); do
  cp "$DIST/$m.jar" "$WARDIR/WEB-INF/lib/"
done
for d in $WAR_LIB_DIRS; do
  for j in lib/"$d"/*.jar; do
    is_shippable_jar "$j" || continue
    cp "$j" "$WARDIR/WEB-INF/lib/"
  done
done
EX3="$DIST/ex3"
mkdir -p "$EX3"
"$JAR" --create --file "$EX3/$WAR_NAME.war" -C "$WARDIR" .
echo "  $EX3/$WAR_NAME.war"
"$JAR" --list --file "$EX3/$WAR_NAME.war" | grep 'WEB-INF/lib/.' | sed 's/^/    /'

echo "=== Exercise 3: client directory ==="
CDIR="$EX3/client"
mkdir -p "$CDIR/lib"
cp "$DIST/$CLIENT.jar" "$CDIR/"
for m in $(deps_of "$CLIENT"); do
  cp "$DIST/$m.jar" "$CDIR/"
done
for d in $CLIENT_LIB_DIRS; do
  cp -r "$DIST/lib/$d" "$CDIR/lib/"
done
cp "$DIST/run-${CLIENT#ui-}.bat" "$CDIR/"
ls "$CDIR" | sed 's/^/    /'

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.
for e in $EXECUTABLES; do
  rm -rf "$TMP/mf" && mkdir -p "$TMP/mf"
  (cd "$TMP/mf" && "$JAR" -xf "$OLDPWD/$DIST/$e.jar" META-INF/MANIFEST.MF)
  echo "-- $e.jar --"
  sed 's/^/  /' "$TMP/mf/META-INF/MANIFEST.MF"

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

echo "======================================"
echo "Self-contained output in $DIST"
for e in $EXECUTABLES; do
  echo "  cd $DIST && java -jar $e.jar"
done
echo "======================================"