Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Geertjan is a DZone Zone Leader and has posted 421 posts at DZone. You can read more from them at their website. View Full User Profile

Generate UML Diagrams into Javadoc in NetBeans Projects

08.27.2008
Email
Views: 14718
  • submit to reddit

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
Tags:
Published at DZone with permission of its author, Geertjan Wielenga.

(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 :)

IRA RA 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?

Kt Parikh replied on Wed, 2010/02/17 - 7:32am

Please help me with this: I am also getting the error No source files and no packages have been specified at the last line(private=true). It is not able to find the source packages. I am not able to understand why this is happening, as this is working with another project of similar structure, but throwing this error for other projects. I have added the jar file and cross checked almost everything. Please help with this. Thanks and Regards, KT

Kt Parikh replied on Wed, 2010/02/17 - 7:33am in response to: raai

did u get the reason why this was happening? I am facibng the same problem. Please help.

Javier Ortiz replied on Mon, 2010/06/21 - 3:15pm

Any reason for this not working now? UmlGraph-5.3 For some reason it complains about not finding dot:

Building Package view for package games.jwrestling.common java.io.IOException: Cannot run program "dot": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:460) at java.lang.Runtime.exec(Runtime.java:593) at java.lang.Runtime.exec(Runtime.java:466) at org.umlgraph.doclet.UmlGraphDoc.runGraphviz(UmlGraphDoc.java:134) at org.umlgraph.doclet.UmlGraphDoc.generatePackageDiagrams(UmlGraphDoc.java:97) at org.umlgraph.doclet.UmlGraphDoc.start(UmlGraphDoc.java:65) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:269) at com.sun.tools.javadoc.DocletInvoker.start(DocletInvoker.java:143) at com.sun.tools.javadoc.Start.parseAndExecute(Start.java:340) at com.sun.tools.javadoc.Start.begin(Start.java:128) at com.sun.tools.javadoc.Main.execute(Main.java:41) at com.sun.tools.javadoc.Main.main(Main.java:31) The weird thing is that dot is accessible from the command prompt. I tried restarting the IDE without luck. When is the Netbeans RCP version of this going to be available?

Matt Coleman replied on Tue, 2012/05/08 - 4:29am in response to: javydreamercsw

 is Netbeans RCP version out now?we would love to try it

graphic artist buffalo

Mateo Gomez replied on Wed, 2012/05/09 - 2:23am

mexican dip recipes   have encountered error too.

Comment viewing options

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