Learn the Uses of Different Maven Plugins

0
2137
Maven Plugins

In the last article, we discussed the use and significance of various Maven repositories. In this article, we are going to discuss the use of various Maven Plug-ins. Maven can be considered as a plugin execution framework in which each and every task is conducted through Plug-ins. Maven Plug-ins are used to perform the following type of tasks.

• JAR file creation.
• WAR file creation.
• Compilation of code files.
• Unit testing.
• Project document creation.
• Project reports creation.

A plugin in Maven generally delivers a set of goals that can be performed by using the following syntax.

As an example, we can compile a Maven project with the maven-compiler-plugin just by initiating a compile-goal after execution of the following command.

Types of Maven Plug-ins
The following type of Plug-ins are present in Maven.

S No.

Plugin Type

Description

1.

Build plugins

Build Plug-ins are used for the execution during the build process. They are configured in the <build/> element of pom.xml file.

2.

Reporting plugins

Reporting Plug-ins are used for execution during the site generation process and they are configured in the <reporting/> element of the pom.xml file.

Frequently used common Plug-ins of Maven.

S No.

Plug-in

Description

1.

clean

This Plug-in is used to clean up target after the build. It is used to completely delete the target directory.

2.

compiler

This Plug-in is used to compile Java source files.

3.

surefire

This Plug-in is used to run the JUnit unit tests. It is used to create test reports.

4.

jar

This Plug-in is used to build a JAR file from the current project.

5.

war

This Plug-in is used to build a WAR file from the current project.

6.

javadoc

This Plug-in is used to generate Javadoc for the project.

7.

antrun

This Plug-in is used to run a set of ant tasks from any phase which we have mentioned for the build.

Example
In the following example, we have used the maven-antrun-plugin in order to print data on console for various Maven profiles. Here the phase for each of profile is ‘clean’ and goal is ‘run’. The following is the content of the POM.xml file.

When we execute any of the Maven profile say ‘prod’, we can observe that the Maven Plug-in will start processing and display the clean phase of the clean life cycle as shown below.
Maven profile say prod

We can conclude the following points based on the above Maven Plug-ins.
• We can specify various Maven Plug-ins in pom.xml by using plugins element.
• Each of the specified plugin can have one or more goals.
• We can define the phase where these plug-ins can start processing by using their phase elements. Here we have used clean phase.
• We can configure tasks to echo particular message on the console by binding them to particular goals of plugin. Here, we have bound echo task to the run goal of maven-antrun-plugin.
• Maven has the capability to download the plugin from central or remote repository if it is not present in the local repository and can start its processing.

Source code for the various Maven Plug-ins

Conclusion: –
In this article, we discussed the different Maven plug-ins, along with a suitable example for ‘clean’ phase and goal as ‘run’.

LEAVE A REPLY

Please enter your comment!
Please enter your name here