Generate Javadoc with UML Diagrams

It is very useful to have UML Diagrams integrated into your javadoc – they give you a neat visual overview of the contents of you sourcecode. I want to show you how  to integrate this into an automated build. I implemented this with an ant target and my IDE Netbeans.

Add this to your build.xml in the root of your project:

<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="Aufgabe2" default="default" basedir=".">
    <description>Builds, tests, and runs the project Aufgabe2.</description>
    <property file="nbproject/project.properties"/>
    <import file="nbproject/build-impl.xml"/>
    <target name="javadocs" depends="-post-compile" description="generates javadoc and also UML Diagram">
        <echo message="Generating Javadoc and UML Diagram to: ${dist.javadoc.dir}"/>
        <mkdir dir="${dist.javadoc.dir}"/>
        <javadoc sourcepath="${src.dir}" packagenames="com.bla.*" destdir="${dist.javadoc.dir}" private="true">
            <doclet name="org.umlgraph.doclet.UmlGraphDoc"
                                   path="lib/UmlGraph.jar">
                <param name="-windowtitle" />
                <param name="-attributes" />
                <param name="-operations" />
                <param name="-qualify" />
                <param name="-types" />
                <param name="-visibility" />
                <param name="-inferdep" />
                <param name="-inferrel" />
                <param name="-all" />
            </doclet>
        </javadoc>
        <apply executable="dot" dest="${dist.javadoc.dir}" parallel="false">
            <arg value="-Tpng"/>
            <arg value="-o"/>
            <targetfile/>
            <srcfile/>
            <fileset dir="${dist.javadoc.dir}" includes="*.dot"/>
            <mapper type="glob" from="*.dot" to="*.png"/>
        </apply>
        <nbbrowse file="${dist.javadoc.dir}/index.html"/>
    </target>
</project>

You will need Graphviz and UMLGraph.

I had to execute sudo ln -s /usr/local/bin/dot /usr/bin/dot on my Mac OSX to make dot work. Since it was not recognized in my PATH and quitted with the error:

Cannot run program “dot”: error=2, No such file or directory ensure that dot is in your path and that its path does not contain spaces

Arguments

You can pass several arguments to UML Grap. You can find a full list on the website.

Generate

You can start build like this:

Run target
If everything works fine you will see:

Run target output

The javadoc is opened in your browser.

Shortcut

For convenience you can add a shortcut to build your javadoc and UML diagrams.

Bildschirmfoto 2015-03-25 um 22.23.48

Create Shortcut

Create Shortcut Menu Select Menu

Run "Generate Javadoc and UML Diagram"

Result

Result

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: