Over a million developers have joined DZone.
Platinum Partner

Generate UML Diagrams into Javadoc in NetBeans Projects

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

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:

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}