Make java-examples a dist build, add script to build runnable bot jars

This commit is contained in:
ghubstan 2022-06-24 14:48:27 -03:00
parent 0d44fc2148
commit 14d35527d9
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 154 additions and 1 deletions

View File

@ -1,5 +1,7 @@
plugins {
id 'java'
// Build a distribution without a Class-Path definition in the distribution's java-examples-X.X.X.jar file.
// A bash script will be used to create runnable jars from the Java bot examples in java-examples-X.X.X.jar
id 'application'
id 'com.google.protobuf' version '0.8.16'
}

View File

@ -0,0 +1,52 @@
#! /bin/bash
########################################################################################################################
# Create a runnable jar files from the java-examples' Gradle distribution tarball.
#
# Usage: $ ./create-bot-jars.sh 0.0.1-SNAPSHOT
#
# Should be called from this directory.
########################################################################################################################
VERSION="$1"
if [[ -z "$VERSION" ]]; then
VERSION="0.0.1-SNAPSHOT"
fi
# Get the script directory (relative to the current directory), cd into the directory, use pwd to get the absolute path.
export SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
cd "$SCRIPT_DIR"
export GRADLE_DIST_NAME="java-examples-$VERSION"
export GRADLE_DIST_TARBALL="$GRADLE_DIST_NAME.tar"
export GRADLE_DIST_PATH="../build/distributions/$GRADLE_DIST_TARBALL"
echo "Build runnable bot jars from $GRADLE_DIST_PATH"
ls -l "$GRADLE_DIST_PATH"
extractdistribution() {
# Copy the build's distribution tarball to this directory.
cp -v "$GRADLE_DIST_PATH" .
# Extract the tarball content.
rm -rf "$GRADLE_DIST_NAME"
tar -xf "$GRADLE_DIST_TARBALL"
cd "$SCRIPT_DIR/$GRADLE_DIST_NAME"
# TODO might want to just delete files in bin directory, and put java jar cmd scripts in them?
# Delete the bin directory.
rm -rf bin
echo "Run $ ls -l lib (lib dir should contain all distribution jars)"
ls -l lib
cd "$SCRIPT_DIR"
}
extractdistribution
./create-runnable-jar.sh "$GRADLE_DIST_NAME" bisq.bots.TakeBestPricedOfferToBuyBtc
./create-runnable-jar.sh "$GRADLE_DIST_NAME" bisq.bots.TakeBestPricedOfferToSellBtc
./create-runnable-jar.sh "$GRADLE_DIST_NAME" bisq.bots.TakeBestPricedOfferToBuyBsq
./create-runnable-jar.sh "$GRADLE_DIST_NAME" bisq.bots.TakeBestPricedOfferToSellBsq
rm -r "$GRADLE_DIST_TARBALL"
echo "Done"

View File

@ -0,0 +1,99 @@
#! /bin/bash
########################################################################################################################
# Create a runnable jar file containing a single class, and a manifest defining the complete classpath.
#
# Usage:
# `$ ./create-runnable-jar.sh java-examples-0.0.1-SNAPSHOT bisq.bots.TakeBestPricedOfferToSellBtc`
#
# Should be called from create-bot-jars.sh, with extracts and arranges the Gradle distribution for this script.
########################################################################################################################
GRADLE_DIST_NAME="$1"
if [[ -z "$GRADLE_DIST_NAME" ]]; then
echo "Gradle distribution name argument required."
exit
fi
FULLY_QUALIFIED_CLASSNAME="$2"
if [[ -z "$FULLY_QUALIFIED_CLASSNAME" ]]; then
echo "Fully qualified classname argument required."
exit
fi
tolowercasekebabname() {
STRING="$1"
# Say we have STRING=FuBarFooBar. We want to return fu-bar-foo-bar.
# 1. echo "$(echo "$STRING" | sed 's/[A-Z]\+/-&/g')" RETURNS "-Fu-Bar-Foo-Bar".
MESSY_KEBAB="$(echo "$STRING" | sed 's/[A-Z]\+/-&/g')"
# 2. Strip the leading "-".
CLEAN_KEBAB="$(echo "$MESSY_KEBAB" | sed 's/-//1')"
# 3. Lowercase everything using 'tr'.
LOWERCASE_KEBAB=$(echo "$CLEAN_KEBAB" | tr '[:upper:]' '[:lower:]')
echo "$LOWERCASE_KEBAB"
}
getsimpleclassname() {
FULLY_QUALIFIED_CLASSNAME="$1"
SIMPLE_CLASSNAME=$(echo "${FULLY_QUALIFIED_CLASSNAME##*.}")
echo "$SIMPLE_CLASSNAME"
}
getmainclassfilepath() {
FULLY_QUALIFIED_CLASSNAME="$1"
FILE_PATH=$(echo "$FULLY_QUALIFIED_CLASSNAME" | tr '.' '/')
echo "$FILE_PATH.class"
}
writemanifest() {
# Make the cli.jar runnable, and define its dependencies in a MANIFEST.MF file.
echo "Manifest-Version: 1.0" > MANIFEST.txt
echo "Main-Class: $FULLY_QUALIFIED_CLASSNAME" >> MANIFEST.txt
printf "Class-Path:" >> MANIFEST.txt
for file in lib/*
do
# Each new line in the classpath must be preceded by two spaces.
printf " %s\n" "$file" >> MANIFEST.txt
done
echo "Manifest for runnable jar:"
cat MANIFEST.txt
}
SIMPLE_CLASSNAME=$(getsimpleclassname "$FULLY_QUALIFIED_CLASSNAME")
# echo "SIMPLE_CLASSNAME = $SIMPLE_CLASSNAME"
JAR_BASENAME=$(tolowercasekebabname "$SIMPLE_CLASSNAME")
# echo "JAR_BASENAME = $JAR_BASENAME"
echo "Build runnable $JAR_BASENAME.jar containing only $FULLY_QUALIFIED_CLASSNAME"
cd $GRADLE_DIST_NAME
# Build MANIFEST.MF with a Main-Class.
writemanifest
# Create $JAR_BASENAME.jar with the NO class files.
jar --verbose --create --file "$JAR_BASENAME.jar" --manifest MANIFEST.txt
# Delete generated manifest.
rm MANIFEST.txt
# Get the relative file path for the Main-Class to be added to the new jar.
MAINCLASS_FILE_PATH=$(getmainclassfilepath "$FULLY_QUALIFIED_CLASSNAME")
# echo "MAINCLASS_FILE_PATH = $MAINCLASS_FILE_PATH"
# Extract the Main-Class from the distribution jar, to the current working directory.
jar xfv "lib/$GRADLE_DIST_NAME.jar" "$MAINCLASS_FILE_PATH" "$SIMPLE_CLASSNAME.properties"
echo "Extracted one class:"
ls -l bisq/bots/pazza
mv "$SIMPLE_CLASSNAME.properties" "$JAR_BASENAME.conf"
echo "Extracted one properties file and renamed it $JAR_BASENAME.conf"
ls -l *.conf
# Now it can be added to the empty jar with the correct path.
jar uf "$JAR_BASENAME.jar" "$MAINCLASS_FILE_PATH"
# Remove bisq (bisq/bots/junk).
rm -rf bisq
echo "Runnable $JAR_BASENAME.jar is ready to use."
echo "Usage: $ java -jar $JAR_BASENAME.jar --password=xyz --conf=$JAR_BASENAME.conf --dryrun=true"