1 # Makefile base template
4 # - 1.0.0: add a version comment
5 # - 1.1.0: add help, sjar
7 # Required parameters (the commented out ones are supposed to change per project):
9 #MAIN = path to main java source to compile
10 #MORE = path to supplementary needed resources not linked from MAIN
11 #NAME = name of project (used for jar output file)
12 #PREFIX = usually /usr/local (where to install the program)
13 #TEST = path to main test source to compile
14 #JAR_FLAGS += a list of things to pack, each usually prefixed with "-C bin/"
15 #SJAR_FLAGS += a list of things to pack, each usually prefixed with "-C src/", for *-sources.jar files
16 #TEST_PARAMS = any parameter to pass to the test runnable when "test-run"
19 JAVAC_FLAGS += -encoding UTF-8 -d ./bin/ -cp ./src/
21 JAVA_FLAGS += -cp ./bin/
29 @echo "Usual options:"
30 @echo "=============="
31 @echo " make : to build the jar file"
32 @echo " make help : to get this help screen"
33 @echo " make libs : to update the libraries into src/"
34 @echo " make build : to update the binaries (not the jar)"
35 @echo " make test : to update the test binaries"
36 @echo " make build jar : to update the binaries and jar file"
37 @echo " make sjar : to create the sources jar file"
38 @echo " make clean : to clean the directory of intermediate files"
39 @echo " make mrpropre : to clean the directory of all outputs"
40 @echo " make run : to run the program from the binaries"
41 @echo " make run-test : to run the test program from the binaries"
42 @echo " make jrun : to run the program from the jar file"
43 @echo " make install : to install the application into $PREFIX"
45 .PHONY: all clean mrproper mrpropre build run jrun jar sjar resources test-resources install libs love
52 sjar: $(NAME)-sources.jar
55 @echo Compiling program...
57 @$(JAVAC) $(JAVAC_FLAGS) "src/$(MAIN).java"
58 @[ "$(MORE)" = "" ] || for sup in $(MORE); do \
60 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
64 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
65 @[ -e bin/$(MAIN).class ]
66 @echo Compiling test program...
67 @[ "$(TEST)" != "" ] || echo No test sources defined.
68 @[ "$(TEST)" = "" ] || for sup in $(TEST); do \
70 $(JAVAC) $(JAVAC_FLAGS) "src/$$sup.java" ; \
75 @echo Removing sources taken from libs...
76 @for lib in libs/*-sources.jar libs/*-sources.patch.jar; do \
77 if [ "$$lib" != 'libs/*-sources.jar' -a "$$lib" != 'libs/*-sources.patch.jar' ]; then \
79 jar tf "$$lib" | while read -r ln; do \
80 [ -f "src/$$ln" ] && rm "src/$$ln"; \
82 jar tf "$$lib" | tac | while read -r ln; do \
83 [ -d "src/$$ln" ] && rmdir "src/$$ln" 2>/dev/null || true; \
92 rm -f $(NAME)-sources.jar
93 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`.jar"
94 [ ! -e VERSION ] || rm -f "$(NAME)-`cat VERSION`-sources.jar"
100 @echo Copying resources into bin/...
101 @cd src && find . | grep -v '\.java$$' | grep -v '/test/' | while read -r ln; do \
102 if [ -f "$$ln" ]; then \
103 dir="`dirname "$$ln"`"; \
104 mkdir -p "../bin/$$dir" ; \
105 cp "$$ln" "../bin/$$ln" ; \
110 test-resources: resources
111 @echo Copying test resources into bin/...
112 @cd src && find . | grep -v '\.java$$' | grep '/test/' | while read -r ln; do \
113 if [ -f "$$ln" ]; then \
114 dir="`dirname "$$ln"`"; \
115 mkdir -p "../bin/$$dir" ; \
116 cp "$$ln" "../bin/$$ln" ; \
121 @[ -e bin/libs -o ! -d libs ] || echo Extracting sources from libs...
122 @[ -e bin/libs -o ! -d libs ] || (cd src && for lib in ../libs/*-sources.jar ../libs/*-sources.patch.jar; do \
123 if [ "$$lib" != '../libs/*-sources.jar' -a "$$lib" != '../libs/*-sources.patch.jar' ]; then \
128 @[ ! -d libs ] || touch bin/libs
130 $(NAME)-sources.jar: libs
131 @echo Making sources JAR file...
133 @[ "$(SJAR_FLAGS)" = "" ] || echo Creating $(NAME)-sources.jar...
134 @[ "$(SJAR_FLAGS)" = "" ] || $(JAR) cfm $(NAME)-sources.jar bin/manifest $(SJAR_FLAGS)
135 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`-sources.jar"...
136 @[ "$(SJAR_FLAGS)" = "" ] || [ ! -e VERSION ] || cp $(NAME)-sources.jar "$(NAME)-`cat VERSION`-sources.jar"
138 $(NAME).jar: resources
139 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
140 @[ -e bin/$(MAIN).class ]
141 @echo "Main-Class: `echo "$(MAIN)" | sed 's:/:.:g'`" > bin/manifest
142 @echo >> bin/manifest
143 $(JAR) cfm $(NAME).jar bin/manifest $(JAR_FLAGS)
144 @[ ! -e VERSION ] || echo Copying to "$(NAME)-`cat VERSION`.jar"...
145 @[ ! -e VERSION ] || cp $(NAME).jar "$(NAME)-`cat VERSION`.jar"
148 @[ -e bin/$(MAIN).class ] || echo You need to build the sources
149 @[ -e bin/$(MAIN).class ]
150 @echo Running "$(NAME)"...
151 $(JAVA) $(JAVA_FLAGS) $(MAIN)
154 @[ -e $(NAME).jar ] || echo You need to build the jar
156 @echo Running "$(NAME).jar"...
157 $(RJAR) $(RJAR_FLAGS) $(NAME).jar
160 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ] || echo You need to build the test sources
161 @[ "$(TEST)" = "" -o -e "bin/$(TEST).class" ]
162 @echo Running tests for "$(NAME)"...
163 @[ "$(TEST)" != "" ] || echo No test sources defined.
164 [ "$(TEST)" = "" ] || ( clear ; $(JAVA) $(JAVA_FLAGS) $(TEST) $(TEST_PARAMS) )
167 @[ -e $(NAME).jar ] || echo You need to build the jar
169 mkdir -p "$(PREFIX)/lib" "$(PREFIX)/bin"
170 cp $(NAME).jar "$(PREFIX)/lib/"
171 echo "#!/bin/sh" > "$(PREFIX)/bin/$(NAME)"
172 echo "$(RJAR) $(RJAR_FLAGS) \"$(PREFIX)/lib/$(NAME).jar\" \"\$$@\"" >> "$(PREFIX)/bin/$(NAME)"
173 chmod a+rx "$(PREFIX)/bin/$(NAME)"