Skip to content

Instantly share code, notes, and snippets.

@psyllo
Created January 23, 2014 09:22
Show Gist options
  • Save psyllo/8575535 to your computer and use it in GitHub Desktop.
Save psyllo/8575535 to your computer and use it in GitHub Desktop.
Sonian Scotty Launch4j JAR to EXE build automation
#!/usr/bin/env bash
# Synopsis:
# Build a scotty.exe from a scotty.jar using Launch4j
# Helpful docs:
# - http://launch4j.sourceforge.net/docs.html
# - https://gist.github.com/iantruslove/8fe3631fdd14b2417758
# The primary dependencies to resolve are:
# 1 - The scotty.jar (standalone/uberjar)
# 2 - The location of the JRE
# Setup the most important variables
scotty_optional_prebuilt_jar=$1
original_pwd=`pwd`
tmp_dir=/tmp/scotty_exe
scotty_jar_default=target/scotty-snapshot-standalone.jar
scotty_exe_filename=scotty.exe
l4j_tarball_url='http://softlayer-dal.dl.sourceforge.net/project/launch4j/launch4j-3/3.1.0-beta2/launch4j-3.1.0-beta2-linux.tgz'
l4j_tarball=launch4j-3.1.0-beta2-linux.tgz
l4j_dir=launch4j
l4j_cmd=./launch4j
jre_used_by_l4j=`find /usr/lib/jvm/ -name 'jre' -type d | head -n 1`
bundled_jre_zip_url='https://bitbucket.org/alexkasko/openjdk-unofficial-builds/downloads/openjdk-1.7.0-u45-unofficial-icedtea-2.4.3-windows-i586-image.zip'
bundled_jre_zip=openjdk-1.7.0-u45-unofficial-icedtea-2.4.3-windows-i586-image.zip
bundled_jre_zip_dir=openjdk-1.7.0-u45-unofficial-icedtea-2.4.3-windows-i586-image
bundled_jre_dir=$tmp_dir/$bundled_jre_zip_dir/jre
bundled_scotty_config_file=test/resources/scotty.edn
final_zip_name=scotty.zip
# Test Scotty before continuing
lein test
if [ $? != 0 ]; then
echo Oops. Scotty unit tests command returned an error.
exit 1
fi
# Find or build Scotty JAR
# 1 - Use the optional argument to this script
# 2 - Or use a defaut
scotty_jar=${scotty_optional_prebuilt_jar:-$scotty_jar_default}
if [ ! -f "$scotty_jar" ]; then
if [ -z "$scotty_optional_prebuilt_jar" ]; then
lein uberjar
if [ $? != 0 ]; then
echo Oops. The Scotty uberjar command returned an error.
exit 1
fi
fi
if [ ! -f $scotty_jar ]; then
echo
echo ' Oops. I could find or build the scotty JAR that you'
echo ' want to convert into an EXE.'
echo
echo " I tried, but could not find or build: $scotty_jar"
echo
echo ' Suggestion 1: Run this command from the Scotty base dir with'
echo ' no arguments.'
echo
echo " Suggestion 2: Specify a pre-built JAR. Like: $0 scotty.jar"
echo
exit 1
fi
fi
# Verify the existence of Scotty bundled config
if [ ! -f $bundled_scotty_config_file ]; then
echo Expected to find the file: $bundled_scotty_config_file
exit 1
fi
# Setup tmp dir
if [ -e $tmp_dir ]; then
if [ ! -d $tmp_dir ]; then
echo Scotty Launch4J tmp dir exists but is not a directory.
echo Please fix that.
exit 1
fi
else
mkdir $tmp_dir
fi
# cd into tmp dir
cd $tmp_dir
if [ $? != 0 ]; then
echo Cannot cd to tmp dir: $tmp_dir
exit 1
fi
echo "Working in tmp dir: `pwd`"
# Download Launch4j tarball if necessary, then extract it
if [ ! -e $l4j_dir ]; then
if [ ! -e $l4j_tarball ]; then
wget $l4j_tarball_url
if [ $? != 0 ]; then
echo Error while downloading tarball from: $l4j_tarball_url
exit 1
fi
fi
echo Extracting $l4j_tarball
tar -xf $l4j_tarball
if [ ! -d $l4j_dir ]; then
echo Extracted Launch4j tarball but did not find the directory: $l4j_dir
exit 1
fi
fi
# Download JRE zip if necessary, then extract it
if [ ! -e $bundled_jre_zip_dir ]; then
if [ ! -e $bundled_jre_zip ]; then
wget $bundled_jre_zip_url
if [ $? != 0 ]; then
echo Error while downloading tarball from: $bundled_jre_zip_url
exit 1
fi
fi
echo Extracting $bundled_jre_zip
unzip $bundled_jre_zip
if [ ! -d $bundled_jre_zip_dir ]; then
echo Extracted JRE ZIP but did not find the directory: $bundled_jre_zip_dir
exit 1
fi
fi
# cd into Launch4j directory
cd $l4j_dir
if [ $? != 0 ]; then
echo Failed to cd into directory: `pwd`/$l4j_dir
exit 1
fi
# Creating a unique output directory for this run
unique_output_dir=`mktemp -d $tmp_dir/scotty_l4j_exe_output_XXXXXXXX`
# Copy the Scotty JAR
scotty_jar_abs=$original_pwd/$scotty_jar
if [ ! -f $scotty_jar_abs ]; then
echo Expected the Scotty JAR at: $scotty_jar_abs
exit 1
fi
cp $scotty_jar_abs $unique_output_dir/
# Generate L4j scotty EXE build config
scotty_l4j_config=$unique_output_dir/scotty_l4j.xml
echo "Writing $scotty_l4j_config ..."
echo '<launch4jConfig>' > $scotty_l4j_config
echo ' <headerType>console</headerType>' >> $scotty_l4j_config
echo " <jar>`basename $scotty_jar`</jar>" >> $scotty_l4j_config
echo " <outfile>$scotty_exe_filename</outfile>" >> $scotty_l4j_config
echo ' <chdir>.</chdir>' >> $scotty_l4j_config
echo ' <jre>' >> $scotty_l4j_config
echo " <path>$jre_used_by_l4j</path>" >> $scotty_l4j_config
echo ' </jre>' >> $scotty_l4j_config
echo '</launch4jConfig>' >> $scotty_l4j_config
# The Launch4j EXE build command
$l4j_cmd $scotty_l4j_config
if [ $? != 0 ]; then
echo The Launch4j EXE build command returned an error
exit 1
fi
if [ ! -f $scotty_exe ]; then
echo Apparently Launch4j did not generate the file: $scotty_exe
exit 1
fi
# Verify that the 'zip' CLI tool exists
which zip
if [ $? != 0 ]; then
echo Missing the command line tool 'zip'
exit 1
fi
#
# Bundle everything into a single ZIP
#
zip_tmp_dir=`mktemp -d $tmp_dir/scotty_zip_XXXXXXXX`
final_zip_abs_name=$zip_tmp_dir/$final_zip_name
rm $final_zip_abs_name > /dev/null 2>&1
if [ -e $final_zip_name ]; then
echo I tried to delete this fine but failed: $final_zip_abs_name
exit 1
fi
cd $tmp_dir/
cp -R $bundled_jre_dir/ $zip_tmp_dir/jre
cp $unique_output_dir/$scotty_exe_filename $zip_tmp_dir/
cp $original_pwd/$bundled_scotty_config_file $zip_tmp_dir/
cd $zip_tmp_dir
zip --quiet --recurse-paths $final_zip_name \
jre `basename $bundled_scotty_config_file` $scotty_exe_filename
if [ $? != 0 ]; then
echo Error returned while zipping file.
exit 1
fi
# Verfiy that the final zip was created
if [ ! -f $final_zip_abs_name ]; then
echo I thought I built this file, but I cannot see it: $final_zip_abs_name
exit 1
fi
echo Created: $final_zip_abs_name
echo Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment