Last active
March 12, 2021 14:23
-
-
Save grunka/76f3d39915db43ad3cd2b44358c36c00 to your computer and use it in GitHub Desktop.
A small makefile for building small Java projects and making runnable jars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: all clean run jar | |
JAR_NAME=Main.jar | |
MAIN_CLASS=Main | |
BIN_PATH=bin | |
LIB_PATH=lib | |
NOTHING= | |
SPACE=$(NOTHING) $(NOTHING) | |
LIBS=$(wildcard $(LIB_PATH)/*.jar) | |
CLASSPATH=$(BIN_PATH):$(subst $(SPACE),:,$(LIBS)) | |
SOURCES=$(wildcard *.java) | |
CLASSES=$(addprefix $(BIN_PATH)/,$(SOURCES:.java=.class)) | |
all: $(CLASSES) | |
$(BIN_PATH)/%.class: %.java | |
javac -cp $(CLASSPATH) -d $(BIN_PATH) $< | |
run: all | |
java -cp $(CLASSPATH) $(MAIN_CLASS) | |
clean: | |
rm -rf $(BIN_PATH) | |
rm -f $(JAR_NAME) | |
jar: all $(JAR_NAME) | |
$(JAR_NAME): $(LIBS) | |
$(eval JAR_TEMP=$(shell mktemp -d)) | |
$(foreach lib,$(LIBS),unzip -o $(lib) -d $(JAR_TEMP);) | |
rm -rf $(JAR_TEMP)/META-INF | |
cp -R $(BIN_PATH)/* $(JAR_TEMP)/ | |
jar --create --file=$(JAR_NAME) --main-class=$(MAIN_CLASS) -C $(JAR_TEMP) . | |
rm -rf $(JAR_TEMP) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't really recommend using this but I've needed it a few times 😅
Create a Main.java file with a
public static void main(String... args) {
in it in the same directory, create alib
directory and put any needed library jars in there.