Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Created May 7, 2025 16:17
Show Gist options
  • Save mcgivrer/3fabaa2cd1a1e1fc934c7b5da6a6e8ca to your computer and use it in GitHub Desktop.
Save mcgivrer/3fabaa2cd1a1e1fc934c7b5da6a6e8ca to your computer and use it in GitHub Desktop.
java micro build with libs
#!/bin/bash
project_name=jdemo
project_version=1.0.0
main_class=App
JARS=
vendor_name=DEMO
author_name="Frédéric Delorme"
#----
GIT_COMMIT_ID=$(git rev-parse HEAD)
JAVA_BUILD=$(java --version | head -1 | cut -f2 -d' ')
## Prepare Build
rm -vrf target/
find src/main/java src/main/resources -name "*.java"
mkdir -vp target/{classes,build/libs}
## Compile sources
javac -cp libs/ $(find src/main/java src/main/resources -name "*.java") -d target/classes
## create MANIFEST file
echo """
Manifest-Version: ${project_name}
Main-Class: ${main_class}
Class-Path: ${JARS}
Created-By: ${JAVA_BUILD}
Implementation-Title: ${project_name}
Implementation-Version: ${project_version}-build_${GIT_COMMIT_ID:0:12}
Implementation-Vendor: ${vendor_name}
Implementation-Author: ${author_name}
""" >>target/MANIFEST.MF
## create JAR
jar cvfe target/build/${project_name}-${main_class}-${project_version}.jar ${main_class} -C target/classes . -C src/main/resources .
## prepare run
cp -r libs/ target/build/libs
echo "project Compiled, now run it !"
java -cp libs/ -jar target/build/${project_name}-${main_class}-${project_version}.jar
@mcgivrer
Copy link
Author

mcgivrer commented May 7, 2025

Java project inspired from maven structure :

[Project]
|_ libs
|_ src
|   |_ main
|   |   |_ java
|   |   |_ resources
|   |_ test
|      |_ java
|      |_ resources
|_ target
|  |_ classes
|  |_ build
|_ build

build is the bash script.

You need to define the following variables in the script:

  1. Defining the jar output :

    • project_name=jdemo
    • project_version=1.0.0
    • main_class=App
  2. List jar dependencies for build and run :

    • JARS= [space separated list of JAR in libs/]
  3. Define entries for MANIFEST:

    • vendor_name=[a vendor name]
    • author_name=[an author name]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment