Maven Javadoc Plugin uses the Javadoc tool to generate javadocs for the specified project. This tutorial show you how to Generate HTML JavaDoc in your build using maven. The Javadoc Plugin gets the parameter values that will be used from the plugin configuration specified in the pom. To hold all javadoc arguments, packages or files, the Javadoc Plugin generates argument files and calls the Javadoc located in $JDK_HOME/bin/ directory.
Let’s begin:
Before generating javadoc using maven javadoc plugin, we will create a maven project following the steps below:
Project directory structure
The project directory structure should be like this
Maven dependency
This project are using hibernate and mysql dependency and here is our 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<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.javabycode.hibernate</groupId> <artifactId>HibernateHelloWorldExample</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>HibernateHelloWorldExample</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <hibernate.version>4.3.11.Final</hibernate.version> <mysql.version>5.1.31</mysql.version> </properties> <dependencies> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.8</version> </plugin> </plugins> </reporting> </project> |
Let’s dig deeper:
We are using Maven Javadoc Plugin to generate HTML JavaDoc so that we have this plugin in pom.xml file.
1 2 3 4 5 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> </plugin> |
We are using Maven Project Info Reports plugin to generate reports information about the project so that this plugin must be added in pom.xml file
1 2 3 4 5 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>2.8</version> </plugin> |
Create Model
We create a simple POJO class named Student which be mapped to STUDENT table in database.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
package com.javabycode.hibernate.model; import java.util.Date; /** * Student Object * */ public class Student { private int id; private String name; private Date enterDate; private String nationality; private String code; /** * Default constructor */ public Student(){} /** * Creates a new Student * @param name unique name of the Student * @param enterDate enterDate of the Student * @param nationality nationality of the Student * @param code code of the Student */ public Student(String name,Date enterDate,String nationality, String code){ this.name = name; this.enterDate = enterDate; this.nationality = nationality; this.code = code; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setCode(String code) { this.code = code; } public void setEnteringDate(Date enterDate) { this.enterDate = enterDate; } public void setNationality(String nationality) { this.nationality = nationality; } public String getCode() { return code; } public Date getEnterDate() { return enterDate; } public String getNationality() { return nationality; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((code == null) ? 0 : code.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Student)) return false; Student other = (Student) obj; if (id != other.id) return false; if (code == null) { if (other.code != null) return false; } else if (!code.equals(other.code)) return false; return true; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", enteringDate=" + enterDate + ", nationality=" + nationality + ", code=" + code + "]"; } } |
Create Hibernate Utility class
For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:
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 |
package com.javabycode.hibernate; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; /** * Hibernate Utility class with a convenient method to get Session Factory object. * */ public class HibernateUtil { private static final SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; static { try { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = new Configuration().configure() .buildSessionFactory(serviceRegistry); } catch (Throwable ex) { System.err.println("Session Factory could not be created." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } |
Create a program to perform CRUD operations on Database
Now we create Hibernate Hello World Example in Eclipse like below
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
package com.javabycode.hibernate; import java.util.Date; import java.util.List; import org.hibernate.Session; import com.javabycode.hibernate.model.Student; /** * The program performs CRUD operation on database with Hibernate API's * */ public class HelloworldExample { @SuppressWarnings("unused") public static void main(String[] args) { HelloworldExample application = new HelloworldExample(); /* * Dump data by saving few objects with hibernate */ int id1 = application.saveStudent("Bill Gate", new Date(), "USA", "1111111"); int id2 = application.saveStudent("Larry Page", new Date(), "France", "1111112"); int id3 = application.saveStudent("Steve Job", new Date(), "England", "1111113"); int id4 = application.saveStudent("David Pham", new Date(), "Canada", "1111114"); /* * Get all saved objects */ List<Student> students = application.getAllStudents(); System.out.println("\n*******List of all persisted students*******"); for (Student student : students) { System.out.println(student); } /* * Update an object */ application.updateStudent(id3, "Tim Cook"); /* * Deletes an object */ application.deleteStudent(id2); /* * Retrieve all saved objects */ List<Student> remaingStudents = application.getAllStudents(); System.out .println("\n*******List of all remained persisted students*******"); for (Student student : remaingStudents) { System.out.println(student); } } /** * Save a Student object in database */ public int saveStudent(String name, Date enteringDate, String nationality, String code) { Student student = new Student(); student.setName(name); student.setEnteringDate(enteringDate); student.setNationality(nationality); student.setCode(code); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); int id = (Integer) session.save(student); session.getTransaction().commit(); return id; } /** * List of all persisted Student objects from database */ public List<Student> getAllStudents() { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); @SuppressWarnings("unchecked") List<Student> employees = (List<Student>) session.createQuery( "FROM Student s ORDER BY s.id ASC").list(); session.getTransaction().commit(); return employees; } /** * Update a specific Student object */ public void updateStudent(int id, String nationality) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Student student = (Student) session.get(Student.class, id); student.setNationality(nationality); session.update(student); session.getTransaction().commit(); } /** * Delete a specific Student object */ public void deleteStudent(int id) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Student student = (Student) session.get(Student.class, id); session.delete(student); session.getTransaction().commit(); } } |
Generate HTML JavaDoc using Maven Javadoc Plugin
Now, we are using Maven Javadoc Plugin to build the reports with command below
1 |
mvn clean site |
Output in console
Project Information Output
Html JavaDoc
That’s it on the tutorial Maven Javadoc Plugin – Generate HTML javadoc
References
Apache Maven Documentation
Download complete source code, click link below
HibernateMavenJavaDocExample.zip (306 downloads)