Skip to content

Instantly share code, notes, and snippets.

@eponvert
Last active December 15, 2015 00:09
Show Gist options
  • Save eponvert/5170595 to your computer and use it in GitHub Desktop.
Save eponvert/5170595 to your computer and use it in GitHub Desktop.
A handy shell script for generating a distribution of your Java Maven project as a CLI tool. The uses `mvn package` to create a jar file, writes out a little script to call the jar. For this to work, use shade or something to bundle the dependencies into the jar. With assembly, update the script to work with the assembly jar. Also, be sure to se…
#!/usr/bin/env bash
REBUILD_CMD=rebuild
if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "-?" ] ; then
cat<<EOF
Usage: $0 [$REBUILD_CMD]
With '$REBUILD_CMD', the script rebuilds the package Jar.
EOF
exit 0
fi
PROJ_POM=$(tr -d '\n ' < pom.xml)
PROJ_NAME=$(echo $PROJ_POM | perl -pe 's/^.*?<name>([^<]*)<.*$/\1/')
PROJ_VERS=$(echo $PROJ_POM | perl -pe 's/^.*?<version>([^<]*)<.*/\1/')
PROJ_FULL=$PROJ_NAME-$PROJ_VERS
PROJ_JAR=$PROJ_FULL.jar
if [ ! -e target/$PROJ_JAR ] || [ "$1" == "$REBUILD_CMD" ] ; then
echo -n 'building package... ' >/dev/stderr
mvn clean package 2>&1 1>/dev/null
if [ -e target/$PROJ_JAR ] ; then
echo 'done' >/dev/stderr
else
echo 'problem. run `mvn package` manually' >/dev/stderr
exit 1
fi
fi
cd target
echo java -jar \$\(dirname \$0\)/$PROJ_JAR > $PROJ_FULL
chmod +x $PROJ_FULL
ln -sf $PROJ_FULL $PROJ_NAME
echo -n 'building binary distributable... ' >/dev/stderr
BIN_DIST=$PROJ_FULL.tgz
tar -zcf $BIN_DIST $PROJ_FULL $PROJ_JAR $PROJ_NAME 2>/dev/null
if [ -e $BIN_DIST ] ; then
echo 'done' >/dev/stderr
else
echo "problem. run `tar -zcf $BIN_DIST $PROJ_FULL $PROJ_JAR $PROJ_NAME` manually" 2>/dev/null
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment