Maven Tutorials – Maven plugins

Previous – Chapter 6

Maven Tutorial – Chapter 7

What is a maven plugins?

Plugin in maven is the one of the vital feature that is basically used to reuse the common build logic across different projects. Plugins are the ones through which all the tasks like compiling code, testing them with the junits, creating jar/war/ear files and documentation of the projects are carried out. Most of the work in maven is done using plugins, since the dependencies (jar files) are added only to the classpath while executing tasks.

So, maven is nothing but a plugin execution framework in which every tasks are accomplished by the usage of plugins. Below are the basic list of tasks that are handled by plugins in maven:

  • Creating jar/war/ear files.
  • Code compilation
  • Unit testing of the code.
  • Project documentation

Behavior of any plugin can be changed using a set of parameters exposed by each plugin goal (also known as Mojo). A Mojo in maven is just a goal, basically specifies the metadata of a goal – goal name, which phase it fits into and parameters expected by the goal.

Types of Plugins

Basically 2 important types of plugins exist in maven as listed below:

  • Build Plugins – Basically these plugins will execute during the build phase. These plugins are defined under the <build> element in pom.xml
  • Report Plugins – These plugins are executed during the site generation (report or javadocs generation) phase. These plugins will be configured under the <reporting> element in pom.xml

Common plugins

Below are list of commonly used plugins in any java projects built using maven:

Plugin Description
clean Used to delete the target directory in order to clean up the earlier build artifacts
compiler To compile java source files
surefile Executes the Junit tests and generate the reports.
jar Build the jar file of the project
war Build the war file of the project
javadoc Generates the javadocs of the project
antrun Runs a set of ant tasks specified in any stage/phase of the build

Sample plugin in pom.xml

 


<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.orgname.orggroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<build>
      <plugins>
            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-antrun-plugin</artifactId>
                <version>1.1</version>
                <executions>
                     <execution>
                          <id>id.clean</id>
                          <phase>clean</phase>
                          <goals>
                                  <goal>run</goal>
                          </goals>
                        <configuration>
                           <tasks>
                             <echo>Clean Phase</echo>
                          </tasks>
                        </configuration>
                     </execution>
                </executions>
             </plugin>
       </plugins>
   </build>
</project>
  • Each plugin can have more than one goals to perform different set of tasks accordingly.
  • Starting phase of any plugin can be defined.
  • Tasks can be configured associated to each goal of the plugin defined.

Benefits of maven plugins

  • More detailed and well architecture.
  • A managed life cycle
  • Implementation of multiple languages
  • Re-usability of the common build logic.
  • Ability to write maven plugins completely in java

Next – Chapter 8


Comments

Questions? Comments? Suggestions? Let us know!! Like / Subscribe / Follow for more updates.