Over a million developers have joined DZone.

Generate UML Diagrams into Javadoc in NetBeans Projects

DZone's Guide to

Generate UML Diagrams into Javadoc in NetBeans Projects

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

One incredibly useful article recently on Javalobby is called Reverse-engineer Source Code into UML Diagrams, by Meera Subbarao. Sandip wrote a similar article, on the same GraphViz functionality, in one of his last Sun blogs, Trunk NetBeans cluster dependency graph generated using dot. The latter is particularly interesting for NetBeans Platform users, since (if you get everything working right) you should be able to generate dependency graphs between modules in your NetBeans Platform application. But, let's leave that for another article.

Here I simply want to outline what I needed to do to apply Meera's instructions to a standard NetBeans Java project. First of all, here is the target you need to put into "build.xml". Take note of the fact that the target is called "-post-compile", which means that it will be run automatically whenever a build is completed. Therefore, you'll not need to do anything other than invoke the project's build command and then you'll end up with UML diagrams in your Javadoc. So here's the target:

<target name="-post-compile" description="Generates UML-Javadoc">
    <mkdir dir="${dist.javadoc.dir}"/>
     <javadoc source="${javac.source}" packagenames="com.*"
            <path path="${javac.classpath}"/>
        <fileset dir="${src.dir}" excludes="${excludes}"
            <filename name="**/*.java"/>
        <doclet name="org.umlgraph.doclet.UmlGraphDoc"
            <param name="-attributes" />
            <param name="-operations" />
            <param name="-qualify" />
            <param name="-types" />
            <param name="-visibility" />

OK, now some other comments on the target above. Firstly, it is based on the standard NetBeans Javadoc target, supplemented with some extra tags from Meera's article. Note especially the following:

  • Line 2: "dist.javadoc.dir" and all other properties, are all set in the project.properties file, but there's nothing you need to do yourself, since all properties in the target above reuse the predefined targets in the project.properties file.
  • Line 3: The "packagenames" attribute sets the package names to be included, so here I've set everything from "com" onwards (e.g., "com.pkg.subpkg.etc" to be included).
  • Line 14: The "file.reference.UmlGraph.jar" property is generated when you add the UmlGraph.jar (created according to Meera's article) to the application's Libraries node. You could change this to something different, that's up to you, so long as it points to UmlGraph.jar

And that's all I needed. I'm on Ubuntu, so all I needed to do was this to get the dot program to work:

sudo apt-get install graphviz


That's all. Then build the project and you'll find DOT files and PNG files added to the "dist" folder:

Open one of the HTML files and there you'll see magically your UML diagrams:

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}