Over a million developers have joined DZone.
Platinum Partner

Javadoc or Doxygen?

· Agile Zone

The Agile Zone is brought to you in partnership with JetBrains. Learn how Agile Boards in YouTrack are designed to help teams plan, visualize and manage their work in an efficient manner, with support for both Scrum and Kanban processes.

In the last two articles, "Reverse-engineer Source Code into UML Diagrams" and "Visual Documentation of Ant Dependencies in 3 Simple Steps"  we saw how easy and valuable it was to automate technical documentation. By using open source tools, we were easily able to provide good technical documentation within a few minutes, and at no cost at all. We were also able to keep this up-to date by adding additional tasks to our Ant build files, and run them from our CI Server(Hudson in our case) on commit and nightly builds, and also publish the results.

In this article, I will be showing you how to use yet another tool called Doxygen for generating technical documentation based on your source code. We all have used Javadoc and have been using it for a long time, right? So, you may ask what's the need to have another tool which produces the same HTML documentation? Doxygen has a slight edge over Javadoc and here are a few reasons why you should consider using the same:

  1. With Javadoc you have to remember all the HTML tags, you need to embed within your code comments. However, with Doxygen code comments are much more concise and polished, without the need for any HTML.

  2. Doxygen can also generate a variety of diagrams, we will take a look at some of them later.

  3. Doxygen also provides a structured view on the source code. As I mentioned in 2 above in the form of various diagrams, cross-referenced and syntax highlighted code.

  4. You get all the above benefits even if the code does not have any comments at all. 

  5. Last but not the least, Doxygen is a documentation system not for just Java but also for various other languages like  C++, C, Java, Objective-C, Python, IDL (Corba and Microsoft flavors), Fortran, VHDL, PHP, C#.

So, without wasting further time, lets see what we need to get started with Doxygen.

Step 1. Download, Install Doxygen.

Download the binary distribution for Doxygen for the operating system you are using. I downloaded the binary distribution for Mac OS X called Doxygen-1.5.6.dmg. Installation is very simple, just drag the doxygen icon from this folder to the Applications folder, or wherever you want to keep it; as shown. I dropped it within my Applications folder. Just be sure to remember where you dragged it. To uninstall, just delete the file. It is completely self-contained.  


Step 2: Configure Doxygen. 

To generate documentation using Doxygen, you will need a configuration file called the Doxyfile. You can generate this file in two ways; either by using the Doxygen wizard or by using the command line option. Lets see how to use both these options to generate the configuration file:

a. Command line. Open a command window and type the following as shown below:

You should be able to locate the configuration file created within your default user folder. The file looks like this:

# Doxyfile 1.5.6

# This file describes the settings to be used by the documentation system
# doxygen (www.doxygen.org) for a project
# All text after a hash (#) is considered a comment and will be ignored
# The format is:
#       TAG = value [value, ...]
# For lists items can also be appended using:
#       TAG += value [value, ...]
# Values that contain spaces should be placed between quotes (" ")

# Project related configuration options

# This tag specifies the encoding used for all characters in the config file
# that follow. The default is UTF-8 which is also the encoding used for all
# text before the first occurrence of this tag. Doxygen uses libiconv (or the
# iconv built into libc) for the transcoding. See
# http://www.gnu.org/software/libiconv for the list of possible encodings.


# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
# by quotes) that should identify the project.

PROJECT_NAME           =

# The PROJECT_NUMBER tag can be used to enter a project or revision number.
# This could be handy for archiving the generated documentation or
# if some version control system is used.



b. Wizard Option.

Launch the  Doxygen application, and you should be able to create the configuration file using the wizard approach as shown below.


The user interface is quite intuitive so I am going to skip explaining this in detail.
The wizard approach was the one I used to get the initial settings for the configuration file. which you can always modify later.

A few options in my Doxygen configuration file are as follows:

# Doxyfile 1.5.6

# Project related configuration options
PROJECT_NAME           = PetStore
PROJECT_NUMBER         = 1.0
# Build related configuration options
EXTRACT_ALL            = NO
# configuration options related to the HTML output
HTML_OUTPUT            = html

 Step 3. Doxygen and Ant.

In order to use Doxygen, we need an Ant task. There is already an Ant task written for Doxygen which you can download from here.
As always, since using Mac, when I downloaded the binaries and tried to use them, I got the ever famous error message :

java.lang.UnsupportedClassVersionError: Bad version number in .class file

 So, had to do download the source, compile, and jar it up. Copy this library to your projects folder. Next, lets start making changes to our build file.

3.a: Lets define the Doxygen task.

<taskdef name="doxygen" classname="org.doxygen.tools.DoxygenTask" classpath="lib/ant_doxygen.jar" />

3.b: To generate HTML documentation:

<doxygen configFilename="reports/Doxyfile">
<property name="INPUT" value="${srcdir}" />
<property name="RECURSIVE" value="yes" />

3.c: Lets combine them in target and run:

<target name="generate-doxygen-docs">
<taskdef name="doxygen" classname="org.doxygen.tools.DoxygenTask"
classpath="lib/ant_doxygen.jar" />
<doxygen configFilename="reports/Doxyfile">
<property name="INPUT" value="${srcdir}" />
<property name="RECURSIVE" value="yes" />

  [doxygen] Exec: /Applications/Doxygen.app reports/Doxyfile

/CodeMetricsProject/build.xml:91: Doxygen not found on the PATH.

Total time: 7 seconds

3.d: So, to launch Doxygen not in the path.

we make change to the doxygen task as shown below:

<doxygen doxygenPath="/Applications/Doxygen.app/Contents/Resources/doxygen" configFilename="reports/Doxyfile">

Lets run the target again and see if it fixed things.Yes indeed.

  [doxygen] Exec: /Applications/Doxygen.app/Contents/Resources/doxygen reports/Doxyfile

Total time: 8 seconds

4. Integrate with Hudson.

4.a Modify Hudson Job.

Once you have an Ant target working, calling this from your CI server is trivial. Within Hudson, select your Job, click on configure and add this new target to be called when running the build.

 4.b Publish the reports.

 4. c: Sample Reports.

Force a build, and take a detailed look at the reports generated by Doxygen as shown below:



I have given you a brief overview of Doxygen in this article, how to configure the same, and use it effectively to generate technical documentation on a continuous basis; either on commit builds or nightly builds. The Doxygen web site has lots of information on how to use it with other programming languages and also has tutorials in languages other than English as well. 

As always, if you are having trouble getting Doxygen to work, leave a comment or check out the Doxygen web site.

The Agile Zone is brought to you in partnership with JetBrains. Learn how Agile Boards in YouTrack are designed to help teams plan, visualize and manage their work in an efficient manner, with support for both Scrum and Kanban processes.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}