Uml4j is an unopinionated, highly customizable UML diagramming tool. It reads your Java code and generates text-based UML class diagrams. You declare UML class diagrams by adding specific annotations to your codebase. Uml4j can generate UML class diagrams in the following formats:
| Format | URL | About |
|---|---|---|
| Graphviz | https://www.graphviz.org | The traditional tool for representing a graph of nodes and edges with a simple textual language. |
| PlantUml | https://plantuml.com/ | UML specific tool supporting different types of UML diagrams. |
| Mermaid | https://mermaid.js.org/ | General-purpose diagramming tool with support for UML class diagrams. Integrates nicely with GitHub. |
| D2 | https://d2lang.com/ | General-purpose diagramming tool supporting UML class diagrams with different layout engines. |
You can view the diagrams with your favorite IDE plugin or save them as images.
Simply add an annotation like the following to a package-info.java file in your codebase, referencing your project's classes. Uml4j will generate the diagram in your preferred format.
@UmlDiagram(name="orders",
types = {
Order.class,
OrderLine.class,
Product.class,
Customer.class,
Contact.class
},
format = UmlFormat.MERMAID,
options = @UmlOptions(fields = true, methods = true,
accessors = false, constructors = false, dependencies = true))Below is an example of the generated diagram:
classDiagram
class org_uml4j_model_examples_Customer ["Customer"] {
-id : int
-name : String
-email : String
-address : Address
+changeAddress(newAddress : Address)
}
link org_uml4j_model_examples_Customer "https://github.com/diamantidakos/uml4j/blob/main/uml4j-core/src/test/java/org/uml4j/model/examples/Customer.java"
class org_uml4j_model_examples_OrderLine ["OrderLine"] {
-order : Order
-product : Product
-quantity : int
}
link org_uml4j_model_examples_OrderLine "https://github.com/diamantidakos/uml4j/blob/main/uml4j-core/src/test/java/org/uml4j/model/examples/OrderLine.java"
class org_uml4j_model_examples_Product ["Product"] {
-id : int
-name : String
-price : int
~changePrice(newPrice : int)
}
link org_uml4j_model_examples_Product "https://github.com/diamantidakos/uml4j/blob/main/uml4j-core/src/test/java/org/uml4j/model/examples/Product.java"
class org_uml4j_model_examples_Order ["Order"] {
-id : int
-customer : Customer
-orderLines : Set~OrderLine~
+totalPice() int
+deliveryAddress() Address
+addProduct(product : Product, quantity : int)
}
link org_uml4j_model_examples_Order "https://github.com/diamantidakos/uml4j/blob/main/uml4j-core/src/test/java/org/uml4j/model/examples/Order.java"
class org_uml4j_model_examples_Contact ["Contact"] {
<<interface>>
+getEmail() String
}
link org_uml4j_model_examples_Contact "https://github.com/diamantidakos/uml4j/blob/main/uml4j-core/src/test/java/org/uml4j/model/examples/Contact.java"
org_uml4j_model_examples_Contact <|.. org_uml4j_model_examples_Customer
org_uml4j_model_examples_OrderLine --> "1" org_uml4j_model_examples_Product
org_uml4j_model_examples_OrderLine "*" <--> "1" org_uml4j_model_examples_Order
org_uml4j_model_examples_Order --> "1" org_uml4j_model_examples_Customer
Uml4j is inspired by UMLGraph, a tool that pioneered the text-based approach to UML diagramming.
Uml4j requires JDK 24 or newer and is heavily based on the Java Class file API. You need Maven 3.9.9 or newer to build Uml4j. Run mvn install in the root of the Uml4j project. Maven will install the uml4j-annotations and uml4j-core libraries, as well as the uml4j-maven-plugin, into your local Maven repository. It will also build the uml4jrun jar file in the /uml4j-cli/target directory.
See the user guide for more information.