Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 41 ยท Lifecycle and Plugins

Plugins and Goals

Learn how Maven plugins and goals work: the plugin:goal syntax, default bindings per packaging, mvn compile versus compiler:compile, executions and pluginManagement.

Maven itself is a small engine. Everything useful it does, such as compiling, testing, packaging and publishing, is done by plugins. A plugin is a JAR that offers one or more goals, and a goal is one concrete task. The lifecycle you met in Lifecycles and Phases decides when goals run; plugins decide what they do.

This lesson explains the plugin:goal syntax, how goals are bound to phases for each packaging type, the difference between running a phase and running a goal directly, and how to configure plugins. All mvn commands and POM fragments are read-only, because Maven cannot run on this site. The Java TryIts are stand-ins that model the binding logic.

Plugins, goals, phases

Plugin A JAR that bundles related goals.
Goal One task, such as compiler:compile.
Phase A slot in the lifecycle.
Binding Which goal runs in which phase.
1 / 2

The plugin:goal syntax

A goal is written prefix:goal. The prefix names the plugin, and the goal names the task inside it.

Terminal
mvn compiler:compile
mvn surefire:test
mvn dependency:tree
mvn help:effective-pom

Plugin artifact IDs follow a naming pattern, and the prefix is the part that varies: maven-compiler-plugin has the prefix compiler, maven-surefire-plugin has surefire, and a third-party plugin named spring-boot-maven-plugin has the prefix spring-boot. The pattern is maven-<prefix>-plugin for official plugins and <prefix>-maven-plugin for others.

Prefixes are resolved from metadata in the plugin groups Maven knows about (the official org.apache.maven.plugins and org.codehaus.mojo are included by default). If a prefix is unknown, Maven says "No plugin found for prefix". Then you either add the plugin to your POM, or use the full coordinates:

Terminal
mvn org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile

The longest form is groupId:artifactId:version:goal, and it removes all guessing. You can inspect any plugin with mvn help:describe -Dplugin=compiler, and add -Ddetail to see every goal and parameter.

Default bindings per packaging

When you write <packaging>jar</packaging>, or leave it out, Maven loads a set of default bindings for that packaging. These are the reason a POM with no <build> section can still compile and package.

Phase jar packaging war packaging pom packaging
process-resources resources:resources resources:resources none
compile compiler:compile compiler:compile none
process-test-resources resources:testResources resources:testResources none
test-compile compiler:testCompile compiler:testCompile none
test surefire:test surefire:test none
package jar:jar war:war none
install install:install install:install install:install
deploy deploy:deploy deploy:deploy deploy:deploy

A pom project, such as a parent, has almost nothing to do: nothing to compile and no archive to build. It can still be installed and deployed, which is how parent POMs reach other developers. Packaging covers more formats.

mvn compile versus mvn compiler:compile

These look alike, but they behave differently.

  • mvn compile runs the lifecycle up to the compile phase, so validate, process-resources and compile all run. Resources are copied and the compiler goal runs.
  • mvn compiler:compile runs only the compiler plugin's compile goal. Nothing before it runs, so target/classes gets class files but resources are not copied.

Some goals expect earlier work. For example, mvn surefire:test run on a clean checkout finds no compiled test classes and reports that there are no tests to run. That is not a bug; you skipped the phases that produce them.

Use direct goals for tools that are not part of the normal build, such as mvn dependency:tree, mvn versions:display-dependency-updates or mvn help:effective-pom. Use phases for the build itself.

Configuring a plugin

Plugins are configured in <build><plugins>. You give coordinates and a <configuration>; the settings apply to every execution of that plugin.

XML
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.13.0</version>
      <configuration>
        <release>25</release>
        <compilerArgs>
          <arg>-Xlint:all</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

Always give a <version>. Without one, Maven uses the version from its own defaults, which differs between Maven releases and makes builds less repeatable. The <configuration> element names are the plugin's parameters; look them up with help:describe or the plugin's site.

Executions: binding extra goals

An execution attaches a goal to a phase. Defaults are created for you (with ids like default-compile), but you can add your own to run an extra goal. For instance, to build a sources JAR whenever you package:

XML
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <version>3.3.1</version>
  <executions>
    <execution>
      <id>attach-sources</id>
      <phase>package</phase>
      <goals>
        <goal>jar-no-fork</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Now mvn package also runs source:jar-no-fork. Many goals declare a default phase, so the <phase> element can sometimes be left out. When several goals share one phase, the packaging's defaults run first, then others in the order they appear in the POM.

pluginManagement

<pluginManagement> declares versions and configuration without activating a plugin. It works like dependencyManagement: children (and the project itself) pick up the settings when they mention the plugin, or when a lifecycle binding uses it.

XML
<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.5.2</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

Put plugin versions in the parent's pluginManagement once, and every child that runs surefire, directly or through the default binding, uses that version. It is the standard way to make builds consistent across many projects. See Parent POMs and the Effective POM.

A binding-table simulator

The stand-in below stores the default bindings for the three packagings. It adds a user execution and prints the plan Maven would follow for mvn package, in lifecycle order. It is a model of the idea, not Maven's real planner.

Java Which goals run for mvn package? (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The first plan shows the extra source:jar-no-fork after the default jar:jar. The war plan swaps in war:war, and the pom plan shows nothing at all up to package, just as the table says.

Calling a goal directly versus a phase

This second stand-in mimics the difference between mvn compile and mvn compiler:compile by tracking what is on disk. A goal declares what it needs; a direct call does not prepare it.

Java Phase versus direct goal (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The direct call leaves resources out, the phase call produces both, and surefire:test on a clean tree has nothing to run. In real projects, always prefer phases for building and goals for tools.

Tips

  • Pin the version of every plugin you use, ideally in a parent's pluginManagement.
  • Run mvn help:effective-pom to see the plugin versions Maven actually uses.
  • Use mvn help:describe -Dplugin=<prefix> -Dgoal=<goal> -Ddetail for a parameter list.
  • If a prefix is not found, use the full coordinates or add the plugin to the POM.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which part of surefire:test is the plugin prefix?

    In prefix:goal, the part before the colon names the plugin (here, maven-surefire-plugin shortened to surefire) and the part after names the goal.
  2. 2. How does mvn compiler:compile differ from mvn compile?

    A direct goal runs alone. A phase runs every earlier phase of the lifecycle first, including process-resources.
  3. 3. Why does a project with packaging pom have almost no work in mvn package?

    The pom packaging binds only install and deploy by default, because there is nothing to compile and no archive to build.
  4. 4. What does pluginManagement do?

    Like dependencyManagement, it supplies defaults. A plugin only runs when it is bound by packaging or listed in the build plugins.

Summary

  • Plugins are JARs of goals; goals are single tasks; phases are slots; bindings connect them.
  • Goals are called as prefix:goal, or with full groupId:artifactId:version:goal coordinates.
  • Each packaging (jar, war, pom) brings its own default bindings.
  • mvn compile runs the lifecycle; mvn compiler:compile runs only one goal.
  • Executions bind extra goals to phases; pluginManagement fixes versions and settings for the whole project.
  • Both TryIts are stand-ins; real plugin planning is done by Maven.

Next, tune the plugins you will use every day: The Core Plugins in Practice.