Generate UML Diagrams into Javadoc in NetBeans Projects

Tags:

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:

AttachmentSize
fig-1.png66.26 KB
fig-2.png46.37 KB
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Meera Subbarao replied on Wed, 2008/08/27 - 7:13am

Nice article, Geertjan. I sure will try this.

Geertjan Wielenga replied on Wed, 2008/08/27 - 7:58am

Well, it's all thanks to you, Meera!

Varun Nischal replied on Wed, 2008/08/27 - 8:20am

Amazing :)

Aliseleo replied on Tue, 2009/03/31 - 4:44am

Any mainstream IDE like Eclipse or IntelliJ IDEA have Javadoc assistant. You can either generate Javadoc for existing class or package, or you can have it assist you as you create the code.There is also a javadoc executable utility that is provided with JDK that can generate javadoc using command line.Hiding code is kind of pain, because javadoc has to be a part of original source .java file. You can give .class file to the person who documents, but that won't do any good since you won't be able to get the results of that person's work into the source code unless you yourself manually copy it. Plus, .class files can be easily decompiled....thanks...by cheap van contract hire park!!!!

raai replied on Thu, 2009/05/28 - 1:31pm

I have this error no source file and no package have been specified Line:

<javadoc source="${javac.source}" packagenames="com.*"

             destdir="${dist.javadoc.dir}"               private="true"> 

 

Why?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.