1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#!/usr/bin/make -f
# Install paths
DESTDIR ?=
MAVEN_LOCAL_REPO ?= ~/.m2
# Utilities
XMLSTARLET = xml
FAIL = exit 1
FIND = find
JAVAC = javac
INSTALL = install
MKDIRS = mkdir -p
RM = rm -f
JAR = jar
CP = cp
# Detect information from POMs
artifactId := $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:artifactId -n pom.xml)
version := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:version -n -c /_:project/_:parent/_:version pom.xml) $(version))
groupId := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:groupId -n -c /_:project/_:parent/_:groupId pom.xml))
sourceDirectory := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:build/_:sourceDirectory -n pom.xml) $(sourceDirectory) src/main/java)
subdirs = $(patsubst %/pom.xml,%,$(wildcard */pom.xml))
targets = pom $(if $(wildcard src/main/ $(sourceDirectory)),jar)
$(if $(extra_makefiles),$(eval include $(extra_makefiles)))
################################################################################
dep_dir = $1 $(shell $(FIND) $1 2>/dev/null)
dep_optdir = $(shell $(FIND) $1 2>/dev/null)
# all
all: PHONY \
$(addprefix target/$(artifactId)-$(version).,$(targets)) \
$(addsuffix /all,$(subdirs))
target/$(artifactId)-$(version).pom: pom.xml
$(INSTALL) -Dm644 $< $@
target/$(artifactId)-$(version).jar: %.jar: %
$(JAR) -cf $@ -C $< .
target/$(artifactId)-$(version): %: \
target/META-INF/maven/$(groupId)/$(artifactId)/pom.properties \
target/META-INF/maven/$(groupId)/$(artifactId)/pom.xml \
$(call dep_dir,target/classes) \
$(call dep_optdir,src/main/resources)
$(RM) -r $@
$(MKDIRS) $@
$(CP) -r target/META-INF $(wildcard target/classes/* src/main/resources/*) $@
target/META-INF/maven/$(groupId)/$(artifactId)/pom.xml: pom.xml
install -Dm644 $< $@
# That is almost the default date format for locale=C, but the DOM is
# 0-padded instead of space-padded.
target/META-INF/maven/$(groupId)/$(artifactId)/pom.properties: pom.xml
$(MKDIRS) $(@D)
printf '%s\n' \
'#Generated by Make' \
"#$$(LC_ALL=C date '+%a %b %d %H:%M:%S %Z %Y')" \
'version=$(version)' \
'groupId=$(groupId)' \
'artifactId=$(artifactId)' \
> $@
target/classes: $(call dep_optdir,$(sourceDirectory))
$(MKDIRS) $@
@echo $(JAVAC) -d $@ ...; $(if $^,$(JAVAC) -d $@ $(filter %.java,$^) || { $(RM) -r $@; $(FAIL); })
# install
install: PHONY \
$(addprefix $(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(groupId))/$(artifactId)/$(version)/$(artifactId)-$(version).,$(targets)) \
$(addsuffix /install,$(subdirs))
$(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(groupId))/$(artifactId)/$(version)/%: target/%
$(INSTALL) -Dm644 $< $@
# clean
clean: PHONY $(addsuffix /clean,$(subdirs))
rm -rf target
# recurse
deps2jars = $(wildcard $(patsubst %/all,%/target/*.jar,$(filter $(addsuffix /all,$(subdirs)),$1)))
deps2classpath = $(shell echo $(abspath $(call deps2jars,$1)) $(CLASSPATH) | tr ' ' :)
define recurse-rule
$1/%: PHONY $(addsuffix /all,$($1_deps))
CLASSPATH='$$(call deps2classpath,$$^)' sourceDirectory='$$(sourceDirectory)' version='$$(version)' $$(MAKE) -C '$1' -f '$$(abspath $$(firstword $$(MAKEFILE_LIST)))' '$$*'
endef
$(foreach subdir,$(subdirs),$(eval $(call recurse-rule,$(subdir))))
# boilerplate
.PHONY: PHONY FORCE
.DELETE_ON_ERROR:
|