Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

What are the different plugins used for Maven? Explain their uses?

Maven is a plugin-execution framework: every real task in a build, such as compiling code, running tests, or packaging a JAR, is performed by a plugin. Apache Maven groups plugins into two categories. Build plugins run during the build and are declared in the <build> element of pom.xml, while reporting plugins run during site generation and are declared in the <reporting> element. The most common build plugins include the compiler, resources, surefire, failsafe, jar, war, shade, install, and deploy plugins, each bound to a specific lifecycle phase.

What Is a Maven Plugin?

A Maven plugin is a bundle of one or more goals. A goal is a single executable task, for example What Is Maven? uses compiler:compile to compile sources and surefire:test to run unit tests. Because Maven itself does almost nothing without plugins, you can think of it as an engine that orchestrates plugin goals.

Goals connect to the build through the lifecycle. The default lifecycle moves through ordered phases such as validate, compile, test, package, verify, install, and deploy. A plugin goal can be bound to a phase, so it executes automatically whenever that phase runs, or you can invoke a goal directly with mvn plugin:goal. For a standard JAR project, for instance, the compiler plugin is bound to compile, surefire to test, and the jar plugin to package.

Plugins are declared in the What Is Pom in Maven?, and they differ from dependencies: a plugin extends the build process itself, whereas a dependency is a library your code compiles or runs against. If that distinction is what you are after, see What Is the Difference Between Plugins and Dependencies?.

Build Plugins vs Reporting Plugins

  • Build plugins: Run throughout the build process and are declared inside the <build><plugins> section of pom.xml. They compile code, run tests, and produce artifacts. Examples include the compiler, surefire, and jar plugins.
  • Reporting plugins: Run during site generation and are declared inside the <reporting><plugins> section. They produce HTML reports that appear on the generated project site, such as Javadoc and code-coverage reports.
  • Dual-purpose plugins: A handful of plugins, including the dependency and javadoc plugins, can be configured as either build or reporting plugins depending on where you declare them.

Common Maven Build Plugins and Their Uses

These are the plugins you will encounter in almost every Maven project, listed roughly in lifecycle order:

  • maven-clean-plugin: Deletes the target/ directory so each build starts from a clean state. It is bound to the clean lifecycle and runs when you call mvn clean.
  • maven-compiler-plugin: Compiles your main and test Java sources, and is where you set the Java release version. It binds to the compile and test-compile phases.
  • maven-resources-plugin: Copies and filters non-code resources, such as property files, into the output directory. It binds to process-resources and process-test-resources.
  • maven-surefire-plugin: Runs unit tests in an isolated classloader during the test phase. It executes JUnit and TestNG suites and is the plugin most automation engineers configure first.
  • maven-failsafe-plugin: Runs integration tests during integration-test and reports failures in verify, so post-test cleanup still runs even after a failure. It is the right plugin for slower end-to-end and Selenium suites.
  • maven-jar-plugin: Packages compiled classes and resources into a JAR file during the package phase, and can add a manifest entry for a main class.
  • maven-war-plugin: Packages a web application into a WAR file during package for deployment to servlet containers such as Tomcat.
  • maven-shade-plugin: Builds an uber-JAR that bundles your code together with all of its dependencies, and can relocate package names to avoid version conflicts. It runs in the package phase.
  • maven-assembly-plugin: Builds custom distributions from a descriptor, such as a zip or tar archive or a jar-with-dependencies. It also attaches to the package phase.
  • maven-install-plugin: Installs the built artifact into your local .m2 repository during the install phase so other local projects can depend on it.
  • maven-deploy-plugin: Uploads the final artifact to a remote repository, such as Nexus or Artifactory, during the deploy phase for sharing across teams.
  • maven-dependency-plugin: Copies, unpacks, and analyzes What Is Maven Dependency?; goals such as dependency:tree are usually run on demand rather than bound to a phase.
  • maven-site-plugin: Generates the project documentation site, including reports contributed by reporting plugins. It runs in the separate site lifecycle.

Reporting and Code-Quality Plugins

These plugins focus on documentation, coverage, and static analysis. Most are declared as reporting plugins so their output appears on the generated site:

  • maven-javadoc-plugin: Generates Javadoc API documentation for the project and can run as both a build and a reporting plugin.
  • jacoco-maven-plugin: Instruments your code and produces code-coverage reports for unit and integration tests, often used as a quality gate in CI.
  • maven-checkstyle-plugin: Checks source code against a configured coding-standard ruleset to keep formatting and conventions consistent.
  • maven-pmd-plugin: Performs static analysis to flag common programming flaws such as unused variables, empty blocks, and duplicated code.
  • spotbugs-maven-plugin: Analyzes compiled bytecode to detect likely bug patterns; it is the maintained successor to the older FindBugs plugin.

Maven Plugins Quick Reference

PluginPurposeLifecycle phase
maven-compiler-pluginCompiles main and test Java sourcescompile / test-compile
maven-resources-pluginCopies and filters resources to outputprocess-resources
maven-surefire-pluginRuns unit tests (JUnit / TestNG)test
maven-failsafe-pluginRuns integration testsintegration-test / verify
maven-jar-pluginPackages classes into a JARpackage
maven-shade-pluginBuilds an uber-JAR with dependenciespackage
maven-install-pluginInstalls artifact to local .m2 repositoryinstall
maven-deploy-pluginDeploys artifact to a remote repositorydeploy
maven-clean-pluginRemoves the target directoryclean

A Sample pom.xml Plugin Configuration

The snippet below declares the compiler and surefire plugins as build plugins. The compiler plugin sets the Java release, and surefire is pointed at a TestNG suite file so mvn test runs your test classes:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.13.0</version>
      <configuration>
        <release>17</release>
      </configuration>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.2.5</version>
      <configuration>
        <suiteXmlFiles>
          <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
      </configuration>
    </plugin>
  </plugins>
</build>

Running Maven Test Suites Across Browsers

Because surefire and failsafe simply launch your JUnit, TestNG, or Selenium tests, you do not need to change your plugin configuration to scale coverage. By pointing your WebDriver remote URL at a cloud Selenium grid, the same mvn test or mvn verify command can execute the suite across many browser and operating-system combinations in parallel, without maintaining local browser infrastructure.

TestMu AI provides such a grid: you keep your existing Maven plugins and test code, and run them across 3000+ real browser and OS environments on the Selenium cloud, which fits naturally into a Maven-based CI pipeline.

Frequently Asked Questions

What are the two categories of Maven plugins?

Apache Maven defines build plugins and reporting plugins. Build plugins run during the build and are declared in the <build> element of pom.xml. Reporting plugins run during site generation and are declared in the <reporting> element. A few plugins, such as the dependency and javadoc plugins, can act as both.

What is the difference between a Maven plugin and a goal?

A plugin is a bundle of one or more goals, and a goal is a single executable task such as compiler:compile or surefire:test. Goals are bound to lifecycle phases so they run automatically when that phase executes, or you can invoke a goal directly with mvn plugin:goal.

Which Maven plugin runs the tests?

The maven-surefire-plugin runs unit tests in the test phase, while the maven-failsafe-plugin runs integration tests across the integration-test and verify phases. Both can execute JUnit, TestNG, and Selenium-based Java suites.

What is the difference between maven-surefire-plugin and maven-failsafe-plugin?

Surefire runs unit tests during the test phase and fails the build immediately on a failure. Failsafe runs integration tests during integration-test but reports failures in verify, so post-integration cleanup (for example, shutting down a server) still runs even when a test fails.

How do I add a plugin to pom.xml?

Add a <plugin> block with its groupId, artifactId, and version inside <build><plugins> for a build plugin, or inside <reporting><plugins> for a reporting plugin. You can attach a goal to a phase using an <executions> block and pass settings through <configuration>.

What is the difference between maven-shade-plugin and maven-assembly-plugin?

Both can build a fat JAR that bundles dependencies. The shade plugin merges everything into a single uber-JAR and can relocate classes to avoid conflicts, while the assembly plugin builds custom distributions from a descriptor, such as zip or tar archives and a jar-with-dependencies.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests