Maven Lifecycle, Phases and Goals

[Updated: Mar 14, 2017, Created: Nov 22, 2015]
  1. Goal is the single unit of task which does some real work. For example the compile goal (runs as mvn compiler:compile) which compiles the Java source. All goals are provided by plugins, either by default plugins or by user defined plugins (configured in pom file).
  2. Phase is a group of ordered goals or in other words: zero or more plugin goals are bound to a phase (either by default or by user). For example the compile phase (runs as "mvn compile") consists only compiler:compile goal (plugin). Running a phase basically runs the plugins bound with it.
  3. Build Lifecycle is a group of ordered phases. There are three built-in lifecycle: default, clean and site. Please see the list of phases attached with each lifecycle. Build lifecycle cannot be executed, they are conceptual. if we execute a phase, For example mvn compile, all phases up to and including that phase (i.e. validate >generate-sources> process-sources>generate-resources> process-resources> compile) will get executed. A phase with zero plugin goals does nothing.

To understand the general idea behind above concepts, consider this diagram: If we execute phase 2 it will first run phase 1 (the two plugins bound to phase 1 will get executed) then phase 2 (the only one plugin goal bound with it will get executed).
If we run last phase all preceding phases will get executed first. Generally speaking, if we want to run some phase (or even goal/plugin), all pre-required task will run first. Note Phase 3 does nothing cause no goals are bound to it. In maven default life-cycle too, there are number of phases which does nothing. They are actually kind of place holders for the users plugins or different packaging types to bind goals.



Goals to phase binding depends on packaging type

Based on the packaging of a project (jar, war, ear, etc), different maven goals will be bound to different phases of the maven lifecycle. Please check out the reference docs here. One way to list this information is to use maven plugin help:describe which we will explore in the next tutorial.



Difference between Plugin and Goal

  1. A plugin can have multiple goals.
  2. A plugin may not necessarily be bound to a phase, meaning a plugin can be standalone (tool). Check out Available Plugins to have an idea. For example help plugin is a tool plugin. Please see next tutorial for examples.
  3. When a plugin goal is bound to a LifeCycle phase, it is basically to extend functionality not already found in that LifeCycle.
  4. A LifeCycle phase can be bound to one or more goals of a plugin
  5. A goal is also sometimes referred to Mojo

See Also