summaryrefslogtreecommitdiff
path: root/makefiles/mvn-simple.mk
blob: 082e6f2b3a0e74daa5bdf1ab3a8c978219a38236 (plain)
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
#!/usr/bin/make -f

# Install paths
DESTDIR ?=
MAVEN_LOCAL_REPO ?= ~/.m2

# Utilities
XMLSTARLET = xml
FIND = find
JAVAC = javac
INSTALL = install
MKDIRS = mkdir -p
RM = rm -f
JAR = jar

# Detect information from POMs
artifact    :=             $(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))
group       := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:groupId -n -c /_:project/_:parent/_:groupId pom.xml))
srcdir      := $(firstword $(shell $(XMLSTARLET) sel -T -t -c /_:project/_:build/_:sourceDirectory -n                  pom.xml) $(srcdir) src/main/java)
mvn_subdirs := $(patsubst %/pom.xml,%,$(wildcard */pom.xml))
subdirs     := $(if $(subdirs),$(filter $(mvn_subdirs),$(subdirs)),$(mvn_subdirs))
targets     := pom $(if $(wildcard src/main/ $(srcdir)),jar)

################################################################################

dep_dir = $1 $(shell $(FIND) $1 2>/dev/null)
dep_optdir = $(shell $(FIND) $1 2>/dev/null)

# all

all: PHONY \
    $(addprefix target/$(artifact)-$(version).,$(targets)) \
    $(addsuffix /all,$(subdirs))

target/$(artifact)-$(version).pom: pom.xml
	$(INSTALL) -Dm644 $< $@

target/$(artifact)-$(version).jar: \
    target/META-INF/maven/$(group)/$(artifact)/pom.properties \
    target/META-INF/maven/$(group)/$(artifact)/pom.xml \
    $(call dep_dir,target/classes) \
    $(call dep_optdir,src/main/resources)
	$(MKDIRS) $(@D)
	$(JAR) -cf $@ -C target META-INF -C target/classes . $(if $(wildcard src/main/resources),-C src/main/resources .)

target/META-INF/maven/$(group)/$(artifact)/pom.xml: pom.xml
	install -Dm644 $< $@

target/META-INF/maven/$(group)/$(artifact)/pom.properties: pom.xml
	$(MKDIRS) $(@D)
	printf '%s\n' \
		'#Generated by Make' \
		"#$$(LC_ALL=C date)" \
		'version=$(version)' \
		'groupId=$(group)' \
		'artifactId=$(artifact)' \
		> $@

target/classes: $(call dep_optdir,$(srcdir))
	$(MKDIRS) $@
	$(if $^,$(FIND) $(srcdir) -name '*.java' -exec $(JAVAC) -d $@ {} + || { $(RM) -r $@; exit 1; })

# install

install: PHONY \
    $(addprefix $(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(group))/$(artifact)/$(version)/$(artifact)-$(version).,$(targets)) \
    $(addsuffix /install,$(subdirs))

$(DESTDIR)$(MAVEN_LOCAL_REPO)/$(subst .,/,$(group))/$(artifact)/$(version)/%: target/%
	$(INSTALL) -Dm644 $< $@

# clean

clean: PHONY $(addsuffix /clean,$(subdirs))
	rm -rf target

# recurse

$(addsuffix /%,$(subdirs)): PHONY
	srcdir='$(srcdir)' version='$(version)' $(MAKE) -C $(@D) -f '$(abspath $(firstword $(MAKEFILE_LIST)))' '$(@F)'

# boilerplate

.PHONY: PHONY FORCE
.DELETE_ON_ERROR: