Plugins
An overview of various plugins used commonly across various projects in the form of categories.
About
Maven plugins are essential components that help automate and manage various tasks in a Maven build lifecycle. They extend Maven's capabilities beyond dependency management and are used for compilation, testing, packaging, deployment, reporting, and more.
A plugin in Maven is simply a set of goals that perform specific tasks, such as:
Compiling Java code (
maven-compiler-plugin
)Running tests (
maven-surefire-plugin
)Packaging applications (
maven-assembly-plugin
,maven-war-plugin
)Analyzing code quality (
maven-checkstyle-plugin
,spotbugs-maven-plugin
)Deploying artifacts (
maven-deploy-plugin
)
Goals in Maven Plugins
A goal in Maven is a specific task performed by a plugin. Each plugin consists of one or more goals that define what actions the plugin will execute.
For example, the maven-compiler-plugin
has the goal compile
, which compiles Java source files.
Goals vs. Plugins vs. Phases
Plugin = A collection of goals (e.g.,
maven-compiler-plugin
)Goal = A specific task within a plugin (e.g.,
compile
)Phase = A stage in the build lifecycle (e.g.,
compile
,test
,package
)
A goal can be bound to a phase or executed directly without a phase.
Goals can be executed -
Manually using the command:
This will execute the compile
goal from the maven-compiler-plugin
without running the entire build lifecycle.
Bound to the Maven Build Lifecycle (e.g.,
compile
,test
,package
) so they execute automatically.
Maven plugins are executed within different phases of the Maven Build Lifecycle.
Phase
Common Plugin Execution
validate
maven-enforcer-plugin
(checks rules)
compile
maven-compiler-plugin
(compiles Java code)
test
maven-surefire-plugin
(runs unit tests)
package
maven-jar-plugin
, maven-war-plugin
(creates JAR/WAR)
verify
maven-failsafe-plugin
(integration tests)
install
maven-install-plugin
(installs to local repository)
deploy
maven-deploy-plugin
(deploys artifacts)
How to Find Goals for Any Plugin?
To see all goals of a plugin, use:
For example, to see all goals of maven-compiler-plugin
:
Why Do We Need Plugins?
Maven follows the Convention over Configuration principle, meaning most tasks (compiling, testing, packaging) follow predefined conventions. However, real-world projects often require customization and automation, which is where plugins come in.
Some of the Reasons for Using Plugins
Automate Build & Deployment Tasks – Saves time by automating repetitive build steps.
Enhance Code Quality – Plugins like Checkstyle and SpotBugs help enforce coding standards.
Generate Reports & Documentation – Helps create Javadoc, project metadata, and test reports.
Package Applications in Different Formats – WAR, JAR, and assembly packaging.
Optimize Performance – Parallel test execution, build profiling, and performance tuning.
Inbuilt (Default) Plugins in Maven
Maven includes some default plugins that are automatically used when running the build lifecycle. These plugins do not need to be explicitly declared in pom.xml
.
Default Plugins for Each Lifecycle Phase
Maven automatically uses built-in plugins for different phases of the build lifecycle:
Lifecycle Phase
Default Plugin & Goal
validate
maven-enforcer-plugin:enforce
compile
maven-compiler-plugin:compile
test-compile
maven-compiler-plugin:testCompile
test
maven-surefire-plugin:test
package
maven-jar-plugin:jar
, maven-war-plugin:war
install
maven-install-plugin:install
deploy
maven-deploy-plugin:deploy
How to Declare a Plugin ?
Maven plugins are declared inside the <plugins>
section of the pom.xml
file.
Example: Declaring a Plugin
Below is an example of the Maven Compiler Plugin, which sets the Java version:
Common Maven Plugins & Their Goals
Compilation & Testing Plugins
maven-compiler-plugin
compile
, testCompile
Compiles Java source code
maven-surefire-plugin
test
Runs unit tests
maven-failsafe-plugin
integration-test
, verify
Runs integration tests
Packaging & Deployment Plugins
maven-jar-plugin
jar
Packages JAR files
maven-war-plugin
war
Packages WAR files
maven-install-plugin
install
Installs artifacts in the local repository
maven-deploy-plugin
deploy
Deploys artifacts to a remote repository
Code Quality & Reporting Plugins
maven-checkstyle-plugin
check
Enforces code style rules
maven-pmd-plugin
pmd
, cpd
Static code analysis
maven-javadoc-plugin
javadoc
Generates API documentation
jacoco-maven-plugin
prepare-agent
, report
Test coverage analysis
Plugin Management
About
Plugin Management in Maven is a way to predefine plugin configurations and versions in the pom.xml
, ensuring consistency across multiple modules in a project. It allows you to declare plugins in a centralized manner without immediately applying them.
<plugins>
: Defines and applies a plugin immediately.<pluginManagement>
: Declares plugin settings but does NOT apply them automatically. They are only applied if explicitly used in<plugins>
.
Why is Plugin Management Needed?
Ensures consistent versions of plugins across all modules in a multi-module project.
Avoids version mismatches when plugins are implicitly used.
Centralizes plugin configuration, reducing duplication in child modules.
Allows easy customization of plugin settings.
How to Declare Plugin Management?
We define plugin management inside <pluginManagement>
inside <build>
in the parent pom.xml
:
This does not automatically apply the plugins.
To use a managed plugin in a child module, we must explicitly declare it in
<plugins>
without specifying the version.
How to Use a Managed Plugin in a Child Module?
In a child module, we do NOT need to specify the version since it is inherited from <pluginManagement>
:
Maven will automatically use version 3.8.1
from the parent POM.
Use Cases of Plugin Management
1. Multi-Module Project Plugin Version Control
In a large enterprise project, multiple modules should use the same plugin version to avoid compatibility issues.
2. Enforcing a Specific Compiler Version
To prevent developers from using different Java versions for compilation.
3. Centralizing Plugin Configurations
If you need the same configurations across multiple modules (e.g., JUnit test runner in maven-surefire-plugin
).
Last updated
Was this helpful?