/java

Structurizr for Java

Primary LanguageJavaApache License 2.0Apache-2.0

Structurizr

Structurizr for Java

This GitHub repository is a Java library to create software architecture models that are compatible with Structurizr, a SaaS to create web-based software architecture diagrams. It's an implementation of the "C4 software architecture model", as described in Simon Brown's FREE The Art of Visualising Software Architecture book. In a nutshell:

  1. Create a software architecture model using Java code, either manually or by extracting information from an existing codebase.
  2. Upload the model (as a JSON document) to Structurizr using the web API.
  3. Visualise and share the resulting software architecture diagrams (example).

An overview of Structurizr

Table of contents

  1. Building from source
  2. Binaries
  3. Getting started
  4. API Client
  5. Styling elements
  6. Styling relationships
  7. Hiding relationships
  8. Extracting components from your codebase
  9. The Spring PetClinic example
  10. Client-side encryption
  11. Graphviz and DOT

Building

To build "Structurizr for Java" from the sources (you'll need Java 8)...

git clone https://github.com/structurizr/java.git
cd java
./gradlew build

Note: For now, if you want to build the structurizr-dot library you'll also need to clone and install dot-diagram into your local Maven repository:

git clone https://github.com/cyriux/dot-diagram.git
cd dot-diagram/dot-diagram/
mvn install -DskipTests=true

The -DskipTests=true is needed as the unit test are failing in the dot-diagram project.

If necessary, after building, you can install "Structurizr for Java" into your local Maven repo using:

./gradlew publishToMavenLocal

Binaries

The "Structurizr for Java" binaries are hosted on Bintray and the JCenter repository. The dependencies for use with Maven, Ivy, Gradle, etc are as follows.

Name Description
com.structurizr:structurizr-core:0.7.0 The core library that can used to create and upload models to Structurizr.
com.structurizr:structurizr-spring:0.7.0 The Spring integration to extract classes annotated @Controller, @Service and @Repository for identification as components.
com.structurizr:structurizr-annotations:0.7.0 Annotations to add software architecture hints into your own code.

Getting started

Here is a quick overview of how to get started with Structurizr for Java so that you can create a software architecture model as code. You can find the code here. For more examples, please see structurizr-examples.

If you want a quick start, simply clone the Structurizr for Java starter project.

1. Dependencies

The Structurizr for Java binaries are hosted on Bintray and the JCenter repository. The dependencies for use with Maven, Ivy, Gradle, etc are as follows.

Name Description
com.structurizr:structurizr-core:0.7.0 The core library that can used to create models and upload models to Structurizr.
com.structurizr:structurizr-client:0.7.0 The structurizr.com API client for Java for uploading models.

Please note that you will need to add "http://jcenter.bintray.com" as an additional repository to your build configuration.

2. Create a model

The first step is to create a workspace in which the software architecture model will reside.

Workspace workspace = new Workspace("My model", "This is a model of my software system.");
Model model = workspace.getModel();

Now let's add some elements to the model.

Person user = model.addPerson("User", "A user of my software system.");
SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System", "My software system.");
user.uses(softwareSystem, "Uses");

3. Create some views

With the model created, we need to create some views with which to visualise it.

ViewSet viewSet = workspace.getViews();
SystemContextView contextView = viewSet.createContextView(softwareSystem);
contextView.addAllSoftwareSystems();
contextView.addAllPeople();

4. Add some colour

Elements and relationships can be styled by specifying colours, sizes and shapes.

Styles styles = viewSet.getConfiguration().getStyles();
styles.addElementStyle(Tags.SOFTWARE_SYSTEM).background("#a4b7c9").color("#000000");
styles.addElementStyle(Tags.PERSON).background("#728da5").color("#ffffff");

5. Upload to Structurizr

structurizr.com provides an API to get and put workspaces directly from/to your Structurizr account as follows.

StructurizrClient structurizrClient = new StructurizrClient("key", "secret");
structurizrClient.putWorkspace(1234, workspace);

In order to upload your model to Structurizr using the web API, you'll need to sign up to get your own API key and secret.

The result is a diagram like this (once you've dragged the boxes around).

Getting Started with Structurizr for Java

Retaining diagram layout information

Once you have uploaded your model to Structurizr and organised the boxes on the diagrams, you'll probably want to retain the diagram layout next time you upload the model. To do this, you can use the mergeWorkspace helper method on the StructurizrClient.

structurizrClient.mergeWorkspace(1234, workspace);

This will get the current version of the workspace via the API, merge the diagram layout information, and then upload the new version via the API. See API Client for more details.