6 echo "Usage: $CMD [-v N] /path/to/jarfile" >&2
7 echo " -v N build jar with alternative classes for version N of java (optional, defaults to base jar packages which is usually 8)" >&2
25 v) VERSION=${OPTARG};;
31 [ -z $JARFILE ] && usagexit
32 [ -f $JARFILE ] || error "No file $JARFILE"
33 [ -r $JARFILE ] || error "$JARFILE not readable"
36 SUFFIX=-SINGLE_RELEASE
37 FILENAME=$(basename $JARFILE)
38 BASE=$(basename -s $EXT $JARFILE)
39 DIR=$(dirname $JARFILE)
41 # set absolute path to $JARFILE if not specified
42 [ x${DIR#/} = x$DIR ] && DIR=$(cd "$DIR" && pwd)
44 TMPDIR=/tmp/$USER-$CMD-$BASE-$$
46 [ x$FILENAME = x$BASE ] && error "Should be $EXT file"
48 mkdir -p $TMPDIR || error "Could not create tmp dir $TMPDIR"
50 jar -xvf $JARFILE > /dev/null
51 VDIR=$TMPDIR/META-INF/versions
53 [ -d $VDIR ] || error "$JARFILE doesn't look like a multi-release jar file"
55 if [ -z $VERSION ]; then
56 # no version set... nothing to copy
58 elif [ -d $VDIR/$VERSION ]; then
59 # this version has alternative classes for the version asked for, copy them into the base jar
60 tar -cf - -C $VDIR/$VERSION . | tar -xf - -C $TMPDIR
62 echo "No specific classes for version $VERSION" >&2
63 echo "Available alternative versions are" >&2
68 # remove the alternative versions
71 # alter the manifest. note sed on macos is a bit weird
72 if [ `uname -s` = "Darwin" ]; then
73 sed -E -i '' 's/^([Mm]ulti-[Rr]elease):).*/\1 false/' $TMPDIR/META-INF/MANIFEST.MF
75 sed -E -i 's/^([Mm]ulti-[Rr]elease):).*/\1 false/' $TMPDIR/META-INF/MANIFEST.MF
78 jar -cvf $DIR/${BASE}${SUFFIX}${EXT} -C $TMPDIR . > /dev/null