For college, I have to use Java. However, I do not want to use an IDE. I like vim.
So I'm trying to create a Makefile to build my java projects. My requirements are :
No hardcoding of classes. The Makefile shall find all
.javafiles in thesrcdirectory and any subdirectories, and compile the resulting classes to theclassesdirectory.Multiple
mains: Files containingmainfunctions shall have an executable.jarfile built with a correspondingMANIFEST.MF. Listing them explicitly is acceptable, although I'm pretty sure something could be done withgrephere to find them automatically (but I suck atgrep)A source jar for the whole project shall be created, containing unaltered
.javafiles and noMANIFEST.MFor other extraneous files.
My best attempt so far (currently broken) follows :
include gmsl
targets := Client Server
# Folders
binDir := ./bin
clsDir := ./classes
srcDir := ./src
testDir := ./test
# Flags
version := 11
# Files
mainSrc := $(targets:%=$(srcDir)/tp1/%.java)
src := $(shell find $(srcDir) -name *\.java -type f)
cls := $(src:%.java=%.class)
# Rules
# Base
all : $(targets)
again : clean all
classes : $(cls)
# For debug
info:
$(info mainSrc : $(mainSrc))
$(info srcJar : $(srcJar))
$(info exeJar : $(exeJar))
$(info src : $(src))
$(info cls : $(cls))
# Executables
$(targets): $(cls)
$(shell cd $(srcDir))
$(eval mainCls := $(shell find . -name *$@\.java -type f) )
$(info mainCls = $(mainCls))
jar --verbose --create --file=$(call lc,$@)-exe.jar --main-class=tp1/$@ -C $(clsDir) .
# Source
$(srcJar) : $(cls)
jar --verbose --create --file=$@ --no-manifest -C $(srcDir) .
# Classes
$(cls):
javac --release $(version) --source-path $(srcDir) -d $(clsDir) $(mainSrc)
# Cleanup
clean:
-rm $(targets)
-cd $(clsDir) && rm -rf ./*
Short Answer: It's OK to prefer non-IDE solution. However, limiting yourself to "make" as your ONLY build tool will mostly like lead to one of two outcome for any non-trivial project:
What's the alternative ? use a tool that is "understand" Java, and has proper built in actions for building Java artifacts:
Technically, your can build a Makefile that will execute ant/build for you.