Skip to content

Instantly share code, notes, and snippets.

@hjst
Created June 6, 2017 18:58
Show Gist options
  • Save hjst/4f2f2c2ca9bd550e50c7f06cb17775b2 to your computer and use it in GitHub Desktop.
Save hjst/4f2f2c2ca9bd550e50c7f06cb17775b2 to your computer and use it in GitHub Desktop.
Makefile for use with PlantUML diagrams
PLANTUML_JAR_URL = https://sourceforge.net/projects/plantuml/files/plantuml.jar/download
DIAGRAMS_SRC := $(wildcard diagrams/*.plantuml)
DIAGRAMS_PNG := $(addsuffix .png, $(basename $(DIAGRAMS_SRC)))
DIAGRAMS_SVG := $(addsuffix .svg, $(basename $(DIAGRAMS_SRC)))
# Default target first; build PNGs, probably what we want most of the time
png: plantuml.jar $(DIAGRAMS_PNG)
# SVG are nice-to-have but don't need to build by default
svg: plantuml.jar $(DIAGRAMS_SVG)
# clean up compiled files
clean:
rm -f plantuml.jar $(DIAGRAMS_PNG) $(DIAGRAMS_SVG)
# If the JAR file isn't already present, download it
plantuml.jar:
curl -sSfL $(PLANTUML_JAR_URL) -o plantuml.jar
# Each PNG output depends on its corresponding .plantuml file
diagrams/%.png: diagrams/%.plantuml
java -jar plantuml.jar -tpng $^
# Each SVG output depends on its corresponding .plantuml file
diagrams/%.svg: diagrams/%.plantuml
java -jar plantuml.jar -tsvg $^
# Quirk of GNU Make: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
.PHONY: png svg clean
@hjst
Copy link
Author

hjst commented Jun 6, 2017

Assumptions

  • GNU make installed — (dnf|apt|yum) install make or install the Apple developer tools on Mac
  • Graphviz installed — (dnf|apt|yum) install graphviz or brew install graphviz on Mac
  • A working Java install — varies too much by platform, dnf install java-1.8.0-openjdk on Fedora
  • One diagram per file; files have the .plantuml extension and live in the diagrams/ directory relative to this Makefile

Example usage

$ tree docs/
docs/
├── diagrams
│   ├── components.plantuml
│   ├── seq-request-a.plantuml
│   ├── seq-session-create.plantuml
├── Makefile
└── Intro.md

$ cd docs
$ make
    ...make downloads the .jar file
    ...make compiles PNG files by default; use `make svg` for SVG files

$ tree
.
├── diagrams
│   ├── components.plantuml
│   ├── components.png
│   ├── seq-request-a.plantuml
│   ├── seq-request-a.png
│   ├── seq-session-create.plantuml
│   ├── seq-session-create.png
├── Makefile
└── Intro.md

Note: you'll probably want to add plantuml.jar to your .gitignore file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment