Maven plugins are executable components that extend Maven's functionality. Each plugin contains one or more goals for executing specific tasks (such as compilation, testing, packaging, etc.).
Relationship between Plugins and Lifecycle:
Lifecycle: Defines the sequence of build phases
Plugin: Provides concrete implementations (goals) to complete tasks for these phases
Maven has three standard lifecycles:
clean: Handles project cleanup
default (or build): Handles project deployment
site: Handles project site documentation creation
Each lifecycle contains a series of phases. These phases represent Maven's unified interfaces, with their implementations provided by Maven plugins.
When executing Maven commands like mvn clean, clean corresponds to the clean phase in the Clean lifecycle. However, the actual clean operation is implemented by maven-clean-plugin.
Thus, every phase in Maven's lifecycle is implemented through Maven plugins.
Maven is essentially a framework that executes tasks through plugins. Each task is completed by plugins. Maven plugins are typically used for:
Creating JAR files
Creating WAR files
Compiling code files
Running unit tests
Generating project documentation
Creating project reports
Plugins usually provide a set of goals and can be executed using the following syntax:
mvn :
For example, a Java project can use the compile goal of maven-compiler-plugin for compilation with the command:
mvn compiler:compile
Core Plugin Mechanism
Plugin Coordinates
Similar to dependencies, plugins are uniquely identified by groupId, artifactId, and version:
Property
Description
Common Values
groupId
Plugin organization
org.apache.maven.plugins (official plugins)
artifactId
Plugin name
maven-compiler-plugin
version
Plugin version
3.8.1
Plugin Goals
Each plugin can contain multiple goals, for example:
maven-compiler-plugin:
compile: Compile main code
testCompile: Compile test code
maven-surefire-plugin:
test: Run unit tests
Plugin Binding Methods
1. Built-in Binding
Maven has pre-bound plugin goals to lifecycle phases:
Lifecycle Phase
Bound Plugin Goal
compile
maven-compiler-plugin:compile
test
maven-surefire-plugin:test
package
maven-jar-plugin:jar (or maven-war-plugin:war)
2. Custom Binding
Explicit configuration in pom.xml:
org.apache.maven.pluginsmaven-compiler-plugin3.8.1compilecompile
Set via configuration tag:
org.apache.maven.pluginsmaven-surefire-plugin2.22.2**/*Test.java
Multiple goal execution:
run-teststesttest
Plugin Development Basics
Mojo Concept
The Java class corresponding to a plugin goal is called a Mojo (Maven plain Old Java Object).
Must inherit from AbstractMojo and implement the execute() method.
Simple Plugin Example
Example
@Mojo(name ="greet", defaultPhase = LifecyclePhase.COMPILE)
public class GreetingMojo extends AbstractMojo {
@Parameter(property ="name", defaultValue ="World")
private String name;
public void execute()throws MojoExecutionException {
getLog().info("Hello, "+ name +"!");
}
}
Packaging and Installing Plugins
mvn clean install
Plugin Types
Maven provides two types of plugins:
Type
Description
Build plugins
Execute during build and configured in element of pom.xml.
Reporting plugins
Execute during site generation and configured in element of pom.xml.
Common plugins list:
Plugin
Description
clean
Cleans target files after build. Deletes target directory.
compiler
Compiles Java source files.
surefire
Runs JUnit unit tests. Creates test reports.
jar
Builds JAR file from current project.
war
Builds WAR file from current project.
javadoc
Generates Javadoc for project.
antrun
Runs a set of Ant tasks from any phase of the build process.
Example
We've extensively used maven-antrun-plugin in our examples to output data to the console. See the Maven - Build Profiles chapter. Let's understand this better by creating a pom.xml file in the C:\MVN\project directory.
4.0.0com.companyname.projectgroupproject1.0org.apache.maven.pluginsmaven-antrun-plugin1.1id.cleancleanrunclean phase
Next, open the command terminal, navigate to the directory containing pom.xml, and execute the following mvn command.
mvn clean
Maven will start processing and display the clean phase of the clean lifecycle.
Scanning for projects... ------------------------------------------------------------------ Building Unnamed - com.companyname.projectgroup:project:jar:1.0 task-segment: ------------------------------------------------------------------ [clean:clean {execution: default-clean}] [antrun:run {execution: id.clean}] Executing tasks clean phase Executed tasks ------------------------------------------------------------------ BUILD SUCCESSFUL ------------------------------------------------------------------ Total time: < 1 second Finished at: Sat Jul 07 13:38:59 IST 2012 Final Memory: 4M/44M ------------------------------------------------------------------
The example demonstrates the following key concepts:
Plugins are defined in pom.xml using the element.
Each plugin can have multiple goals.
You can define phases where plugins start processing using the element. We used the clean phase.
You can configure tasks to execute by binding to plugin goals. We bound the echo task to maven-antrun-plugin's run goal.
That's it - Maven handles the rest. It will download plugins not available in the local repository and start processing.