Table of contents
1.
Introduction
2.
What is Javadoc?
3.
Javadoc Plugin in Maven
3.1.
Build Lifecycle of Maven
3.2.
Javadoc Plugin
4.
Frequently Asked Questions
4.1.
How can we fix require upper bound dependencies error in Maven? 
4.2.
What is the difference between phase and goal in Maven?
4.3.
What is a Plugin in Maven? 
5.
Conclusion
Last Updated: Mar 27, 2024

Maven Javadoc Plugin

Author Neha Chauhan
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Maven is a management tool for managing Java projects. It makes the task of creating documentation for a project, adding plugins and dependencies for a project, and version control for a Java project simple, fast and clean. 

It is based on the concept of a Project Object Model (POM), which is an XML file holding all the information about the configuration details, plugin details, and goals of a project. Creating documentation for an API (Application Programming Interface) is important but the task of creating the documentation is a boring and tedious job. 

To make this task easier every latest version of JDK (Java Development Kit) has an inbuilt tool name Javadoc that can be used to generate Java API documentation from the comments present in the Java code. This functionality of JDK can be used from the command line also, but Maven’s Javadoc plugin functionality makes the task of using Javadoc much easier and error-free.

Maven Javadoc plugin

In this article, we will discuss how to use the Javadoc plugin in Maven. We will also discuss how to disable this plugin. 

What is Javadoc?

Javadoc is a Java tool in all the latest versions of JDK (Java Development Kit). It generates the documentation for a Java API in the form of an HTML page. It takes the comments in the Java code and creates an HTML document. 

The comments should be written in the same manner as the multi-line comments, but the only difference is that there should be an extra ‘*’ (asterisk) sign at the start. 

Example - 

/* Comment line 1

* Comment line 2

*/

The above lines of comment are regular comments. 

/*Comment line 1

Comment line 2

*/

In the above lines of comments, you can see that there is an extra ‘*’ sign, this extra ‘*’ sign makes it a Javadoc-style comment. 

Now that we have understood what Javadoc is, let’s understand how Maven uses this Javadoc tool to generate documentation. 

Javadoc Plugin in Maven

Before moving forward and discussing Javadoc Plugin in Maven, let’s discuss one more concept that will help you understand Maven and Plugins more clearly. 

Build Lifecycle of Maven

Let’s discuss what the build lifecycle of Maven is. 

▶️ There are three build processes - defaultclean and site

▶️ For each build process, we have Phases. A Phase is responsible for performing a task. It is made up of a sequence of goals. 

▶️ A Goal is a much finer task that should be executed. 

▶️ A group of goals, either they belong to the same phase or they belong to different phases is called a Plugin

A Plugin is a reusable piece of code that can be used to execute the same build logic without writing it again and again. 

Javadoc Plugin

The Javadoc Plugin uses the Javadoc tool to generate documentation for specified files.  

Configure the pom.xml file as given below - 

<project xmlns="http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ninja</groupId>
<artifactId>ninja</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>ninja project</name>
<url>http://maven.apache.org</url>
<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
         <executions>
            <execution>
               <id>attach-javadocs</id>
               <goals>
                  <goal>jar</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>
</project>

 

Run the following command

mvn install

 

Output

Two .jar file will be created containing the documentation. 

Must Read Apache Server

Frequently Asked Questions

How can we fix require upper bound dependencies error in Maven? 

Update the version in the pom. xml to the newest version listed in the output from maven-enforcer-plugin, in case that artifact is already declared in the pom.xml. You can apply the requireUpperBoundDeps to ensure that the dependencies are in a correct inheritance. 

What is the difference between phase and goal in Maven?

A Phase is a collection of goals that perform a task. When we run a phase, several different goals that are included in the phase run in a pre-defined sequence. A Goal is a specific task that needs to be performed, it is a much finer task than a Phase. For example, in compiler: compile - compile is the Goal for the compiler phase of the compiler plugin. 

What is a Plugin in Maven? 

A Plugin is the heart of Maven. It is a reusable piece of code that can be used to reuse the common build logic across different platforms. A Plugin is a group of different goals. These goals might or might not be related to each other. 

Conclusion

In this article, we have discussed what a Javadoc tool is and how it is used by Maven as a Javadoc Plugin. We discussed how to configure this Javadoc plugin in the pom.xml file and use it. 

Do not stop learning! We recommend you read these articles- 

🔥 Maven Eclipse Plugin

🔥 Maven Plugins

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass