What is a Maven Plugin?
A Maven plugin is a set of goals that extend Maven’s functionality.
Think of Maven as the engine, and plugins as tools that tell Maven what to do — like compiling code, packaging a JAR, running tests, or deploying to a server.
Each plugin performs a specific task in the build lifecycle.
Example:
maven-compiler-plugin→ compiles Java source files.maven-surefire-plugin→ runs unit tests.maven-jar-plugin→ packages compiled code into a JAR file.
How Plugins Work
A Maven build lifecycle (e.g., clean, default, site) consists of phases like compile, test, package, install, and deploy.
Each phase executes one or more plugin goals.
Example:
mvn package
runs:
-
compile→ viamaven-compiler-plugin -
test→ viamaven-surefire-plugin -
package→ viamaven-jar-plugin(ormaven-war-plugin)
Why Use Plugins?
Because Maven itself doesn’t "do" much — it delegates all real work to plugins.
They let you:
- Automate build steps (compile, test, package)
- Run code quality checks
- Generate reports
- Deploy artifacts to a repository
- Create Docker images, run servers, etc.
Basically, plugins = automation + flexibility.
When to Use Plugins
Use a plugin when you need Maven to perform a custom or automated task that’s not built-in.
Examples:
| Situation | Plugin |
|---|---|
| Need to compile Java code | maven-compiler-plugin |
| Need to create a JAR/WAR |
maven-jar-plugin / maven-war-plugin
|
| Need to run unit tests | maven-surefire-plugin |
| Need to run integration tests | maven-failsafe-plugin |
| Need to execute custom Java class | exec-maven-plugin |
| Need to package app as Docker image |
jib-maven-plugin or spotify-dockerfile-maven-plugin
|
| Need to format code or lint |
spotless-maven-plugin, checkstyle-maven-plugin
|
Common Maven Plugins (with Use Cases)
| Plugin | Goal / Phase | Purpose |
|---|---|---|
| maven-compiler-plugin |
compile, test-compile
|
Compiles Java source code and test code. |
| maven-surefire-plugin | test |
Runs unit tests. |
| maven-failsafe-plugin |
integration-test, verify
|
Runs integration tests (after packaging). |
| maven-jar-plugin | package |
Builds .jar files. |
| maven-war-plugin | package |
Builds .war files for web apps. |
| maven-install-plugin | install |
Installs the package into the local repository (~/.m2/repository). |
| maven-deploy-plugin | deploy |
Deploys artifacts to remote repositories. |
| maven-clean-plugin | clean |
Deletes target/ directory before a new build. |
| maven-site-plugin | site |
Generates project documentation. |
| exec-maven-plugin | Custom goals | Runs Java main classes or external commands. |
| spring-boot-maven-plugin | repackage |
Packages Spring Boot apps as executable JARs, allows mvn spring-boot:run. |
| checkstyle-maven-plugin | verify |
Performs static code analysis using Checkstyle rules. |
| spotless-maven-plugin | verify |
Formats and enforces code style. |
| jib-maven-plugin | Custom goal | Builds Docker/OCI images without Dockerfile. |
| liquibase-maven-plugin | Custom goal | Runs database migrations with Liquibase. |
Example: Define a Plugin in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>
Now running:
mvn compile
will use Java 21 for compilation.
Summary
| Concept | Description |
|---|---|
| What | Plugins extend Maven’s capabilities via goals. |
| How | Bound to build phases (like compile/test/package). |
| Why | Automate builds, testing, deployment, quality checks. |
| When | Whenever a project step can be automated or repeated. |
Top comments (0)