How to Generate JavaDoc Jar file with Maven

This example explains how you can Generate JavaDoc Jar file in your build using Maven. When developing internal libraries for your company it is recommended to automatically generate javadoc jar files. That way other developers in your company who uses your dependencies can look inside the code with a decent IDE. Believe me things can be quite frustrating when they don’t. These JavaDocs can be automatically created by the maven-javadoc plugin which uses the javadoc executable located in $JDK_HOME/bin/ directory.

Create a simple User POJO

Lets create a simple POJO as an example JavaDoc candidate.

/**
 * User object
 */
public class User {

    private Integer id;
    private String username;
    private String password;
    private String email;

    /**
     * Creates a new user
     * @param username unique username of the user
     * @param password password of the user
     * @param email email address of the user
     */
    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Create a User Service

For the example we also create a simple UserService interface for JavaDoc purposes.

/**
 * This service will handle all the user interactions
 */
public interface UserService {

    /**
     * This method will register a user
     * @param user user that will be registered
     */
    void register(User user);

    /**
     * Fetches a user with a given Identifier
     * @param id unique identifier of a user
     * @return User Object
     */
    User fetchUser(Integer id);
}

Generate HTML JavaDoc using Maven

In order to Generate HTML JavaDoc using maven we must register the maven-javadoc plugin in the build element. This will tell maven to generate JavaDoc jar files in the install phase.

<?xml version="1.0" encoding="UTF-8"?>
<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.memorynotfound.maven.plugin.javadoc</groupId>
    <artifactId>generate-javadoc-jar</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <url>http://memorynotfound.com</url>
    <name>MAVEN - ${project.artifactId}</name>
    <description>This project will generate javadoc jar file using maven</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.3</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Execute maven goals

Lets build the reports using mvn clean install.

mvn clean install

Output

target
    |--generate-javadoc-jar-1.0.0-SNAPSHOT.jar
    |--generate-javadoc-jar-1.0.0-SNAPSHOT-javadoc.jar

References

Download

You may also like...