Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Generate UML Diagrams into Javadoc in NetBeans Projects

DZone's Guide to

Generate UML Diagrams into Javadoc in NetBeans Projects

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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.*"
              destdir="${dist.javadoc.dir}"  
              private="true">  
        <classpath>
            <path path="${javac.classpath}"/>
        </classpath>
        <fileset dir="${src.dir}" excludes="${excludes}"
              includes="${includes}">
            <filename name="**/*.java"/>
        </fileset>
        <doclet name="org.umlgraph.doclet.UmlGraphDoc"
              path="${file.reference.UmlGraph.jar}">
            <param name="-attributes" />
            <param name="-operations" />
            <param name="-qualify" />
            <param name="-types" />
            <param name="-visibility" />
        </doclet>
    </javadoc>
</target>

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:

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}