feature/manual_and_auto_classpath_option (#1)

Reviewed-on: https://git.kirby-link.dd-dns.de/public/warp4j/pulls/1
This commit is contained in:
David 2022-08-12 18:40:09 +02:00
parent aeffdc3eb5
commit 6ee83e1056
6 changed files with 77 additions and 30 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
fatjar/
warped/
TODO.md
/.project

View File

@ -6,7 +6,7 @@ RUN apt-get update && apt-get install -y \
zip \
&& rm -rf /var/lib/apt/lists/*
RUN curl -fsSL -o /tmp/warp-packer \
https://github.com/dgiagio/warp/releases/download/v0.3.0/linux-x64.warp-packer \
https://git.kirby-link.dd-dns.de/attachments/f701fbff-c58b-4aac-91e3-47efda1fc760 \
&& install -D \
--mode=755 \
--owner=root \

View File

@ -4,11 +4,11 @@ Here are several more ways to install `warp4j`.
## Manual
First install [warp-packer](https://github.com/dgiagio/warp/releases) and ensure other common tools available: `awk`, `curl`, `file`, `grep`, `sed`, `sort`, `tar`, `unzip`, optional: `zip`. Then install `warp4j` like this:
First install [warp-packer](https://git.kirby-link.dd-dns.de/public/warp/releases) and ensure other common tools available: `awk`, `curl`, `file`, `grep`, `sed`, `sort`, `tar`, `unzip`, optional: `zip`. Then install `warp4j` like this:
```
$ LOCATION=/usr/local/bin \
LINK=https://raw.githubusercontent.com/guziks/warp4j/stable/warp4j \
LINK=https://git.kirby-link.dd-dns.de/public/warp4j/raw/branch/master/warp4j \
TEMP_LOCATION=/tmp/warp4j \
bash -c 'curl -fsSL -o $TEMP_LOCATION $LINK && \
sudo install -D \

View File

@ -6,7 +6,7 @@ Turn JAR (java archive) into self-contained executable in a single command.
- downloads java runtimes automatically
- makes runtimes optimized for your application
- creates self-contained binaries for Linux, macOS and Windows using [warp-packer](https://github.com/dgiagio/warp)
- creates self-contained binaries for Linux, macOS and Windows using [warp-packer](https://git.kirby-link.dd-dns.de/public/warp)
- works on Linux, macOS and Windows (with Windows Subsystem for Linux)
- supports cross "compilation"
- does not require neither JDK nor JRE installed
@ -20,7 +20,7 @@ Just put both `warp4j` and `warp-packer` somewhere in your PATH and run `warp4j
Curl one-liner installs latest versions of both `warp4j` and `warp-packer`:
```
$ bash -c "$(curl -fsSL https://raw.githubusercontent.com/guziks/warp4j/stable/install)"
$ bash -c "$(curl -fsSL https://git.kirby-link.dd-dns.de/public/warp4j/raw/branch/master/install)"
```
This script will show missing dependencies (if there are any), they must be installed with your package manager.
@ -63,8 +63,12 @@ Turn JAR (java archive) into self-contained executable
Options:
-j, --java-version <version>
override JDK/JRE version
examples: "11", "11.0", "11.0.2", "11.0.2+9"
(default: 11)
examples: "17", "17.0", "17.0.2", "17.0.2+9"
(default: 17)
-cp, --class-path <classpath>
adds additional classpaths to the jdeps call
--auto-class-path extract and get class-path values from jar file
ignored when -cp, --class-path is set
-o, --output <directory>
override output directory;
this is relative to current PWD
@ -93,6 +97,7 @@ Options:
"java <options> -jar <jar file>";
use quotes when passing multiple options
example: '-Xms512m -Xmx1024m'
-s, --silent using javaw instead of java for windows
-h, --help show this message
```
@ -100,6 +105,7 @@ Options:
Tested on the following operating systems:
- Debian 10.8
- Ubuntu 18.04
- macOS Mojave
- Windows Subsystem for Linux with Ubuntu 14.04

26
install
View File

@ -12,7 +12,9 @@ function fail() {
# platform IDs
LIN=linux
LIN_URL=https://git.kirby-link.dd-dns.de/attachments/f701fbff-c58b-4aac-91e3-47efda1fc760
MAC=macos
MAC_URL=https://git.kirby-link.dd-dns.de/attachments/b09c6469-406a-4dd1-b5e8-1294a3aabf0f
WIN=windows
# returns this platform ID
@ -32,20 +34,14 @@ function get_this_platform() {
THIS_PLATFORM=$(get_this_platform)
# fetches latest release download link for the platform
function get_latest_warp_link() {
function get_warp_link() {
local this_platform=$1
local info
info=$(curl -fsSL https://api.github.com/repos/dgiagio/warp/releases)
if [[ $? != 0 ]]; then
echo "Error: Failed to get information about releases" >&2
fail
fi
echo "$info" \
| grep "browser_download_url" \
| grep "$this_platform-x64.warp-packer" \
| head -n 1 \
| awk '{print $2}' \
| sed -e 's/"//g'
if [ "$this_platform" = "$LIN" ]; then
echo "$LIN_URL"
else
echo "$MAC_URL"
fi
}
# downloads and installs single binary
@ -91,10 +87,10 @@ function get_missing_deps() {
done
}
WARP4J_LINK="https://raw.githubusercontent.com/guziks/warp4j/stable/warp4j"
WARP4J_LINK="https://git.kirby-link.dd-dns.de/public/warp4j/raw/branch/master/warp4j"
echo "Getting information about warp-packer releases..."
WARP_LINK=$(get_latest_warp_link $THIS_PLATFORM)
WARP_LINK=$(get_warp_link $THIS_PLATFORM)
MISSING_DEPS=$(get_missing_deps)

58
warp4j
View File

@ -9,8 +9,12 @@ function print_help {
echo 'Options:'
echo ' -j, --java-version <version>'
echo ' override JDK/JRE version'
echo ' examples: "11", "11.0", "11.0.2", "11.0.2+9"'
echo ' (default: 11)'
echo ' examples: "17", "17.0", "17.0.2", "17.0.2+9"'
echo ' (default: 17)'
echo ' -cp, --class-path <classpath>'
echo ' adds additional classpaths to the jdeps call'
echo ' --auto-class-path extract and get class-path values from jar file'
echo ' ignored when -cp, --class-path is set'
echo ' -o, --output <directory>'
echo ' override output directory;'
echo ' this is relative to current PWD'
@ -27,6 +31,7 @@ function print_help {
echo ' --pull check if more recent JDK/JRE distro is available;'
echo ' by default latest cached version that matches'
echo ' "--java-version" is used'
echo ' to get the classpath for jdeps call'
echo ' --linux create binary for Linux'
echo ' --macos create binary for macOS'
echo ' --windows create binary for Windows'
@ -39,6 +44,7 @@ function print_help {
echo ' "java <options> -jar <jar file>";'
echo ' use quotes when passing multiple options'
echo " example: '-Xms512m -Xmx1024m'"
echo ' -s, --silent using javaw instead of java for windows'
echo ' -h, --help show this message'
exit
}
@ -104,6 +110,10 @@ while [[ $# -gt 0 ]]; do
print_help
exit
;;
-cp|--class-path)
CLASS_PATH="$2"
shift 2
;;
-j|--java-version)
JAVA_VERSION="$2"
JAVA_VERSION_OVERRIDEN=true
@ -145,6 +155,14 @@ while [[ $# -gt 0 ]]; do
JVM_OPTIONS="$2"
shift 2
;;
--auto-class-path)
AUTO_CLASS_PATH=true
shift
;;
-s|--silent)
SILENT=true
shift
;;
-*|--*) # unsupported options
fail_with "Unsupported option $1"
;;
@ -223,7 +241,7 @@ if [[ $JVM_IMPL ]] &&
fail_with "jvm implementation \"$JVM_IMPL\" is not correct"
fi
LATEST_LTS=11 # latest LTS java branch
LATEST_LTS=17 # latest LTS java branch
# default options
test -z $JAVA_VERSION && JAVA_VERSION=$LATEST_LTS
@ -369,6 +387,11 @@ fi
JAVA_DOWNLOAD_PATH=$CACHE_PATH/$JAVA_DISTRO_TYPE/$JVM_IMPL
BUNDLES_PATH=$CACHE_PATH/bundle # prepare bundles here
# path for extracted jar files
EXTRACTED_JAR_PATH=$CACHE_PATH/app-jar # prepare bundles here
mkdir -p ${EXTRACTED_JAR_PATH}
trap 'rm -rf ${EXTRACTED_JAR_PATH}' EXIT
DIR="$(pwd -P)" # execution directory path
# final binaries created in WARPED_TEMP_PATH and then moved to WARPED_PATH
@ -390,7 +413,7 @@ JAVA_DIST='"$BUNDLED_DISTRO_SUBDIR"'
JAR='"$JAR_NAME"'.jar
DIR="$(cd "$(dirname "$0")" ; pwd -P)"
JAVA=$DIR/$JAVA_DIST/bin/java
JAVA=$DIR/$JAVA_DIST/bin/'"$JAVA_EXEC"'
JAR_PATH=$DIR/$JAR
exec "$JAVA" '"$JVM_OPTIONS"' -jar "$JAR_PATH" "$@"
@ -407,10 +430,10 @@ SETLOCAL
SET "JAVA_DIST='"$BUNDLED_DISTRO_SUBDIR"'"
SET "JAR='"$JAR_NAME"'.jar"
SET "JAVA=%~dp0\%JAVA_DIST%\bin\java.exe"
SET "JAVA=%~dp0\%JAVA_DIST%\bin\'"$JAVA_EXEC"'
SET "JAR_PATH=%~dp0\%JAR%"
CALL %JAVA% '"$JVM_OPTIONS"' -jar %JAR_PATH% %*
START %JAVA% '"$JVM_OPTIONS"' -jar %JAR_PATH% %*
EXIT /B %ERRORLEVEL%
'
}
@ -620,7 +643,21 @@ JDEPS=$JDK_PATH/bin/jdeps
if [[ $JAVA_DISTRO_TYPE == $DISTRO_TYPE_JDK ]]; then
echo "Analyzing dependencies..."
# TODO check for errors
MODULES=$("$JDEPS" --print-module-deps "$JAR" | grep -v Warning)
# TODO If JAVA_VERSION is not an INT it will throw an error
if [ -n "$CLASS_PATH" ]
then
echo "Using given classpaths: $CLASS_PATH"
MODULES=$($JDEPS --class-path=$CLASS_PATH --print-module-deps --ignore-missing-deps --multi-release $JAVA_VERSION_BASE $JAR | grep -v Warning)
elif [ "$AUTO_CLASS_PATH" ]
then
echo "Extracting jar file to get classpath"
unzip -q "${JAR}" -d "${EXTRACTED_JAR_PATH}"
echo "Fetching modules"
MODULES=$($JDEPS --ignore-missing-deps --print-module-deps --recursive --multi-release $JAVA_VERSION_BASE -quiet --class-path=${EXTRACTED_JAR_PATH}/BOOT-INF/lib/* --module-path=${EXTRACTED_JAR_PATH}/BOOT-INF/lib/BOOT-INF/lib/* $JAR | grep -v Warning)
else
echo "Fetch modules with default behavior"
MODULES=$("$JDEPS" --print-module-deps --ignore-missing-deps --multi-release $JAVA_VERSION_BASE "$JAR" | grep -v Warning)
fi
fi
# creates minimized runtime for the platform
@ -641,6 +678,13 @@ function create_optimized_runtime() {
# creates warp bundle for the platform
function create_bundle() {
local platform=$1
if [[ $SILENT ]] && [[ $platform == $WIN ]]; then
JAVA_EXEC=javaw
else
JAVA_EXEC=java
fi
case $JAVA_DISTRO_TYPE in
$DISTRO_TYPE_JDK)
create_optimized_runtime $platform