Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML
Tags: uml, java, intellij, plantuml
Published: 2024-03-02 13:52:49

---
title: Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML
published: true
description: 
tags: uml, java, intellij, plantuml
# Use a ratio of 100:42 for best results.
# published_at: 2024-03-02 13:37 +0000
---

## Introduction

[UML Class diagrams](https://www.visual-paradigm.com/guide/uml-unified-modeling-language/what-is-class-diagram/) can be useful for understanding complex code or for designing major new features. However, hand-drawing those diagrams may require too much time and effort. To reduce those costs, the Ultimate edition of IntelliJ IDEA has the bundled ["Diagrams" plugin](https://www.jetbrains.com/help/idea/diagrams.html) which can generate UML Class diagrams for Java and Kotlin code.

For the examples used below, the source code of the Apache Kafka project is being used (see [github.com](https://github.com/apache/kafka)).

## Create diagram

Given that the project is already set up in IntelliJ, select the files to be included in the diagram in the Project window. Then right-click the files and select "Diagrams > Show diagram" from the context menu.

![show-diagram.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pxn5bn1q9re66i5zlnd.png)

More classes can be added later on to the diagram by pressing "Space" from within the diagram and then typing in the class name. Or by dragging and dropping the file file into the diagram.

## Configure diagram

To include the class relationships, activate the "Show Dependencies" options. Sometimes it may also be useful to activate the options next left to it.

![include-rel.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d1o1g4nvo0dpfwl4t5m.png)

## Export

The diagram can be exported as a PlantUML file with the help of the "Export Diagram" icon and the "Export to file > PlantUML" context menu.

![export-to-file.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwbb3sdvv0re7dagd0by.png)

## Normalize generated code

PlantUML offers plenty of configuration options. The options that were used by the Diagrams plugin may be changed by running a custom [Python](https://www.python.org) script which is performing string-replacements in the generated file.

```sh
python3 normalize.py example.puml
```

Here is how the `normalize.py` script may look like:

```py
#!/usr/bin/env python3

#
# normalize.py by janux_de is marked with CC0 1.0 
#
# https://creativecommons.org/publicdomain/zero/1.0/
#

import argparse
import re

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('file', nargs=1)
    args = parser.parse_args()
    path = args.file[0]

    f = open(path, "r")
    original_lines = f.readlines()
    f.close()

    updated_lines = []
    for line in original_lines:
        # Replace theme configuration with "hiding empty members" flag
        line = line.replace("!theme plain", "hide empty members")

        # Remove cardinality indicators
        line = re.sub(r'"\d+"', "", line)

        # Remove variable names
        line = re.sub(r'-> ".*?"', "-> ", line)

        # Remove text on edges
        line = re.sub(r' : .*', "", line)
        
        # Simplify dashed lines
        line = re.sub(r'-\[.*?dashed\]-', "..", line)
        
        # Simplify plain lines
        line = re.sub(r'-\[.*?plain\]-', "--", line)

        # Replace composition symbol with directed association
        line = line.replace("*-->", "-->")

        # Use a more intuitive flavor for inheritance symbols
        line = line.replace("--^", "--|>")
        line = line.replace("..^", "..|>")

        updated_lines.append(line)

    f = open(path, "w")
    f.writelines(updated_lines)
    f.close()
```

## Further processing

The diagram may then be manually edited, e.g. with the help of the [PlantUML plugin for VS Code](https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml).

![vscode-example.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9ohnn84hjgz964tfztr.png)

Finally, the PlantUML file may be compiled to a PNG with the help of this [Docker](https://www.docker.com/) command:

```sh
cat example.puml | docker run --rm -i \
    dstockhammer/plantuml:1.2023.13 \
    -pipe -tpng > example.png
```

(see [github.com](https://github.com/dstockhammer/docker-plantuml) for the source code of the Docker image from the snippet above)

![example.png](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvs00mw1kjnpjdhlqscc.png)

## References

- [UML class diagrams | jetbrains.com](https://www.jetbrains.com/help/idea/class-diagram.html)
- [Class diagram | plantuml.com](https://plantuml.com/en/class-diagram)
- [How to create UML Class Diagrams with IntelliJ Ultimate macOS | Teacher Marcos | youtube.com](https://www.youtube.com/watch?v=66ArYQUFLdM)