Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Last active May 13, 2020 09:40
Show Gist options
  • Save mcgivrer/85075539679f32763146ee4d9335a437 to your computer and use it in GitHub Desktop.
Save mcgivrer/85075539679f32763146ee4d9335a437 to your computer and use it in GitHub Desktop.
a buils script for java project

Build

This very useful build.sh script allow you to build any java project with the maven inspired file structure below. It is particularly adapeted to very light machine : ARM archtecture based ones a.k.a. RaspberryPi, OrangePi, BananaPi, etc ...

It is using only bash, javac, java,jar and git command line instructions.

Project Structure

${projectfolder}/
 |_ lib
 |  |_ options.sh
 |  |_ stub.sh
 |_ src
 |  |_ main
 |     |_ java
 |     |  |_ my
 |     |     |_ program
 |     |        |_ package
 |     |           |_ MyMainClass.java
 |     |_ resources
 |        |_ res
 |        |  |_ images
 |        |  |  |_ mylogo.png
 |        |  |_ game.properties
 |_ .gitignore
 |_ README.md
 |_ build.sh
 (|_ pom.xml)

Configuration

The first variables must be adapted to your needs:

  • PROGRAM_NAME Your program name,
  • PROGRAM_VERSION The version of your program,
  • MAINCLASS canonical name of your JAR entry point class,
  • VENDOR_NAME the vendor for this program,
  • AUTHOR_NAME the author of this program.

The manifest is generated by the buils.sh script.

McG.

Thanks to https://github.com/maynooth/CS210/wiki/Convert-Java-Executable-to-Linux-Executable for creating a linux executable with concatenated sh and jar file.

#!/bin/bash
#!/bin/sh
cd ./
export PROGRAM_NAME=spacesurvivor
export PROGRAM_VERSION=1.0
export PROGRAM_TITLE=SpaceSurvivor
export MAINCLASS=com.snapgames.demo.DemoGame
# pathes
export SRC=./src
export LIBS=./lib
export TARGET=./target
export BUILD=$TARGET/build
export CLASSES=$TARGET/classes
export RESOURCES=$SRC/main/resources
# prepare target
mkdir -p $CLASSES
# build manifest
echo "Build of program '$PROGRAM_NAME' ..."
echo "-----------"
echo "|_ 1. Create Manifest file '$TARGET/manifest.mf'"
echo 'Manifest-Version: $PROGRAM_NAME'>$TARGET/manifest.mf
echo "Main-Class: $MAINCLASS">>$TARGET/manifest.mf
echo "Implementation-Title: $PROGRAM_NAME">>$TARGET/manifest.mf
echo "Implementation-Version: build_N$GIT_COMIT_ID">>$TARGET/manifest.mf
echo "Implementation-Vendor: SnapGames">>$TARGET/manifest.mf
echo "Implementation-Author: fredericDOTdelormeATgmailDOTcom">>$TARGET/manifest.mf
echo " |_ done"
# Compile class files
rm -Rf $CLASSES/*
echo "|_ 2. compile sources from '$SRC' ..."
find $SRC -name '*.java' > $LIBS/sources.lst
javac @$LIBS/options.txt @$LIBS/sources.lst -cp $CLASSES
echo " done."
# Build JAR
echo "|_ 3. package jar file '$PROGRAM_NAME.jar'..."
jar -cfmv $TARGET/$PROGRAM_NAME.jar $TARGET/manifest.mf -C $CLASSES . -C $RESOURCES .
echo " |_ done."
# create runnable program
echo "|_ 4. create run file '$PROGRAM_NAME.run'..."
mkdir -p $BUILD
cat $LIBS/stub.sh $TARGET/$PROGRAM_NAME.jar > $BUILD/$PROGRAM_NAME.run
chmod +x $BUILD/$PROGRAM_NAME.run
echo " |_ done."
echo "-----------"
echo "... '$PROGRAM_NAME' is built".
-d target/classes
-g:source,lines,vars
-sourcepath src/main/java/;src/main/resources
-source 1.8
-target 1.8
-classpath tartget/classes
#!/bin/sh
# see https://github.com/maynooth/CS210/wiki/Convert-Java-Executable-to-Linux-Executable
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$@"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment