Horje
Maven Plugins

Maven plugins are essential components in the Apache Maven build system designed to extend its functionality. They perform tasks such as compilation, testing, packaging, deployment, and others. In Maven, the plugins are divided into two types.

Types of Maven Plugin

  • Build Plugins
  • Reporting Plugins

Maven Build Plugins

These plugins are executed during the build process and these are defined in the <build> section in the pom.xml file. For example, Compiler Plugin, Surefire Plugin, and Assembly Plugin.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>


Maven Reporting Plugins

These plugins are used for generating reports about the project. These are defined in the <reporting> section in the pom.xml file. Examples include the Surefire Report Plugin for generating test reports and Javadoc Plugin for generating API documentation.

 <reporting>
        <plugins>
            <!-- Surefire Report Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>

            <!-- Javadoc Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <source>1.8</source>
                </configuration>
            </plugin>

            <!-- Checkstyle Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <configLocation>google_checks.xml</configLocation>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

Different types of Maven Plugins

  • Compiler Plugin: It is used for Compiles the project source code.
  • Surefire Plugin: It is used for Run the Project unit Tests.
  • Failsafe Plugin: It is used for Runs the project integration tests.
  • JAR Plugin: It is used for Packages the compiled code into a JAR file.
  • War Plugin: It is used for Packages the compiled code into a WAR file.
  • Assembly Plugin: It is used for Creates distributions from the project.
  • Javadoc Plugin: It is used for Generates Javadoc documentation for the project.
  • Checkstyle Plugin: It is used for Checks the code against coding standards.

Working Process of Plugins in a Maven Project

  • Define the project configuration and project structure and build plugins configurations in the POM file
  • Now configure the required plugins in the maven project.
  • After that maven build process is started by help of different phases in the maven build like validate, compile, test, package, verify, install, and deploy.

Example of Maven Plugin

Here we created a sample maven project with required project dependencies by using Spring Tool Suite IDE. In this, we will explain about build and reporting plugins with required examples.

Example 1: Build Plugins

In this example we will explain about build plugins. Here we creates a maven project with basic dependencies. Below we provide the that pom file for your reference.

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


pom.xml:

XML
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.app</groupId>
    <artifactId>mavenbuild</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavenbuild</name>
    <description>Spring Reactive</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


Example 2: Reporting Plugins

In this example, we will explain about reporting plugins. Here, we create a maven project with basic dependencies. Below, we provide the pom.xml file for your reference. Here, we use java doc reporting plugin for generating project report.

<reporting>
        <plugins>
            <!-- Javadoc Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <source>1.8</source>
                </configuration>
            </plugin>
        </plugins>
    </reporting>


pom.xml:

XML
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.app</groupId>
    <artifactId>mavenbuild</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavenbuild</name>
    <description>Spring Reactive</description>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <reporting>
        <plugins>
            <!-- Javadoc Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <source>17</source>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


Steps to Generate Javadoc Report

Step 1: Redirect to maven project by using command prompt.

Step 2: After this run below command,

mvn javadoc:javadoc

Output:

Javadoc Report Generated


Step 3: Now go to location in the project folder target/site/apidocs/index.html

Project Folder Structure






Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Maven Build Lifecycle Maven Build Lifecycle
Maven Dependency Scopes Maven Dependency Scopes
Feedback Collection System using Spring Boot Feedback Collection System using Spring Boot
How to Create an Executable JAR with Maven? How to Create an Executable JAR with Maven?
Skipping Tests with Maven Skipping Tests with Maven

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14