In this post, We will learn How to generate Javadoc in the maven project.
Generating Javadocs in Maven:
- Maven usually using the maven-javadoc plugin to generate the Javadocs of a Project.
- This plugin usually internally using JDK\bin\javadoce.exe command to generate Javadocs.
- When we deploy the Project using mvn install command, it generates the Javadocs for the project.
Configuring Javadocs Plugin
The generated Javadocs we can find in the project’s location: /target/apidocs.
The Javadoc plugin can be configured for any project in pom.xml as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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> |
Let’s try to understand the above concept using a demo project:
Project Structure:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>MyAppJavaDocExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MyAppJavaDocExample</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <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> |
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.kkjavatutorials.model; /** * User model class * @author KK JavaTutorials * */ public class User { private Integer id; private String username; private String password; /** * Creates a new user * @param username unique username of the user * @param password password of the user */ public User(Integer id, String username, String password) { super(); this.id = id; this.username = username; this.password = password; } public Integer getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } } |
UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kkjavatutorials.service; import com.kkjavatutorials.model.User; /** * This UserService will handle all the user interactions * @author KK JavaTutorials */ public interface UserService { /** * This method can be used to register a user * @param user object hold user registration information */ public abstract void register(User user); /** * Fetches a user information with a given id * @param id unique identifier of a user * @return User Object */ public abstract User fetchUser(Integer id); } |
UserServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.kkjavatutorials.service.impl; import com.kkjavatutorials.model.User; import com.kkjavatutorials.service.UserService; /** * This class implements UserService and provides business logic for user interactions * @author KK JavaTutorials * */ public class UserServiceImpl implements UserService { /** * Method used to register a user * @param user object hold user registration information */ @Override public void register(User user) { //Business Logic.. } /** * Method to read a user information with a given id * @param id unique identifier of a user * @return User Object */ @Override public User fetchUser(Integer id) { //Business Logic.. return null; } } |
Generating Javadocs
Once You add the above-mentioned plugin in your project’s pom.xml, all you have to do is, Just open the command prompt, go to the project directory, For eg. E:\JavaWorkSpaces\MavenDemoWorkSpaceForBlog\MyAppJavaDocExample and run the command maven install as shown below.
The generated javadocs we can find in the project’s location :
E:\JavaWorkSpaces\MavenDemoWorkSpaceForBlog\MyAppJavaDocExample\target\apidocs
Below screenshots are generated java doc for User.java, UserService.java, and UserServiceImpl.java
E:/JavaWorkSpaces/MavenDemoWorkSpaceForBlog/MyAppJavaDocExample/target/apidocs/com/kkjavatutorials/model/User.html
E:/JavaWorkSpaces/MavenDemoWorkSpaceForBlog/MyAppJavaDocExample/target/apidocs/com/kkjavatutorials/service/UserService.html
E:/JavaWorkSpaces/MavenDemoWorkSpaceForBlog/MyAppJavaDocExample/target/apidocs/com/kkjavatutorials/service/impl/UserServiceImpl.html
That’s all about How to generate Javadoc in the maven project
You May Also Like:
What is Maven?
How to install and setup Maven Environment on windows?
Create a maven project using the command line
How to import the maven project in Eclipse?
How to Create a New Maven Project in Eclipse
How to create a maven web application project in Eclipse
How to create a multi-module project using maven?
How to Fix missing src/main/java & src/test/java folders in the Eclipse Maven web Project?
How to create user-defined properties in Maven?
What is the maven local repository location & how to change it?
How to add or update the maven settings.xml file in eclipse?
Importing maven remote Archetype Catalogs in eclipse ?
What is POM in the maven Project?
What is Super POM in Maven Project?
Understanding the purpose of Maven Plugin
Compiling the Maven project with different JDK versions?
Understanding of settings.xml in maven
How to enable a proxy setting in Maven?
Maven Dependency Scopes
How to create a runnable JAR file with Maven?
How to add local jar files to a Maven project?
How to convert an existing Java Project to Maven in Eclipse?
How to exclude dependency in maven?
Managing External Dependencies in maven
Understanding of Maven Repositories
What is an archetype in Maven?
Snapshot Vs Version in maven
Apache Maven – Profiles
If you have any feedback or suggestion please feel free to drop in below comment box.