From 4bc228c5c62079efd3cd6e2f95a8b3aaca161419 Mon Sep 17 00:00:00 2001 From: Serge Guzik Date: Fri, 1 Feb 2019 13:36:40 +0200 Subject: [PATCH] init: Base on sm2csv warp script --- .gitignore | 4 ++ README.md | 36 ++++++++++++++++ warp4j | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 warp4j diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6ed752 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bundle/ +fatjar/ +launcher/ +warped/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee710ee --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Warp4j + +Turn Java application into self-contained executable. + +## Features + +- automatically downloads JDKs +- creates custom runtimes optimized for your application +- creates self-contained binaries for Linux, MacOS and Windows using [warp-packer](https://github.com/dgiagio/warp) + +## Install + +TODO + +## Usage + +```bash +$ ls +my-app.jar + +$ warp4j my-app.jar +... +... +... + +$ ls +my-app.jar my-app-linux my-app-macos my-app.exe +``` + +## Cache Location + +Downloaded JDKs and prepared bundles are here: + +```bash +$HOME/.local/share/warp4j/ +``` diff --git a/warp4j b/warp4j new file mode 100755 index 0000000..995eba9 --- /dev/null +++ b/warp4j @@ -0,0 +1,123 @@ +#!/bin/bash + +JDK_VERSION=11.0.2 +JDK_URL_BASE=https://download.java.net/java/GA/jdk11/9/GPL +APP_NAME=sm2csv + +TARGETS=(linux osx windows) + +JDK_DISTRO_FILES=(\ + openjdk-"$JDK_VERSION"_${TARGETS[0]}-x64_bin.tar.gz \ + openjdk-"$JDK_VERSION"_${TARGETS[1]}-x64_bin.tar.gz \ + openjdk-"$JDK_VERSION"_${TARGETS[2]}-x64_bin.zip +) + +DIR="$(cd "$(dirname "$0")" ; pwd -P)" +JAR_PATH=$DIR/fatjar/$APP_NAME.jar +JDK_DOWNLOAD_PATH=$DIR/jdk +BUNDLES_PATH=$DIR/bundle +WARPED_PATH=$DIR/warped +LAUNCHER_PATH=$DIR/launcher + +JAVA_HOME="$JDK_DOWNLOAD_PATH/linux" +JLINK="$JAVA_HOME/bin/jlink" + +if [ ! -d $JDK_DOWNLOAD_PATH ]; then + mkdir -p $JDK_DOWNLOAD_PATH + echo "Downloading JDKs..." + for file_name in ${JDK_DISTRO_FILES[@]}; do + echo "$file_name..." + curl --progress-bar \ + $JDK_URL_BASE/$file_name \ + --output $JDK_DOWNLOAD_PATH/$file_name + done + + echo "Uncompressing JDKs..." + (cd $JDK_DOWNLOAD_PATH + echo "${TARGETS[0]}..." + rm -rf $JDK_DOWNLOAD_PATH/${TARGETS[0]} + mkdir -p $JDK_DOWNLOAD_PATH/${TARGETS[0]} + tar --strip-components=1 -C ${TARGETS[0]} -xzf ${JDK_DISTRO_FILES[0]} + + echo "${TARGETS[1]}..." + rm -rf $JDK_DOWNLOAD_PATH/${TARGETS[1]} + mkdir -p $JDK_DOWNLOAD_PATH/${TARGETS[1]} + tar --strip-components=4 -C ${TARGETS[1]} -xzf ${JDK_DISTRO_FILES[1]} \ + ./jdk-$JDK_VERSION.jdk/Contents/Home + + echo "${TARGETS[2]}..." + rm -rf $JDK_DOWNLOAD_PATH/${TARGETS[2]} + unzip -oq ${JDK_DISTRO_FILES[2]} + mv jdk-$JDK_VERSION ${TARGETS[2]} + ) + + # check if downloaded successfully + if [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[0]}/bin/java ] || \ + [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[1]}/bin/java ] || \ + [ ! -f $JDK_DOWNLOAD_PATH/${TARGETS[2]}/bin/java.exe ]; then + echo "Failed to download JDKs" + exit 1 + fi +else + echo "JDKs already present, skip download" +fi + +rm -rf $BUNDLES_PATH + +echo "Creating minimal JDKs..." +for target in ${TARGETS[@]}; do + echo "${target}..." + $JLINK \ + --no-header-files \ + --no-man-pages \ + --module-path $JDK_DOWNLOAD_PATH/${target}/jmods \ + --add-modules $(jdeps --print-module-deps $JAR_PATH | grep -v Warning) \ + --output $BUNDLES_PATH/${target}/jdk +done + +echo "Adding launchers..." +cp $LAUNCHER_PATH/$APP_NAME.sh $BUNDLES_PATH/${TARGETS[0]}/ +cp $LAUNCHER_PATH/$APP_NAME.sh $BUNDLES_PATH/${TARGETS[1]}/ +cp $LAUNCHER_PATH/$APP_NAME.cmd $BUNDLES_PATH/${TARGETS[2]}/ + +echo "Adding jars..." +cp $JAR_PATH $BUNDLES_PATH/${TARGETS[0]}/ +cp $JAR_PATH $BUNDLES_PATH/${TARGETS[1]}/ +cp $JAR_PATH $BUNDLES_PATH/${TARGETS[2]}/ + +rm -rf $WARPED_PATH + +echo "Warping linux target..." +mkdir -p $WARPED_PATH/${TARGETS[0]} +warp-packer \ + --arch linux-x64 \ + --input_dir $BUNDLES_PATH/${TARGETS[0]} \ + --exec $APP_NAME.sh \ + --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 +mv $WARPED_PATH/${TARGETS[0]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[0]} +rmdir $WARPED_PATH/${TARGETS[0]} + +echo "Warping osx target..." +mkdir -p $WARPED_PATH/${TARGETS[1]} +warp-packer \ + --arch macos-x64 \ + --input_dir $BUNDLES_PATH/${TARGETS[1]} \ + --exec $APP_NAME.sh \ + --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 +mv $WARPED_PATH/${TARGETS[1]}/$APP_NAME $WARPED_PATH/$APP_NAME-${TARGETS[1]} +rmdir $WARPED_PATH/${TARGETS[1]} + +echo "Warping windows target..." +mkdir -p $WARPED_PATH/${TARGETS[2]} +warp-packer \ + --arch windows-x64 \ + --input_dir $BUNDLES_PATH/${TARGETS[2]} \ + --exec $APP_NAME.cmd \ + --output $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe +(cd $WARPED_PATH/${TARGETS[2]} +zip -r $WARPED_PATH/$APP_NAME.${TARGETS[2]}-x64.zip $APP_NAME.exe +) +mv $WARPED_PATH/${TARGETS[2]}/$APP_NAME.exe $WARPED_PATH/ +rmdir $WARPED_PATH/${TARGETS[2]}