feat: Remake to accept fatjar as an argument

This commit is contained in:
Serge Guzik 2019-02-01 22:01:25 +02:00
parent 4bc228c5c6
commit f6fd2e8e79

183
warp4j
View File

@ -1,8 +1,104 @@
#!/bin/bash #!/usr/bin/env bash
function print_help {
echo
echo 'Usage: warp4j [options] <file.jar>'
echo
echo 'Turn Java application into self-contained executable'
echo
echo 'Options:'
echo ' --jdk-version <version>'
echo ' override JDK version'
echo ' examples: "11.0.2", TODO more'
echo ' --jvm-options <options>'
echo ' passed to java before "-jar"'
echo ' --launcher-shell <path to shell script>'
echo ' custom launcher for Linux and MacOS'
echo ' (not implemented)'
echo ' --launcher-cmd <path to cmd script>'
echo ' custom launcher for Windows'
echo ' (not implemented)'
exit
}
# default options
JDK_VERSION=11.0.2 JDK_VERSION=11.0.2
LAUNCHER_SHELL=""
LAUNCHER_CMD=""
JVM_OPTIONS=""
if [[ $# -eq 0 ]]; then
print_help
exit
fi
# parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
print_help
exit
;;
--jdk-version)
JDK_VERSION="$2"
shift 2
;;
--launcher-shell)
LAUNCHER_SHELL="$2"
shift 2
;;
--launcher-cmd)
LAUNCHER_CMD="$2"
shift 2
;;
--jvm-options)
JVM_OPTIONS="$2"
shift 2
;;
-*|--*) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*)
POSITIONAL+=("$1") # store positional arguments
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional arguments
if [[ $# -gt 1 ]]; then
echo "Error: Too many arguments: $@, expecting only jar name" >&2
print_help
exit 1
else
JAR=$1
fi
JAR_FILE_BASE_NAME=$(basename -- "$JAR")
JAR_EXTENSION="${JAR_FILE_BASE_NAME##*.}"
JAR_EXTENSION_LOWERCASE=$(printf "%s" "$JAR_EXTENSION" | tr '[:upper:]' '[:lower:]')
JAR_NAME="${JAR_FILE_BASE_NAME%.*}"
# checking jar file exists
if [[ ! -e $JAR ]]; then
echo "Error: File \"$JAR\" does not exist" >&2
exit 1
fi
# checking file is actually java archive
if [[ $(file $JAR) != *"Zip"* ]] ||
[[ $JAR_EXTENSION_LOWERCASE != "jar" ]]; then
echo "Error: File \"$JAR\" is not a java archive" >&2
exit 1
fi
APP_NAME=$JAR_NAME
LAUNCHER_NAME=$JAR_NAME
JDK_URL_BASE=https://download.java.net/java/GA/jdk11/9/GPL JDK_URL_BASE=https://download.java.net/java/GA/jdk11/9/GPL
APP_NAME=sm2csv
TARGETS=(linux osx windows) TARGETS=(linux osx windows)
@ -12,16 +108,48 @@ JDK_DISTRO_FILES=(\
openjdk-"$JDK_VERSION"_${TARGETS[2]}-x64_bin.zip openjdk-"$JDK_VERSION"_${TARGETS[2]}-x64_bin.zip
) )
DIR="$(cd "$(dirname "$0")" ; pwd -P)" DIR="$(pwd -P)"
JAR_PATH=$DIR/fatjar/$APP_NAME.jar CACHE_PATH=$HOME/.local/share/warp4j
JDK_DOWNLOAD_PATH=$DIR/jdk JDK_DOWNLOAD_PATH=$CACHE_PATH/jdk
BUNDLES_PATH=$DIR/bundle BUNDLES_PATH=$CACHE_PATH/bundle
WARPED_PATH=$DIR/warped WARPED_PATH=$DIR/warped
LAUNCHER_PATH=$DIR/launcher
JAVA_HOME="$JDK_DOWNLOAD_PATH/linux" JAVA_HOME="$JDK_DOWNLOAD_PATH/linux"
JLINK="$JAVA_HOME/bin/jlink" JLINK="$JAVA_HOME/bin/jlink"
function print_launcher_bash() {
printf "%s" \
'#!/usr/bin/env bash
JAVA_DIST=jdk
JAR='$JAR_NAME'.jar
DIR="$(cd "$(dirname "$0")" ; pwd -P)"
JAVA=$DIR/$JAVA_DIST/bin/java
JAR_PATH=$DIR/$JAR
exec $JAVA '$JVM_OPTIONS' -jar $JAR_PATH $@
'
}
function print_launcher_cmd() {
printf "%s" \
'@ECHO OFF
SETLOCAL
SET "JAVA_DIST=jdk"
SET "JAR='$JAR_NAME'.jar"
SET "JAVA=%~dp0\%JAVA_DIST%\bin\java.exe"
SET "JAR_PATH=%~dp0\%JAR%"
CALL %JAVA% '$JVM_OPTIONS' -jar %JAR_PATH% %*
EXIT /B %ERRORLEVEL%
'
}
function download_jdks() {
if [ ! -d $JDK_DOWNLOAD_PATH ]; then if [ ! -d $JDK_DOWNLOAD_PATH ]; then
mkdir -p $JDK_DOWNLOAD_PATH mkdir -p $JDK_DOWNLOAD_PATH
echo "Downloading JDKs..." echo "Downloading JDKs..."
@ -55,15 +183,15 @@ if [ ! -d $JDK_DOWNLOAD_PATH ]; then
if [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[0]}/bin/java ] || \ if [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[0]}/bin/java ] || \
[ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[1]}/bin/java ] || \ [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[1]}/bin/java ] || \
[ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[2]}/bin/java.exe ]; then [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[2]}/bin/java.exe ]; then
echo "Failed to download JDKs" echo "Error: Failed to download JDKs" >&2
exit 1 exit 1
fi fi
else else
echo "JDKs already present, skip download" echo "JDKs already present, skip download"
fi fi
}
rm -rf $BUNDLES_PATH function create_optimized_jdks() {
echo "Creating minimal JDKs..." echo "Creating minimal JDKs..."
for target in ${TARGETS[@]}; do for target in ${TARGETS[@]}; do
echo "${target}..." echo "${target}..."
@ -71,20 +199,28 @@ for target in ${TARGETS[@]}; do
--no-header-files \ --no-header-files \
--no-man-pages \ --no-man-pages \
--module-path $JDK_DOWNLOAD_PATH/${target}/jmods \ --module-path $JDK_DOWNLOAD_PATH/${target}/jmods \
--add-modules $(jdeps --print-module-deps $JAR_PATH | grep -v Warning) \ --add-modules $(jdeps --print-module-deps $JAR | grep -v Warning) \
--output $BUNDLES_PATH/${target}/jdk --output $BUNDLES_PATH/${target}/jdk
done done
}
function create_bundles() {
rm -rf $BUNDLES_PATH
create_optimized_jdks
echo "Adding launchers..." echo "Adding launchers..."
cp $LAUNCHER_PATH/$APP_NAME.sh $BUNDLES_PATH/${TARGETS[0]}/ print_launcher_bash > $BUNDLES_PATH/${TARGETS[0]}/$LAUNCHER_NAME.sh
cp $LAUNCHER_PATH/$APP_NAME.sh $BUNDLES_PATH/${TARGETS[1]}/ print_launcher_bash > $BUNDLES_PATH/${TARGETS[1]}/$LAUNCHER_NAME.sh
cp $LAUNCHER_PATH/$APP_NAME.cmd $BUNDLES_PATH/${TARGETS[2]}/ print_launcher_cmd > $BUNDLES_PATH/${TARGETS[2]}/$LAUNCHER_NAME.cmd
echo "Adding jars..." echo "Adding jars..."
cp $JAR_PATH $BUNDLES_PATH/${TARGETS[0]}/ cp $JAR $BUNDLES_PATH/${TARGETS[0]}/
cp $JAR_PATH $BUNDLES_PATH/${TARGETS[1]}/ cp $JAR $BUNDLES_PATH/${TARGETS[1]}/
cp $JAR_PATH $BUNDLES_PATH/${TARGETS[2]}/ cp $JAR $BUNDLES_PATH/${TARGETS[2]}/
}
function warp_targets() {
rm -rf $WARPED_PATH rm -rf $WARPED_PATH
echo "Warping linux target..." echo "Warping linux target..."
@ -92,7 +228,7 @@ mkdir -p $WARPED_PATH/${TARGETS[0]}
warp-packer \ warp-packer \
--arch linux-x64 \ --arch linux-x64 \
--input_dir $BUNDLES_PATH/${TARGETS[0]} \ --input_dir $BUNDLES_PATH/${TARGETS[0]} \
--exec $APP_NAME.sh \ --exec $LAUNCHER_NAME.sh \
--output $WARPED_PATH/${TARGETS[0]}/$APP_NAME --output $WARPED_PATH/${TARGETS[0]}/$APP_NAME
tar -C $WARPED_PATH/${TARGETS[0]} -czf $WARPED_PATH/$APP_NAME.${TARGETS[0]}-x64.tar.gz $APP_NAME tar -C $WARPED_PATH/${TARGETS[0]} -czf $WARPED_PATH/$APP_NAME.${TARGETS[0]}-x64.tar.gz $APP_NAME
mv $WARPED_PATH/${TARGETS[0]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[0]} mv $WARPED_PATH/${TARGETS[0]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[0]}
@ -103,7 +239,7 @@ mkdir -p $WARPED_PATH/${TARGETS[1]}
warp-packer \ warp-packer \
--arch macos-x64 \ --arch macos-x64 \
--input_dir $BUNDLES_PATH/${TARGETS[1]} \ --input_dir $BUNDLES_PATH/${TARGETS[1]} \
--exec $APP_NAME.sh \ --exec $LAUNCHER_NAME.sh \
--output $WARPED_PATH/${TARGETS[1]}/$APP_NAME --output $WARPED_PATH/${TARGETS[1]}/$APP_NAME
tar -C $WARPED_PATH/${TARGETS[1]} -czf $WARPED_PATH/$APP_NAME.${TARGETS[1]}-x64.tar.gz $APP_NAME tar -C $WARPED_PATH/${TARGETS[1]} -czf $WARPED_PATH/$APP_NAME.${TARGETS[1]}-x64.tar.gz $APP_NAME
mv $WARPED_PATH/${TARGETS[1]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[1]} mv $WARPED_PATH/${TARGETS[1]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[1]}
@ -114,10 +250,15 @@ mkdir -p $WARPED_PATH/${TARGETS[2]}
warp-packer \ warp-packer \
--arch windows-x64 \ --arch windows-x64 \
--input_dir $BUNDLES_PATH/${TARGETS[2]} \ --input_dir $BUNDLES_PATH/${TARGETS[2]} \
--exec $APP_NAME.cmd \ --exec $LAUNCHER_NAME.cmd \
--output $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe --output $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe
(cd $WARPED_PATH/${TARGETS[2]} (cd $WARPED_PATH/${TARGETS[2]}
zip -r $WARPED_PATH/$APP_NAME.${TARGETS[2]}-x64.zip $APP_NAME.exe zip -r $WARPED_PATH/$APP_NAME.${TARGETS[2]}-x64.zip $APP_NAME.exe
) )
mv $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe $WARPED_PATH/ mv $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe $WARPED_PATH/$APP_NAME-windows.exe
rmdir $WARPED_PATH/${TARGETS[2]} rmdir $WARPED_PATH/${TARGETS[2]}
}
download_jdks
create_bundles
warp_targets