From 382f5ff058a4c2fed1e9874bb712b40f16ec1a4e Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 3 Dec 2018 15:18:17 +0000 Subject: [PATCH] JAL-3130 de-multi-release-jar.sh Script to make multi-release jar file single release. --- utils/de-multi-release-jar.sh | 77 ++++++++++++++++++++++++++++++++ utils/{modulify.sh => modulify-jar.sh} | 6 +-- 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100755 utils/de-multi-release-jar.sh rename utils/{modulify.sh => modulify-jar.sh} (88%) diff --git a/utils/de-multi-release-jar.sh b/utils/de-multi-release-jar.sh new file mode 100755 index 0000000..6a3af4f --- /dev/null +++ b/utils/de-multi-release-jar.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash + +CMD=$(basename $0) + +usage() { + echo "Usage: $CMD [-v N] /path/to/jarfile" >&2 + echo " -v N build jar with alternative classes for version N of java (optional, defaults to base jar packages which is usually 8)" >&2 +} + +usagexit() { + usage + exit 1 +} + +error() { + echo $1 >&2 + usagexit +} + +VERSION="" +while getopts v: opt +do + case "${opt}" + in + v) VERSION=${OPTARG};; + esac +done +shift $((OPTIND-1)) + +JARFILE=$1 +[ -z $JARFILE ] && usagexit +[ -f $JARFILE ] || error "No file $JARFILE" +[ -r $JARFILE ] || error "$JARFILE not readable" + +EXT=.jar +SUFFIX=-SINGLE_RELEASE +FILENAME=$(basename $JARFILE) +BASE=$(basename -s $EXT $JARFILE) +DIR=$(dirname $JARFILE) + +TMPDIR=/tmp/$USER-$CMD-$BASE-$$ + +[ x$FILENAME = x$BASE ] && error "Should be $EXT file" + +mkdir -p $TMPDIR || error "Could not create tmp dir $TMPDIR" +cd $TMPDIR +jar -xvf $JARFILE > /dev/null +VDIR=$TMPDIR/META-INF/versions + +[ -d $VDIR ] || error "$JARFILE doesn't look like a multi-release jar file" + +if [ -z $VERSION ]; then + # no version set... nothing to copy + echo "" +elif [ -d $VDIR/$VERSION ]; then + # this version has alternative classes for the version asked for, copy them into the base jar + tar -cf - -C $VDIR/$VERSION . | tar -xf - -C $TMPDIR +else + echo "No specific classes for version $VERSION" >&2 + echo "Available alternative versions are" >&2 + cd $VDIR + ls >&2 +fi + +# remove the alternative versions +/bin/rm -r $VDIR + +# alter the manifest. note sed on macos is a bit weird +if [ `uname -s` = "Darwin" ]; then + sed -E -i '' 's/^([Mm]ulti-[Rr]elease):).*/\1 false/' $TMPDIR/META-INF/MANIFEST.MF +else + sed -E -i 's/^([Mm]ulti-[Rr]elease):).*/\1 false/' $TMPDIR/META-INF/MANIFEST.MF +fi + +jar -cvf $DIR/${BASE}${SUFFIX}${EXT} -C $TMPDIR . > /dev/null +rm -rf $TMPDIR + diff --git a/utils/modulify.sh b/utils/modulify-jar.sh similarity index 88% rename from utils/modulify.sh rename to utils/modulify-jar.sh index 428ed6e..65ca36f 100755 --- a/utils/modulify.sh +++ b/utils/modulify-jar.sh @@ -27,20 +27,20 @@ FILENAME=$(basename $JARFILE) BASE=$(basename -s $EXT $JARFILE) DIR=$(dirname $JARFILE) -TMPDIR=/tmp/$USER-$CMD-$BASE +TMPDIR=/tmp/$USER-$CMD-$BASE-$$ [ x$FILENAME = x$BASE ] && error "Should be $EXT file" mkdir -p $TMPDIR/jar || error "Could not create tmp dir $TMPDIR/jar" mkdir -p $TMPDIR/info || error "Could not create tmp dir $TMPDIR/info" cd $TMPDIR/jar -jar -xvf $JARFILE +jar -xvf $JARFILE > /dev/null jdeps --generate-module-info $TMPDIR/info $JARFILE # next line assuming only one module-info.java file created, I think this is always true...? It'll just use the last one if not. find $TMPDIR/info -name "module-info.java" -exec /bin/mv {} . \; [ -e ./module-info.java ] || error "No module-info.java file found in $TMPDIR/info" javac -d $TMPDIR/jar ./module-info.java -jar -cvf $DIR/${BASE}${SUFFIX}${EXT} -C $TMPDIR/jar . +jar -cvf $DIR/${BASE}${SUFFIX}${EXT} -C $TMPDIR/jar . > /dev/null rm -rf $TMPDIR -- 1.7.10.2